pschatzmann / ESP32-A2DP

A Simple ESP32 Bluetooth A2DP Library (to implement a Music Receiver or Sender) that supports Arduino, PlatformIO and Espressif IDF
Apache License 2.0
1.76k stars 280 forks source link

NVS Initialization Error in BluetoothA2DPCommon Library #633

Closed dpoettler closed 1 week ago

dpoettler commented 1 week ago

Problem Description

Problem Description When attempting to use the BluetoothA2DPCommon library on an ESP32 device, the following error messages are encountered during startup:

[   271][E][BluetoothA2DPCommon.cpp:197] read_address(): [BT_AV] NVS OPEN
ERROR
[   279][E][BluetoothA2DPCommon.cpp:206] read_address(): [BT_AV] nvs_get_blob failed

After that I can still connect to the ESP via Bluetooth and use it as playback device but nothing else happens.

I have also used a no_ota.csv file to get more space for my code:

# Name,    Type,    SubType,    Offset,    Size,    Flags
nvs,       data,    nvs,        0x9000,    0x6000,  
app0,      app,     ota_0,      0x10000,   0x1F0000,  
spiffs,    data,    spiffs,     0x200000,  0x1E0000,  
coredump,  data,    coredump,   0x3E0000,  0x20000,

Device Description

Sketch

#include <BluetoothA2DPCommon.h>
#include <BluetoothA2DPSink.h>
#include <BluetoothA2DPSource.h>
#include <SoundData.h>
#include <A2DPVolumeControl.h>
#include <config.h>
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <FS.h>
#include <SPIFFS.h>
#include <nvs_flash.h>

BluetoothA2DPSource a2dp_source;
BluetoothA2DPSink a2dp_sink;

#define RX1 16
#define TX1 17

#define I2C_SDA 33
#define I2C_SCL 25

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SH110X_I2C_ADDRESS 0x3C

Adafruit_SH1106G display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  // Initialize NVS
  esp_err_t ret = nvs_flash_init();
  if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
    // NVS partition was truncated and needs to be erased
    Serial.println("NVS partition needs to be erased");
    ESP_ERROR_CHECK(nvs_flash_erase());
    // Retry nvs_flash_init
    ret = nvs_flash_init();
  }
  if (ret != ESP_OK) {
    Serial.printf("NVS initialization failed with error: %s\n", esp_err_to_name(ret));
  }
  ESP_ERROR_CHECK(ret);

  Wire.begin(I2C_SDA, I2C_SCL);
  Serial.begin(115200);
  Serial.println("Starting...");
  if (!display.begin(SH110X_I2C_ADDRESS)) {
    Serial.println(F("SH110X allocation failed"));
    for (;;);
  }
  display.clearDisplay();
  delay(100);
  display.setTextSize(1);
  display.setCursor(10, 20);
  display.setTextColor(SH110X_WHITE);
  display.println(F("Hello World"));
  display.display();

  static const i2s_pin_config_t my_pin_config = {
      .mck_io_num = 0,
      .bck_io_num = 26,
      .ws_io_num = 25,
      .data_out_num = 27,
      .data_in_num = I2S_PIN_NO_CHANGE
  };

  static i2s_config_t i2s_config = {
      .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
      .sample_rate = 44100,
      .bits_per_sample = (i2s_bits_per_sample_t)32,
      .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
      .communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_STAND_I2S),
      .intr_alloc_flags = 0,
      .dma_buf_count = 8,
      .dma_buf_len = 64,
      .use_apll = true,
      .tx_desc_auto_clear = true
  };

  a2dp_sink.set_i2s_config(i2s_config);
  a2dp_sink.set_pin_config(my_pin_config);
  a2dp_sink.set_auto_reconnect(false);
  a2dp_sink.set_avrc_metadata_attribute_mask(ESP_AVRC_MD_ATTR_TITLE);
  a2dp_sink.set_avrc_metadata_callback(avrc_metadata_callback);
  a2dp_sink.start("NEO - Test");
}

void loop() {
  delay(1000);
}

void avrc_metadata_callback(uint8_t data1, const uint8_t *data2) {
  String metadata = String((char*)data2);
  int separatorIndex = metadata.indexOf(':');
  if (separatorIndex != -1) {
    String artist = metadata.substring(0, separatorIndex);
    String title = metadata.substring(separatorIndex + 1);
    displaySongInfo(artist.c_str(), title.c_str());
  }
}

void displaySongInfo(const char* artist, const char* title) {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(0, 0);
  display.println("Artist:");
  display.println(artist);
  display.println("Title:");
  display.println(title);
  display.display();
}

Other Steps to Reproduce

No response

Provide your Version of the EP32 Arduino Core (or the IDF Version)

PIO Core 6.1.16

I have checked existing issues, discussions and online documentation

pschatzmann commented 1 week ago

I can't see any problem here: the nvs is used to store the last connected address if you decide to activate the auto reconnect, so you can just ignore this.

If the error message is disturbing you feel free to submit a correction via a pull request.

ps. I don't recommend to your obsolete esp32 core version with this old i2s API!
The actual version is 3.0.8 with 3.1 coming out soon.