Hi I'm using this example from visual micro for a bluetooth speaker.
I can see in the serial plotter that when music starts to play the "state" is displayed. what im trying to do is when this state is "started" to turn on a relay and turn off if its not. ive tried an if statement but failed miserably. i was wondering if someone could help.
/*
Streaming data from Bluetooth to internal DAC of ESP32
Using Library: https://github.com/pschatzmann/ESP32-A2DP
Copyright (C) 2020 Phil Schatzmann
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Connect one side of speaker to GND, the other to GPIO25
// Can use existing speakers / headphones --> https://i.pinimg.com/originals/4e/72/72/4e72729dba13164ad78586395fe11873.png
// ==> Example to use built in DAC of ESP32
#include "BluetoothA2DPSink.h"
int Relay = 2; // Digital pin D5
BluetoothA2DPSink a2dp_sink;
// VolumeChanged doesnt work
int volume = 0;
void volumeChanged(int newVolume) {
Serial.println(newVolume);
volume = newVolume;
}
// for esp_a2d_connection_state_t see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/bluetooth/esp_a2dp.html#_CPPv426esp_a2d_connection_state_t
void connection_state_changed(esp_a2d_connection_state_t state, void* ptr) {
Serial.println(a2dp_sink.to_str(state));
}
// for esp_a2d_audio_state_t see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/bluetooth/esp_a2dp.html#_CPPv421esp_a2d_audio_state_t
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(Relay, OUTPUT);
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_volumechange(volumeChanged);
a2dp_sink.set_on_connection_state_changed(connection_state_changed);
a2dp_sink.set_on_audio_state_changed(audio_state_changed);
a2dp_sink.start("vMicro BT Speaker");
}
void loop() {
}