In this tutorial we will see writing data to EEPOM and reading from EEPROM. ESP8266 have 512 bytes of internal EEPROM, it is useful when you want to store some settings, such as IP address of server, WEP key, SSID of WiFi. We also discuss on writing string into EEPROM and reading it.
To write byte to EEPROM we need two commands
EEPROM.write(addr, data); EEPROM.commit();
To read single byte from EEPROM
Char Data; Data = EEPROM.read(addr);
We have only read and write bytes commands, for writing String, Integer and other data types we have to split data into bytes first and rejoin it while reading.
Write data to EEPROM
We write some characters and String to EEPROM, This program only writes data to EEPROM we read it using another program. Bytes ABC are stored at address 0x00,0x01,0x02 respectively and string is stored from 0x0F.
Remember that ESP requires EEPROM.commit(); command. Without this data will not be saved to EEPROM.
/* * EEPROM Write * * Stores values and string to EEPROM. * These values and string will stay in the EEPROM when the board is * turned off and may be retrieved later by another sketch. */ #include <EEPROM.h> // the current address in the EEPROM (i.e. which byte // we're going to write to next) int addr = 0; void setup() { EEPROM.begin(512); //Initialize EEPROM // write appropriate byte of the EEPROM. // these values will remain there when the board is // turned off. EEPROM.write(addr, 'A'); //Write character A addr++; //Increment address EEPROM.write(addr, 'B'); //Write character A addr++; //Increment address EEPROM.write(addr, 'C'); //Write character A //Write string to eeprom String www = "www.circuits4you.com"; for(int i=0;i<www.length();i++) //loop upto string lenght www.length() returns length of string { EEPROM.write(0x0F+i,www[i]); //Write one by one with starting address of 0x0F } EEPROM.commit(); //Store data to EEPROM } void loop() { //We dont have anything in loop as EEPROM writing is done only once delay(10); }
Read data from EEPROM
Now let’s read data from EEPROM and show it on serial monitor. You can combine these two programs as per your need.
String is array of characters.
/* * EEPROM Read * Circuits4you.com */ #include <EEPROM.h> // the current address in the EEPROM (i.e. which byte // we're going to read from) int addr = 0; void setup() { EEPROM.begin(512); //Initialize EEPROM Serial.begin(9600); //Serial communication to display data // read appropriate byte of the EEPROM. Serial.println(""); //Goto next line, as ESP sends some garbage when you reset it Serial.print(char(EEPROM.read(addr))); //Read from address 0x00 addr++; //Increment address Serial.print(char(EEPROM.read(addr))); //Read from address 0x01 addr++; //Increment address Serial.println(char(EEPROM.read(addr))); //Read from address 0x02 //Read string from eeprom String www; //Here we dont know how many bytes to read it is better practice to use some terminating character //Lets do it manually www.circuits4you.com total length is 20 characters for(int i=0;i<20;i++) { www = www + char(EEPROM.read(0x0F+i)); //Read one by one with starting address of 0x0F } Serial.print(www); //Print the text on serial monitor } void loop() { //We dont have anything in loop as EEPROM reading is done only once delay(10); }
Results
You will see what we have written to the EEPROM will appear on Serial terminal. ESP always sends some garbage to serial monitor when you reset it, skip first line.
In this example we have taken how to write String and read String from EEPROM, we need to use String type data many times in ESP8266.