Store data to Arduino or ESP8266 Flash Memory using EEPRM

I created a library that will allow you to save string values to your Arduino or ESP8266  memory.
You can download it from here EEPRM Save Data to Arduino or ESP Memory Please read the readme file.
The importance is not covered here.

But! Before we go on about the library.
As mentioned in the heading both Arduino and ESP8266 support the ability to save values permanently to memory using the built-in EEPROM.h library. The trick is that you have to write a number value to memory. So If you would like to write a Alphanumeric value you have to write the ASCII value of each letter or number to memory and then convert it back to the ASCII code after reading it again.

If you are un-familiar with ASCII and all that don’t stress! It’s less complicated than it looks. See below example.

Arduino Example (Write the letter A to memory)

The value written to memory will be readable even after power cycling or resetting the micro controller.

#include <EEPROM.h>;
int MyVal;
void setup() {
Serial.begin(115200);
}

void loop() {
//USAGE: EEPROM.write(addr, val);
//USAGE: EEPROM.read(addr);
//The ASCII code for the captial letter A = 65
//Let's write it.
EEPROM.write(1, 65); //Write the value 65 to memory address 1
MyVal = EEPROM.read(1); //Read memory address 1
Serial.println(MyVal);
}

ESP8266 Example (This time the code looks slightly different)

Writing to the ESP8266 memory is slightly different. You  have to specify the memory size and after writing to memory you have to commit the changes. Otherwise changes will be discarded when you reboot or power cycle.

#include <EEPROM.h>;
int MyVal;
void setup() {
Serial.begin(115200);
EEPROM.begin(512);

}

void loop() {
//USAGE: EEPROM.write(addr, val);
//USAGE: EEPROM.read(addr);
//The ASCII code for the captial letter A = 65

//Let's write it.
EEPROM.write(1, 65); //Write the value 65 to memory address 1
EEPROM.commit();
MyVal = EEPROM.read(1); //Read memory address 1
Serial.println(MyVal);
}

Using EEPRM-Libary to manage your memory storage.
Now back to the library that I created called EEPRM from now on. You can imagine if you wanted to write the string “Hello World” to memory you would have to loop through each character including the space, convert from ASCII character to ASCII value. And then repeat the whole rigmarole after reading. This can is quite code and chore intensive. EEPRM takes all of that away. The concept is simple. See it as writing to fields or excel cells if you will.

Example: USAGE: Eeprm.writedata(Field_Number,”WHATEVER TEXT YOU WANT TO WRITE”);
Write Data:
Eeprm.writedata(1,”Hello World”);
Eeprm.writedata(2,”Hello Planet”);

Read Data to string
Dataread=Eeprm.readdata(1);

Complete Example: (You have to call Eeprm Eeprm(0); before reading/writing

#include <EEPROM.h>;
#include <eeprm.h>;

String Dataread;

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

//Write to memory
Eeprm Eeprm(0);
Eeprm.writedata(1,"Martin");
Eeprm.writedata(2,"Viljoen");

}


void loop()
{
//Read from field 1 in memory
Eeprm Eeprm(0);
Dataread=Eeprm.readdata(1);
Serial.println(Dataread);
//Read from field 2 in memory
Dataread=Eeprm.readdata(2);
Serial.println(Dataread);
}

An extra Note!
Not to freak you out but I can also read and write to your memory. 🙂
You will want to use this to read & write your wireless SSID and password of your ESP8266 to memory in order to make things a little more dynamic ? Told you so.

To save you some trouble after reading the SSID and password and passing them to the ESP Wifi.begin routine you need to convert the data to a char* because the ESP Wifi.begin routine does not accept a string. You do it like this.

//Read SSID & Password
String BuildAPSSID=Eeprm.readdata(1);
String BuildAPPassword=Eeprm.readdata(2);
WiFi.begin(BuildAPSSID.c_str(),BuildAPPassword.c_str());

Let me know what you think down below!
Cheers!

You May Also Like

About the Author: Martin Viljoen

Leave a Reply

Your email address will not be published. Required fields are marked *