ESP32. Write/Read in EEPROM. Send by classic Bluetooth

Hello friends,

in this example:

  • We write a text (less than 32 bytes) in the Serial Monitor. We press "Send".
  • This text will be WRITTEN in positions 0 ... 32 of the EEPROM.
  • EEPROM positions 0 ... 32 will be READ and displayed on the Serial Monitor.
  • It will be sent via classic Bluetooth from the ESP32 to the App.

#include <EEPROM.h>
#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth no activado! Activa la conexion Bluetooth.
#endif

BluetoothSerial SerialBT;

const char* texto = ""; //MAX 32
String texto_read = "";

char rx_byte = 0;
String rx_str = "";

void setup() {
Serial.begin(9600);
SerialBT.begin("ESP32test");
}

void loop() {
if (Serial.available() > 0) { // ¿Hay algún caracter?
rx_byte = Serial.read(); // Toma el caracter
rx_str += rx_byte;

      if (rx_byte == '\n') {
      EEPROM_ESP32_WRITE(rx_str, 0); // Save in address 0...32
      Serial.print("texto Written to EEPROM: ");
      Serial.println(rx_str);
      rx_str = "";
      
      texto_read = (String) EEPROM_ESP32_READ(0,32);  // Read 0...32 address
      Serial.print("texto Read from EEPROM: ");
      Serial.println(texto_read);
      
      Serial.println(".....................");
      Serial.print("Now texto sended by Bluetooth: ");
      Serial.println(texto_read);
      SerialBT.print(texto_read);
      }
}
}

//// Function WRITE EEPROM
void EEPROM_ESP32_WRITE(String buffer, int N) {
EEPROM.begin(512); delay(10);
for (int L = 0; L < 32; ++L) {
EEPROM.write(N + L, buffer[L]);
}
EEPROM.commit();
}

//// Function READ EEPROM
String EEPROM_ESP32_READ(int min, int max) {
EEPROM.begin(512); delay(10); String buffer;
for (int L = min; L < max; ++L)
// if (isAlphaNumeric(EEPROM.read(L)))
buffer += char(EEPROM.read(L));
return buffer;
}
 

p9A0i_bluetooth_eeprom.aia (3.0 KB)