0

I'm facing an issue while trying to write and retrieve a String in EEPROM of an ESP32 NodeMCU board.

I followed this tutorial : https://roboticsbackend.com/arduino-write-string-in-eeprom/

Here is my code :

#include <EEPROM.h>

void writeStringToEEPROM(int addrOffset, const String &strToWrite)
{
  byte len = strToWrite.length();
  EEPROM.write(addrOffset, len);
  for (int i = 0; i < len; i++)
  {
    EEPROM.write(addrOffset + 1 + i, strToWrite[i]);
  }
}
String readStringFromEEPROM(int addrOffset)
{
  int newStrLen = EEPROM.read(addrOffset);
  char data[newStrLen + 1];
  for (int i = 0; i < newStrLen; i++)
  {
    data[i] = EEPROM.read(addrOffset + 1 + i);
  }
  data[newStrLen] = '\0'; // !!! NOTE !!! Remove the space between the slash "/" and "0" (I've added a space because otherwise there is a display bug)
  return String(data);
}

void setup() {
  Serial.begin(115200);
  Serial.println("-- Starting --");
  writeStringToEEPROM(0, "Hello Arduino");
  
  String retrievedString = readStringFromEEPROM(0);
  Serial.print("The String we read from EEPROM: ");
  Serial.println(retrievedString);
}

void loop() {}

And here is the output :

ets Jun  8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:10944
load:0x40080400,len:6388
entry 0x400806b4
-- Starting --
The String we read from EEPROM: 

The Serial.println(retrievedString) return is blanck...

Cyril Bvt
  • 611
  • 6
  • 9
  • the EEPROM on esp32 is emulated in flash and that requires some additional commands like begin and commit. see the examples – Juraj Dec 30 '21 at 12:20

1 Answers1

0

@Juraj is right, the EEPROM.h library is different for Arduino and ESP32. Before using the function, you must initialize the memory size with begin() and the write function works like the update function. Once you have executed the write function, you need to perform a commit()

#include <EEPROM.h>

#define EEPROM_SIZE 24

void writeStringToEEPROM(int addrOffset, const String &strToWrite)
{
  byte len = strToWrite.length();  
  EEPROM.write(addrOffset, len);
  delay(500);
  for (int i = 0; i < len; i++)
  {
    EEPROM.write(addrOffset + 1 + i, strToWrite[i]);
    delay(500);
  }
  EEPROM.commit();
}
String readStringFromEEPROM(int addrOffset)
{
  int newStrLen = EEPROM.read(addrOffset);
  char data[newStrLen + 1];
  for (int i = 0; i < newStrLen; i++)
  {
    data[i] = EEPROM.read(addrOffset + 1 + i);
  }
  data[newStrLen] = '\0';
  return String(data);
}

void setup() {
  Serial.begin(115200);
  EEPROM.begin(EEPROM_SIZE);
  Serial.println("-- Starting --");
  writeStringToEEPROM(0, "Hello Arduino");
  
  String retrievedString = readStringFromEEPROM(0);
  Serial.print("The String we read from EEPROM: ");
  Serial.println(retrievedString);
}

void loop() {}
Cyril Bvt
  • 611
  • 6
  • 9
  • the `write` function doesn't work as `update` function. both libraries have `write` and `update` https://www.arduino.cc/en/Reference/EEPROM – Juraj Dec 30 '21 at 14:34