Volume control with Bluetooth

I have connected my phone to an ESP32 through bluetooth and added a speaker. I also have a HC-SR04 connected. I the volume on the speaker to be controlled by the outputs by the HC-SR04, but it seems like the volume on the phone is overruling the volume set by the HC-SR04.

I am using pschatzmann ESP32-A2DP library.

Is it possible to overwrite the volume set by the phone?

My code is:

#include "BluetoothA2DPSink.h"

BluetoothA2DPSink a2dp_sink;

int volume = 0;
const int trigPin = 5;
const int echoPin = 18;
const int numReadings = 10;
int readings[numReadings];
int readIndex = 0;
int total = 0;
int average = 0;
int minimum = 2;
int maximum = 400;

#define SOUND_SPEED 0.0343

long duration;
float distanceCm;
int power;
int invertPower;
long DSC = 3.1496

void volumeChanged(int newVolume) {
  Serial.println(newVolume);
  volume = newVolume;
}

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);
  const i2s_config_t i2s_config = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),
    .sample_rate = 44100,
    .bits_per_sample = (i2s_bits_per_sample_t)16,
    .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
    .communication_format = (i2s_comm_format_t)I2S_COMM_FORMAT_STAND_MSB,
    .intr_alloc_flags = 0,
    .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("Robert");

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(proxLED, OUTPUT);
  pinMode(alarmLED, OUTPUT);

  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distanceCm = duration * SOUND_SPEED/2;
  distanceCm = constrain(distanceCm, minimum, maximum);

  total = total - readings[readIndex];
  readings[readIndex] = distanceCm;
  total = total + readings[readIndex];
  readIndex = readIndex + 1;

  if (readIndex >= numReadings) {
    readIndex = 0;
  }

  average = total / numReadings;
  power = average * 5;

  Serial.print("  Distance (cm): ");
  Serial.print(distanceCm);
  Serial.print("  │  Average (cm): ");
  Serial.print(average);
  Serial.print("  │  Voltage (mV): ");
  Serial.print(power);

  invertPower = map(power, 0, 2000, 2000, 0);

  volume = average / DSC;
  Serial.print("  │  Volume: ");
  Serial.println(volume);

  delay(500);
}

Do you see this being called?

What in your code is changing the volume of the speaker?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.