ESP8266 EEPROM WRITE / READ PROBLEM

Hi everyone I am testing my device with EEPROM library .

I am trying to save ssid string and read it after reloading sketch.

Step 1 : Write and Read in same sketch works well →

#define EPROM_MEMORY_SIZE 512
int serverAccessSSIDAddressStart=1; int serverAccessSSIDAddressEnd=32;

void writeStringEEPROM(String s,int startAdress,int endAdress);
String readStringEEPROM(int startAdress,int endAdress);

void setup() {
  Serial.begin(115200);
  
  String Server_router_ssid ="A111111111111111111111111111111A"; 

  Serial.println("Start Write");
  EEPROM.begin(EPROM_MEMORY_SIZE);
  writeStringEEPROM(Server_router_ssid,serverAccessSSIDAddressStart,serverAccessSSIDAddressEnd);
  EEPROM.commit();
  Serial.println("Write Done");
  
  Serial.println("READ");
  //EEPROM.begin(EPROM_MEMORY_SIZE);
  String res = readStringEEPROM(serverAccessSSIDAddressStart,serverAccessSSIDAddressEnd);
  Serial.println(res);
}


String readStringEEPROM(int startAdress,int endAdress){
   String s;
   for(int i=startAdress; i<=endAdress; i++){
        char c = char(EEPROM.read(i));
        s+=c;
    }
    return s;
}

void writeStringEEPROM(String s,int startAdress,int endAdress)
{
    int stringLength=s.length();
    
    for(int i=startAdress; i<=endAdress; i++){
        int indexString=i-startAdress;
        if(indexString<stringLength){
          EEPROM.write(i,s[indexString]);
        }else{
          EEPROM.write(i,' ');
        }
    }
}

loop(){}

Output :

Start Write
Write Done
READ
A111111111111111111111111111111A

But Now when Upload this sketch :

#define EPROM_MEMORY_SIZE 512
int serverAccessSSIDAddressStart=1; int serverAccessSSIDAddressEnd=32;

void writeStringEEPROM(String s,int startAdress,int endAdress);
String readStringEEPROM(int startAdress,int endAdress);

void setup() {
  Serial.begin(115200);
  
  //String Server_router_ssid ="A111111111111111111111111111111A"; 

  //Serial.println("Start Write");
  //EEPROM.begin(EPROM_MEMORY_SIZE);
  //writeStringEEPROM(Server_router_ssid,serverAccessSSIDAddressStart,serverAccessSSIDAddressEnd);
  //EEPROM.commit();
  //Serial.println("Write Done");
  
  Serial.println("READ");
  EEPROM.begin(EPROM_MEMORY_SIZE);
  String res = readStringEEPROM(serverAccessSSIDAddressStart,serverAccessSSIDAddressEnd);
  Serial.println(res);
}

String readStringEEPROM(int startAdress,int endAdress){
   String s;
   for(int i=startAdress; i<=endAdress; i++){
        char c = char(EEPROM.read(i));
        s+=c;
    }
    return s;
}

void writeStringEEPROM(String s,int startAdress,int endAdress)
{
    int stringLength=s.length();
    
    for(int i=startAdress; i<=endAdress; i++){
        int indexString=i-startAdress;
        if(indexString<stringLength){
          EEPROM.write(i,s[indexString]);
        }else{
          EEPROM.write(i,' ');
        }
    }
}

loop(){}

I get this empty output ):

READ

Any problem in my code please ?

Should I use EEPROM.begin when only writing ?

When should I use EEPROM.end and when should I use EEPROM.commit (I know that end includes commit ) ?

Any problem in my code please ?

One major problem for getting help here is that you have not posted all your code.

6v6gt:
One major problem for getting help here is that you have not posted all your code.

What do you mean please I don’t understand , If you mean that I should post all my code,I think that I did post all my code , anything missing for try help solve my problem?

stavbodik:
What do you mean please I dont understand , If I should post all my code ...I did post all my code .

Where is EPROM_MEMORY_SIZE defined in your second sketch ? loop() is also missing.

6v6gt:
Where is EPROM_MEMORY_SIZE defined in your second sketch ? loop() is also missing.

This is same Sketch as the first one but setup edited ,loop() is empty ...I did edit my post . :confused:

OK. Here is a simple example which should answer your questions. http://www.esp8266.com/viewtopic.php?p=16581

6v6gt:
OK. Here is a simple example which should answer your questions. http://www.esp8266.com/viewtopic.php?p=16581

Thanks , Unfortunately saw this post already , there is no problem like mine .

EEPROM.write does not write to flash immediately, instead you must call EEPROM.commit() whenever you wish to save changes to flash. EEPROM.end() will also commit, and will release the RAM copy of EEPROM contents.

This is what the function does , I ask when to use it?
(I don't understand what the meaning of "will release the RAM copy of EEPROM" )

What can be the reason that I don't see any output after write to EEPROM is done and I reload the device with new sketch ?

Here from https://github.com/adafruit/ESP8266-Arduino

EEPROM

This is a bit different from standard EEPROM class. You need to call EEPROM.begin(size) before you start reading or writing, size being the number of bytes you want to use. Size can be anywhere between 4 and 4096 bytes.

EEPROM.write does not write to flash immediately, instead you must call EEPROM.commit() whenever you wish to save changes to flash. EEPROM.end() will also commit, and will release the RAM copy of EEPROM contents.

EEPROM library uses one sector of flash located at 0x7b000 for storage.

EEPROM activities make a copy of the EEPROM in RAM storage. This is deleted when EEPROM.end() is called.

For testing to ensure that your problem is really that the EEPROM has not been written to, I’d change the function in your second sketch as follows:

String readStringEEPROM(int startAdress,int endAdress){
   String s = "";
   Serial.println( ) ;
   for(int i=startAdress; i<=endAdress; i++){
        char c = char(EEPROM.read(i));
        Serial.print( c ) ;
        s+=String( c ) ;
    }
    Serial.println( ) ;
    return s;
}

Thanks you very much 6v6gt ,

So I understood the problem , The thing is that when you use write function you must add delay at least of 100ms after each write operation as in this example :

https://github.com/esp8266/Arduino/blob/master/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino

And when you use read function you must add delay at least of 500ms after each read operation as in this example :

https://github.com/esp8266/Arduino/blob/master/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino

So I added to my functions(readStringEEPROM,writeStringEEPROM) inside the for loop this delays and now everything works great.

The problem is that this is very slow so i preferred to use get,put functions which use memcpy for this operations which looks to be faster and here is final working code if anyone will need it in the feature :

#include <EEPROM.h>

// EEPROM MAP  addresses
size_t EPROM_MEMORY_SIZE= 512;
int stmodeAddressStart = 0; int stmodeAddressEnd = 0;                               // size 1  byte  which stores 1 bit (0/1)
int serverAccessSSIDAddressStart = 1; int serverAccessSSIDAddressEnd = 34;          // size 34 bytes of char includes \0
int serverAccessPasswordAddressStart = 35; int serverAccessPasswordAddressEnd = 68; // size 34 bytes of char includes \0
int serverAccessIPAddressStart = 69; int serverAccessIPAddressEnd = 72;             // size 4  bytes , each byte is int in range of 0-255
int serverAccessPortAddressStart = 73; int serverAccessPortAddressEnd = 74;         // size 2  bytes , 2 bytes represent int int range of 0-65535


void setup() {
	Serial.begin(115200);

	char Server_router_ssid[] = "A1111111111111111111111111111111A\0";	
	char Server_router_password[] = "A1121112111111111111111111211111A\0";
	String ip = "233.14.180.100";
	int port = 5000;

	Serial.println("");
	Serial.println("Start Write");
	EEPROM.begin(EPROM_MEMORY_SIZE);
	EEPROM.put(serverAccessSSIDAddressStart, Server_router_ssid);
	delay(500);
	Serial.println("Write 1 done");
	EEPROM.put(serverAccessPasswordAddressStart, Server_router_password);
	delay(500);
	Serial.println("Write 2 Done");
	
	int ipArr[4];
	ConvertStringIpToIntArray(ip,ipArr);
	for (int i = 0; i < 4; i++) {
		EEPROM.write(serverAccessIPAddressStart+i,ipArr[i]);
		delay(100);
	}

	Serial.println("Write 3 Done");

	EEPROM.put(serverAccessPortAddressStart, port);
	delay(100);

	Serial.println("Write 4 Done");

	EEPROM.commit();
	Serial.println("commited");


	Serial.println("READ");
	EEPROM.begin(EPROM_MEMORY_SIZE);
	char res[34];
	EEPROM.get(serverAccessSSIDAddressStart, res);
	Serial.println(res);
	
	char res2[34];
	EEPROM.get(serverAccessPasswordAddressStart, res2);
	Serial.println(res2);

	int res3[4];
	for (int i = 0; i < 4; i++) {
		res3[i] = EEPROM.read(serverAccessIPAddressStart+i);
		Serial.println(res3[i]);
	}

	int res4;
	EEPROM.get(serverAccessPortAddressStart, res4);
	Serial.println(res4);

}

void loop() {

}

void ConvertStringIpToIntArray(String inputIP,int outPutIP[]) {
	int startDotIndex = -1;
	int endDotIndex = inputIP.indexOf('.');
	int ipSize = 4;

	for (int i = 0; i < ipSize; i++) {

		String number = inputIP.substring(startDotIndex + 1, endDotIndex);
		outPutIP[i] = number.toInt();
		startDotIndex = endDotIndex;
		endDotIndex = inputIP.indexOf('.', endDotIndex + 1);
	}
}