Storing Struct in EEPROM Arduino ESP8266

I’ve been researching and can’t find an answer to this question that I understand: How can I write and read a struct (or other) to EEPROM with Arduino ESP8266?

I’m hoping there might be a library for this that is relatively easy to use. In my case, I have so many variables I need to store that I’m attempting to simplify the EEPROM read/write code by somehow grouping all the data into a single variable. I know Python, PHP, JavaScript et al but I don’t really know C. Despite this I’ve been able to complete nearly everything except this problem of persisting data through a power outage.

I’ve seen so many examples, I can’t get any of them to work. Did I even do it correctly? Here is my current “default” struct:

typedef struct {
char* APMode = “0”; // OPMode = 0, APMode = 1
char* id = “00000000”; // Device ID
char* businessName = “Joey Salads”; // Business Name
char* businessPhone = “(123) 123-4567”; // Support Phone Number
char* network = “12345678901234567890”; // OPMode SSID to connect to
char* password =“12345678901234567890”; // OPMode SSID Password
char* apSSID =“12345678901234567890”; // APMode SSID
char* APpass =“12345678901234567890”; // APMode SSID Password
char* frequency =“12000”; // How often to send data in ms
char* alarmThresh =“123”; // Alarm Threshold
char* serverProtocol =“http://”; // Server Protocol, HTTP or HTTPS
char* serverURL =“”; // Server URL
char* serverURI =“api/alarmMonitor.php”; // URL of data reporting
char* serverPort =“65535”; // Server Port: HTTP = 80, HTTPS = 443
} ltd attribute ((packed));

When the ESP8266 boots up I want to read this data out into a variable, be able to change this data during run time and at some points save or update the EEPROM with this data, which will be read back out into a variable upon next boot, etc.

I am open to using any data type that would support my desired result, json, etc.

I have this for read at startup and write not too often
(events is an array of structs)


reference: Libraries — ESP8266 Arduino Core 3.0.2-1-g929f0fb6 documentation


Here is a solution for you using NodeMCU and exteranl AT24C512 EEPROM for storing data.

I have tested by writing one array (serverPort) and then reading it back. Looks all right!

The Codes:


char APMode[] = "0";                          // OPMode = 0, APMode = 1
char id[] = "00000000";                    // Device ID
char businessName[] = "Joey Salads";           // Business Name
char businessPhone[] = "(123) 123-4567";    // Support Phone Number
char network[] = "12345678901234567890";   // OPMode SSID to connect to
char password[] = "12345678901234567890";  // OPMode SSID Password
char apSSID[] = "12345678901234567890";    // APMode SSID
char APpass[] = "12345678901234567890";    // APMode SSID Password
char frequency[] = "12000";                // How often to send data in ms
char alarmThresh[] = "123";                // Alarm Threshold
char serverProtocol[] = "http://";         // Server Protocol, HTTP or HTTPS
char serverURL[] = "";    // Server URL
char serverURI[] = "api/alarmMonitor.php"; // URL of data reporting
char serverPort[] = "65535";               // Server Port: HTTP = 80, HTTPS = 443
char serverPortR[10];
int addrEE = 0x0100;

void setup()
  Wire.begin(4, 5); //SDA = GPIO-4/D2, SCL=GPIO-5
  Wire.write(serverPort, sizeof(serverPort));
  byte busStatus = Wire.endTransmission();


  Wire.requestFrom(0x52, 5);
  // byte x =;
  // Serial.print(x, HEX);

  for (int i = 0; i < 5; i++)
    serverPortR[i] =;

void loop()



The Screenshot:


This repeats the info you have here, but may be interesting anyway Persisting Application Parameters in EEPROM – Arduino++

1 Like

This link provides an example for storing data into the internal EEPROM Space of the ESP8266/NodeMCU.