Problem saving "Char" var to EEPROM on Esp32

Hello , I am making a project in which I need to store the wifi credentials to the EEPROM. For now, I am able to store a bool array in the EEPROM but not the Char variables. I can not share the complete code since it is REALLY long but here are the problematic functions :

To save to the EEPROM:

//////////////////////////////////WORKING////////////////////////////////
void saveToEEPROM() {
  // Save the contents of the array in EEPROM memory
  for (int i = 0; i < NUM_PARAM_ITEMS; i++) {
    EEPROM.write(i, menu_items_param_bool[i]);
  }
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////NOT WORKING////////////////////////////////
  // Save WiFi credentials in EEPROM if not empty
  int offset = NUM_PARAM_ITEMS;

  if (strlen(ssid1) > 0) {
    for (int i = 0; i < strlen(ssid1); i++) {
      EEPROM.write(offset + i, ssid1[i]);
    }
    offset += strlen(ssid1);
    EEPROM.write(offset, '\0');  // Null-terminate the SSID1 string
  }

  offset++;
  if (strlen(password1) > 0) {
    for (int i = 0; i < strlen(password1); i++) {
      EEPROM.write(offset + i, password1[i]);
    }
    offset += strlen(password1);
    EEPROM.write(offset, '\0');  // Null-terminate the Password1 string
  }

  offset++;
  if (strlen(ssid2) > 0) {
    for (int i = 0; i < strlen(ssid2); i++) {
      EEPROM.write(offset + i, ssid2[i]);
    }
    offset += strlen(ssid2);
    EEPROM.write(offset, '\0');  // Null-terminate the SSID2 string
  }

  offset++;
  if (strlen(password2) > 0) {
    for (int i = 0; i < strlen(password2); i++) {
      EEPROM.write(offset + i, password2[i]);
    }
    offset += strlen(password2);
    EEPROM.write(offset, '\0');  // Null-terminate the Password2 string
  }

  EEPROM.commit();  // Don't forget to call the commit() function to save the data to EEPROM
}

To read from the EEPROM:

void readFromEEPROM() {
//////////////////////////////////WORKING////////////////////////////////
  // Read the contents of the EEPROM memory and restore it to the array
  for (int i = 0; i < NUM_PARAM_ITEMS; i++) {
    menu_items_param_bool[i] = EEPROM.read(i);
  }
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////NOT WORKING////////////////////////////////
  // Read WiFi credentials from EEPROM if not empty
  int offset = NUM_PARAM_ITEMS;

  // Read SSID1
  int ssid1Length = EEPROM.read(offset);
  if (ssid1Length > 0) {
    for (int i = 0; i < ssid1Length; i++) {
      ssid1[i] = (char)EEPROM.read(offset + i + 1);
    }
    ssid1[ssid1Length] = '\0';  // Null-terminate the SSID1 string
    Serial.print("SSID1: ");
    Serial.println(ssid1);
  }

  offset += ssid1Length + 1;

  // Read Password1
  int password1Length = EEPROM.read(offset);
  if (password1Length > 0) {
    for (int i = 0; i < password1Length; i++) {
      password1[i] = (char)EEPROM.read(offset + i + 1);
    }
    password1[password1Length] = '\0';  // Null-terminate the Password1 string
    Serial.print("PASSWORD1: ");
    Serial.println(password1);
  }

  offset += password1Length + 1;

  // Read SSID2
  int ssid2Length = EEPROM.read(offset);
  if (ssid2Length > 0) {
    for (int i = 0; i < ssid2Length; i++) {
      ssid2[i] = (char)EEPROM.read(offset + i + 1);
    }
    ssid2[ssid2Length] = '\0';  // Null-terminate the SSID2 string
    Serial.print("SSID2: ");
    Serial.println(ssid2);
  }

  offset += ssid2Length + 1;

  // Read Password2
  int password2Length = EEPROM.read(offset);
  if (password2Length > 0) {
    for (int i = 0; i < password2Length; i++) {
      password2[i] = (char)EEPROM.read(offset + i + 1);
    }
    password2[password2Length] = '\0';  // Null-terminate the Password2 string
    Serial.print("PASSWORD2: ");
    Serial.println(password2);
  }
}

And the variable to save and read are :

char ssid1[32] = "ssid1";              // SSID of the first WiFi network
char password1[64] = "password1";         // Password of the first WiFi network
char ssid2[32] = "ssid2";       // SSID of the second WiFi network
char password2[64] = "password2";  // Password of the second WiFi network

Can anybody help me?

Do yourself a favour and use EEPROM.put() and EEPROM.get() to save and load a whole array (or any other type of variable) with a single command

1 Like

So I could directly write and read the char variables?

Yes

Note that it is your responsibility to manage the EEPROM address to which the variable is written such that adjacent variables do not overlap in EEPROM. Don't forget that an array holding a string of characters such as "password1" is actually one character longer than the visible characters to allow for the terminating zero of the string

The eeprom library on the esp32 is deprecated and it is better to use Preferences.h.

https://randomnerdtutorials.com/esp32-save-data-permanently-preferences/

#include <Preferences.h>
Preferences prefs;

String MyEmail    = "myEmail@myProvider.com";
String MyPassWord = "thereisnopassword";
String MySecret   = "NUX007";

String MyEmailR    = "";
String MyPassWordR = "";
String MySecretR   = "";


void setup() {
  Serial.begin(115200);
  prefs.begin("Settings"); //namespace
  
  //key/value pairs within namespace
  prefs.putString("MyEmail", MyEmail);
  prefs.putString("MyPassWord", MyPassWord);
  prefs.putString("MySecret", MySecret);

  MyEmailR = prefs.getString("MyEmail");
  Serial.println(MyEmailR);
  MyPassWordR = prefs.getString("MyPassWord");
  Serial.println(MyPassWordR);
  MySecretR = prefs.getString("MySecret");
  Serial.println(MySecretR);

}

void loop() {}

Thanks! I'll check that right now

The only reason that I can see why EEPROM is deprecated in favour of Preferences.h is that the latter does not require the user to keep track of EEPROM addresses but for simple applications that is not a problem

Preferences.h also does automatic wear leveling by moving the stored data around in a larger NV ram space. Address and wear leveling can certainly be managed with the eeprom library, but why bother? With eeprom.h deprecated, it's not clear that the previously reserved nv ram location can't go away if espressif wants it for something else.

I'm testing it and it looks very promising. Do I mark the problem as resolved?

If you feel that one of the posts in the topic has provided a solution to your problem then you can flag it as a solution

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