I came across an EEPROM exampel to read and write WiFi details to the "EEPROM" on the ESP8266 (ESP-01)
It works with out a hitch, until I add my own code to store a few more integers as well as values from an integer array.
This was pulled from How To Save Values On Arduino With EEPROM - The Robotics Back-End
Working Code
EEPROM.begin(512);
//Reads a string out of memory
String read_string(uint8_t l, uint8_t p){
String temp;
for (uint8_t n = p; n < l+p; ++n)
{
Serial.print("readmem - ");
Serial.println(n);
if(char(EEPROM.read(n))!=';'){
if(isWhitespace(char(EEPROM.read(n)))){
//do nothing
}else temp += String(char(EEPROM.read(n)));
}else n=l+p;
}
return temp;
}
void readEEProm()
{
String string_Ssid="";
String string_Password="";
string_Ssid= read_string(30,0);
string_Password= read_string(30,60);
Serial.println("ssid: "+string_Ssid);
Serial.println("Password: "+string_Password);
string_Password.toCharArray(passwordc,30);
Serial.println("FOUND");
WiFiSSID = string_Ssid;
WiFiPassw = string_Password;
}
}
void write_to_Memory(String s, String p) {
s += ";";
write_EEPROM(s, 0);
p += ";";
write_EEPROM(p, 60);
EEPROM.commit();
flashGreen();
}
//write to memory
void write_EEPROM(String x, uint16_t pos) {
for (uint16_t n = pos; n < x.length() + pos; n++) {
EEPROM.write(n, x[n - pos]);
}
}
All the above works, I can store a SSIDname and password and read it.
I serial.print the output and I can verify it works, the readmem is just the memory location being read from
readmem - 0
readmem - 1
readmem - 2
readmem - 3
readmem - 4
readmem - 5
readmem - 6
readmem - 7
readmem - 8
readmem - 9
readmem - 10
readmem - 11
readmem - 60
readmem - 61
readmem - 62
readmem - 63
readmem - 64
readmem - 65
readmem - 66
readmem - 67
readmem - 68
readmem - 69
readmem - 70
readmem - 71
ssid: abcdefghijk
Password: abcdefghijk
BUT When I add my own code to store integer values to the ESP and read them back at boot. Nothing works, upon read every value is set to 255, and the first and third characters in the SSID name value are overwritten.
uint8_t readArray[15]
void write_settings_Memory()
{
uint16_t mempos = 120;
EEPROM.write(mempos, UPDATES_PER_SECOND);
Serial.print(mempos);
Serial.print(" - ");
Serial.println(UPDATES_PER_SECOND);
mempos = mempos +2;
EEPROM.write(mempos, BRIGHTNESS);
Serial.print(mempos);
Serial.print(" - ");
Serial.println(BRIGHTNESS);
mempos = mempos +2;
EEPROM.write(mempos, SPARKING);
Serial.print(mempos);
Serial.print(" - ");
Serial.println(SPARKING);
mempos = mempos +2;
EEPROM.write(mempos, COOLING);
Serial.print(mempos);
Serial.print(" - ");
Serial.println(COOLING);
mempos = mempos +2;
for(uint8_t i = 0; i < 15; i++)
{
EEPROM.write(readArray[i], mempos);
mempos = mempos +2;
Serial.print(mempos);
Serial.print(" - ");
Serial.println(readArray[i]);
}
Serial.println("Writing Settings");
EEPROM.commit();
}
void read_Settings()
{
uint16_t mempos = 120;
UPDATES_PER_SECOND = EEPROM.read(mempos);
Serial.print(mempos);
Serial.print(" - ");
Serial.println(UPDATES_PER_SECOND);
mempos = mempos +2;
BRIGHTNESS = EEPROM.read(mempos);
Serial.print(mempos);
Serial.print(" - ");
Serial.println(BRIGHTNESS);
mempos = mempos +2;
SPARKING = EEPROM.read(mempos);
Serial.print(mempos);
Serial.print(" - ");
Serial.println(SPARKING);
mempos = mempos +2;
COOLING = EEPROM.read(mempos);
Serial.print(mempos);
Serial.print(" - ");
Serial.println(COOLING);
mempos = mempos +2;
for(uint8_t i = 0; i < 15; i++)
{
readArray[i] = EEPROM.read(mempos);
mempos = mempos +2;
Serial.print(mempos);
Serial.print(" - ");
Serial.println(readArray[i]);
}
Serial.println("Reading Settings");
}
For writing my own settings...
As shown, I start at address 120 to being writing these values, and this is the output from console while writing the settings. I'm only changing the values for location 130 and 132 as shown.
120 - 255
122 - 255
124 - 255
126 - 255
130 - 2
132 - 0
134 - 255
136 - 255
138 - 255
140 - 255
142 - 255
144 - 255
146 - 255
148 - 255
150 - 255
152 - 255
154 - 255
156 - 255
158 - 255
Now, when I reboot my device, it will read the WiFi details as well as the other values I've tried to store. (locations 0-11 are the SSID and 60-71 are the Password)
readmem - 0
readmem - 1
readmem - 2
readmem - 3
readmem - 4
readmem - 5
readmem - 6
readmem - 7
readmem - 8
readmem - 9
readmem - 10
readmem - 11
readmem - 60
readmem - 61
readmem - 62
readmem - 63
readmem - 64
readmem - 65
readmem - 66
readmem - 67
readmem - 68
readmem - 69
readmem - 70
readmem - 71
ssid: ⸮b⸮defghijk
Password: abcdefghijk
Here you can see the ssid has an invalid first and third character, appears its getting overwritten. This only occurs when I try and write my integers to the EEPROM. if I only write the ssid and password, it never corrupts.
After those values are read, it then starts at address 120 and begins to read what should be my adjusted values (specifically 130 and 132. they should be 2 and 0, not 255)
120 - 255
122 - 255
124 - 255
126 - 255
130 - 255
132 - 255
134 - 255
136 - 255
138 - 255
140 - 255
142 - 255
144 - 255
146 - 255
148 - 255
150 - 255
152 - 255
154 - 255
156 - 255
158 - 255
If I am understanding what I am trying to do.
Starting at memory location 0-59 it stores the SSID
Memory location 60-119 stores the Password
then starting at memory location 120 it begins to store the uint8_t (single byte integer) values. I increments +2 each time as I've read an integer takes up 2 bytes
So integer 1 goes into 120-121
Integer 2 goes into 122-123 and so on.
I'm new too working with the EEPROM, even more so working with an ESP. What am I doing wrong?
Side note, I'm going to change most of the EEPROM.write to EEPROM.update to avoid crushing the EEPROM with writes, and being its an ESP, I do perform the EEPROM.commit after storing values.