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?