ESP8266. How to read, write, erase the EEPROM. Calculate the space needed in bytes

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp

The ESP8266 has a reserved memory area to simulate the internal Arduino EEPROM. Initially, this area was present to mimic the operation of the Arduino and to make the migration of programs easier. It can be used to store certain parameters, such as an IP address or Wi-Fi parameters. The size can vary from 4 to 4096 bytes on an ESP8266.

 

The organization of the Flash memory of an ESP8266 module

Like a computer, the ESP8266 module has a small amount of user-usable RAM (80 Kb) and storage space consisting of a flash memory element (similar to USB keys) accessible via the SPI bus.

The size of flash memory varies from manufacturer to manufacturer but the standard is 4MB.

Access to the flash memory is fully managed by the ESP-IDF SDK from the manufacturer Espressif and is done via the SPI bus.

Flash memory is cut and each space is reserved for each function:

  • Sketch C++ code of the project
  • OTA update buffer zone used to download a new update of the sketch
  • File System (FS) is what interests us here
  • EEPROM a small memory area which simulates that of the Arduino. You can assign a size from 4 to 4096 bytes.
    WiFi config is a reserved area for storing connection parameters when developing directly in C ++ with the ESP-IDF SDK
|--------------|-------|---------------|--|--|--|--|--|
^              ^       ^               ^     ^
Sketch    OTA update   File system   EEPROM  WiFi config (SDK)

How to calculate the dimension in byte to reserve?

It’s very simple, just reserve one byte per character.

Let us take an example of a username and password to the WiFi network that we want to store in the EEPROM zone of the ESP8266.

  • The network identifier (SSID) which is called ssidwifi here is 8 characters long. In theory, therefore, 8 bytes should be reserved. For security, we will reserve 10. So we will use the “boxes” going from 0 to 9
  • We will assign the network password password. Ditto, in theory, 8 bytes should be reserved, which will be increased to 10 for safety. The network password will be stored from bytes 10 to 19.

Here is what it looks like in a more visual way.

-------------------------------------------------------
|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|...|511|
|s|s|i|d|w|i|f|i| | |p |a |s |s |w |o |r |d |  |...|   |
-------------------------------------------------------
^                   ^
index_ssid (0)      index_password (10)

An example of writing and reading to EEPROM

Here is a small example that records the network ID and password.

It is possible to clear the EEPROM at startup by passing the RESET_EEPROM variable to true.

#include <EEPROM.h> 

int addr_ssid = 0;         // ssid index
int addr_password = 20;    // password index
String ssid = "wifi_ssid"; // wifi ssid
String password = "wifi_password_demo"; // and password

// Set to true to reset eeprom before to write something
#define RESET_EEPROM false

void setup() {
  Serial.begin(115200);
  EEPROM.begin(512);
  Serial.println("");
  
  if ( RESET_EEPROM ) {
    for (int i = 0; i < 512; i++) {
      EEPROM.write(i, 0);
    }
    EEPROM.commit();
    delay(500);
  }
 
  Serial.println("");
  Serial.print("Write WiFi SSID at address "); Serial.println(addr_ssid);
  Serial.print("");
  for (int i = 0; i < ssid.length(); ++i)
  {
    EEPROM.write(addr_ssid + i, ssid[i]);
    Serial.print(ssid[i]); Serial.print("");
  }

  Serial.println("");
  Serial.print("Write WiFi Password at address "); Serial.println(addr_password);
  Serial.print("");
  for (int j = 0; j < password.length(); j++)
  {
    EEPROM.write(addr_password + j, password[j]);
    Serial.print(password[j]); Serial.print("");
  }

  Serial.println("");
  if (EEPROM.commit()) {
    Serial.println("Data successfully committed");
  } else {
    Serial.println("ERROR! Data commit failed");
  }

  Serial.println("");
  Serial.println("Check writing");
  String ssid;
  for (int k = addr_ssid; k < addr_ssid + 20; ++k)
  {
    ssid += char(EEPROM.read(k));
  }
  Serial.print("SSID: ");
  Serial.println(ssid);

  String password;
  for (int l = addr_password; l < addr_password + 20; ++l)
  {
    password += char(EEPROM.read(l));
  }
  Serial.print("PASSWORD: ");
  Serial.println(password);
}

void loop() {

}

How to write in EEPROM?

We declare the library which allows to manage the memory area

#include <EEPROM.h>

Variables contain the index and the content to be recorded, for example for the identifier of the WiFi network.

int addr_ssid = 0;         // ssid index
String ssid = "wifi_ssid"; // wifi ssid

Before being able to use the EEPROM, it must be initialized by assigning it a size in byte. The size can vary from 4 to 4096 bytes. Here, 512 bytes are allocated but it is arbitrary.

EEPROM.begin(512);

We browse the character string containing the network identifier and the password. Each character of the string is written with the EEPROM.write() command using a for loop.

Notice the shift that allows you to position yourself in the correct memory slot addr_password + j at each iteration of the for loop.

for (int j = 0; j < password.length(); j++) { 
  EEPROM.write(addr_password + j, password[j]);  
}

The write command does not write directly to EEPROM. It just prepares an array of bytes which must then be saved in the memory area using the commit() command.

EEPROM.commit()

How to read in EEPROM?

Each bytes is read successively in the EEPROM using the read(position) method. Each character (char) will be added successively to a string (String)

String password;

To recover the password, we will read from addr_password to addr_password + 20 since we have reserved 20 bytes for the password, which gives

for (int l = addr_password; l < addr_password + 20; ++l) { 
 password += char(EEPROM.read(l)); 
}

How to erase the EEPROM in case of problem?

Simply by writing 0 for each byte. The ESP8266 SDK comes with an example of which here is a simplified version.

#include <EEPROM.h>  

void setup() {
  EEPROM.begin(512);
  // write a 0 to all 512 bytes of the EEPROM
  for (int i = 0; i < 512; i++) {
    EEPROM.write(i, 0);
  }
  EEPROM.end();
}

void loop() {
}

Can special characters be stored in EEPROM?

Yes, a priori the majority of special characters can be recorded.

I have successfully tested the following special and accented characters (not forgetting the space)

& é ' ( § è ! ç à ) - @ # ^ ¨ % , ? ; . : / = + ! | _ -

Updates

11/09/2020 First publication of the article

Click to rate this post!
[Total: 0 Average: 0]

Are you having a problem with this topic?

Maybe someone has already found the solution, visit the forum before asking your question
Ask your question

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp

Did you like this project ? Don't miss any more projects by subscribing to our weekly newsletter!

We will be happy to hear your thoughts

      Leave a Reply

      DIY Projects