#include "BluetoothA2DPSink.h"
BluetoothA2DPSink a2dp_sink;
int VolumeupButton = 13;
int VolumedownButton = 14;
int PlayButton = 15;
int PauseButton = 16;
int StopButton = 17;
int NextButton = 18;
int PreviousButton = 19;
int command;
int Volume = 100;
const int MaxVolume = 2000;
const int MinVolume = 0;
const int VolumeIncrement = 100;
#include <BleSerial.h>
#include <esp_attr.h>
#include <esp_task_wdt.h>
#include <driver/rtc_io.h>
#include "soc/rtc_wdt.h"
const int BUFFER_SIZE = 8192;
BleSerial SerialBT;
uint8_t unitMACAddress[6]; // Use MAC address in BT broadcast and display
char deviceName[20]; // The serial string that is broadcast.
uint8_t bleReadBuffer[BUFFER_SIZE];
uint8_t serialReadBuffer[BUFFER_SIZE];
void startBluetooth() {
// Get unit MAC address
esp_read_mac(unitMACAddress, ESP_MAC_WIFI_STA);
unitMACAddress[5] += 2;
//Create device name
sprintf(deviceName, "BleBridge-%02X%02X", unitMACAddress[4], unitMACAddress[5]);
//Init BLE Serial
SerialBT.begin(deviceName);
SerialBT.setTimeout(10);
}
//Task for reading Serial Port
void ReadSerialTask(void *e) {
while (true) {
if (Serial.available()) {
auto count = Serial.readBytes(serialReadBuffer, BUFFER_SIZE);
SerialBT.write(serialReadBuffer, count);
}
delay(20);
}
}
//Task for reading BLE Serial
void ReadBtTask(void *e) {
while (true) {
if (SerialBT.available()) {
auto count = SerialBT.readBytes(bleReadBuffer, BUFFER_SIZE);
Serial.write(bleReadBuffer, count);
}
delay(20);
}
}
void connection_state_changed(esp_a2d_connection_state_t state, void* ptr) {
Serial.println(a2dp_sink.to_str(state));
}
void audio_state_changed(esp_a2d_audio_state_t state, void* ptr) {
Serial.println(a2dp_sink.to_str(state));
}
void setup() {
Serial.begin(115200);
pinMode(13, INPUT);
pinMode(14, INPUT);
pinMode(15, INPUT);
pinMode(16, INPUT);
pinMode(17, INPUT);
pinMode(18, INPUT);
pinMode(19, INPUT);
Serial.setRxBufferSize(BUFFER_SIZE);
Serial.setTimeout(10);
//Start BLE
startBluetooth();
//Disable watchdog timers
disableCore0WDT();
disableCore1WDT();
disableLoopWDT();
esp_task_wdt_delete(NULL);
rtc_wdt_protect_off();
rtc_wdt_disable();
//Start tasks
xTaskCreate(ReadSerialTask, "ReadSerialTask", 10240, NULL, 1, NULL);
xTaskCreate(ReadBtTask, "ReadBtTask", 10240, NULL, 1, NULL);
const i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),
.sample_rate = 44100, // corrected by info from bluetooth
.bits_per_sample = (i2s_bits_per_sample_t)16, /* the DAC module will only take the 8bits from MSB */
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = (i2s_comm_format_t)I2S_COMM_FORMAT_STAND_MSB,
.intr_alloc_flags = 0, // default interrupt priority
.dma_buf_count = 8,
.dma_buf_len = 64,
.use_apll = false
};
a2dp_sink.set_i2s_config(i2s_config);
a2dp_sink.set_on_connection_state_changed(connection_state_changed);
a2dp_sink.set_on_audio_state_changed(audio_state_changed);
a2dp_sink.start("Speaker");
}
void loop() {
// put your main code here, to run repeatedly:
int Volumeupstate;
int Volumedownstate;
int Playstate;
int Pausestate;
int Stopstate;
int Nextstate;
int Previousstate;
Volumeupstate = digitalRead(13);
Volumedownstate = digitalRead(14);
Playstate = digitalRead(15);
Pausestate = digitalRead(16);
Stopstate = digitalRead(17);
Nextstate = digitalRead(18);
Previousstate = digitalRead(19);
if ( Volumeupstate == HIGH) {
digitalWrite(13, HIGH);
Volume += VolumeIncrement;
if(Volume > MaxVolume){
Volume = MaxVolume;
}
Serial.println(Volume);
}
else if (Volumedownstate == HIGH) {
digitalWrite(14, HIGH);
Volume -= VolumeIncrement;
if(Volume < MinVolume){
Volume = MinVolume;
}
Serial.println(Volume);
}
else if (Playstate == HIGH) {
digitalWrite(15, HIGH);
a2dp_sink.play();
}
else if (Pausestate == HIGH) {
digitalWrite(16, HIGH);
a2dp_sink.pause();
}
else if (Stopstate == HIGH) {
digitalWrite(17, HIGH);
a2dp_sink.stop();
}
else if (Nextstate == HIGH) {
digitalWrite(18, HIGH);
a2dp_sink.next();
}
else if (Previousstate == HIGH) {
digitalWrite(19, HIGH);
a2dp_sink.previous();
}
}
what i want to do is increase and decrease the volume using push button on esp32 when i am playing music on the Bluetooth to phone. I have a push button with 10k ohm connected on the breadboard. can someone please help me.