# ESP8266 EEPROM WRITE / READ PROBLEM

**URL:** <https://forum.arduino.cc/t/esp8266-eeprom-write-read-problem/426686>\
**Category:** Programming\
**Created:** [December 23, 2016, 5:11pm UTC](https://forum.arduino.cc/t/esp8266-eeprom-write-read-problem/426686 "2016-12-23T17:11:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![stavbodik](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@stavbodik](https://forum.arduino.cc/u/stavbodik)\
**Post date:** [December 23, 2016, 5:11pm UTC](https://forum.arduino.cc/t/esp8266-eeprom-write-read-problem/426686/1 "2016-12-23T17:11:14Z")

</div>

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 -\>

```cpp
#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 :

```cpp
Start Write
Write Done
READ
A111111111111111111111111111111A

```

But Now when Upload this sketch :

```cpp
#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 ):

```cpp
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 ) ?

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [December 23, 2016, 5:18pm UTC](https://forum.arduino.cc/t/esp8266-eeprom-write-read-problem/426686/2 "2016-12-23T17:18:46Z")

</div>

> Any problem in my code please ?

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

---

<div class="post-metadata">

**Author:** ![stavbodik](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@stavbodik](https://forum.arduino.cc/u/stavbodik)\
**Post date:** [December 23, 2016, 5:21pm UTC](https://forum.arduino.cc/t/esp8266-eeprom-write-read-problem/426686/3 "2016-12-23T17:21:49Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [December 23, 2016, 5:25pm UTC](https://forum.arduino.cc/t/esp8266-eeprom-write-read-problem/426686/4 "2016-12-23T17:25:30Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![stavbodik](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@stavbodik](https://forum.arduino.cc/u/stavbodik)\
**Post date:** [December 23, 2016, 5:32pm UTC](https://forum.arduino.cc/t/esp8266-eeprom-write-read-problem/426686/5 "2016-12-23T17:32:15Z")

</div>

> 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 . 😕

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [December 23, 2016, 5:41pm UTC](https://forum.arduino.cc/t/esp8266-eeprom-write-read-problem/426686/6 "2016-12-23T17:41:17Z")

</div>

OK. Here is a simple example which should answer your questions. [EEPROM put and get methods - Everything ESP8266](http://www.esp8266.com/viewtopic.php?p=16581)

---

<div class="post-metadata">

**Author:** ![stavbodik](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@stavbodik](https://forum.arduino.cc/u/stavbodik)\
**Post date:** [December 23, 2016, 5:51pm UTC](https://forum.arduino.cc/t/esp8266-eeprom-write-read-problem/426686/7 "2016-12-23T17:51:36Z")

</div>

> 6v6gt:  
> OK. Here is a simple example which should answer your questions. [EEPROM put and get methods - Everything ESP8266](http://www.esp8266.com/viewtopic.php?p=16581)

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

```arduino
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 ?

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [December 23, 2016, 6:21pm UTC](https://forum.arduino.cc/t/esp8266-eeprom-write-read-problem/426686/8 "2016-12-23T18:21:49Z")

</div>

Here from [https://github.com/adafruit/ESP8266-Arduino](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:

```arduino
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;
}

```

---

<div class="post-metadata">

**Author:** ![stavbodik](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@stavbodik](https://forum.arduino.cc/u/stavbodik)\
**Post date:** [December 24, 2016, 5:32pm UTC](https://forum.arduino.cc/t/esp8266-eeprom-write-read-problem/426686/9 "2016-12-24T17:32:29Z")

</div>

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 :

```cpp
#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);
	}
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 10:14pm UTC](https://forum.arduino.cc/t/esp8266-eeprom-write-read-problem/426686/10 "2021-05-05T22:14:33Z")

</div>


