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 =“example.com/”; // 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)

 EEPROM.begin(EEPROM_SIZE);
EEPROM.get(EVENTS_EEPROM_ADDR, events);
EEPROM.end();
 EEPROM.begin(EEPROM_SIZE);
EEPROM.put(EVENTS_EEPROM_ADDR, events);
EEPROM.end();

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

@OP

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:

#include<Wire.h>

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[] = "example.com/";    // 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()
{
  Serial.begin(115200);
  Wire.begin(4, 5); //SDA = GPIO-4/D2, SCL=GPIO-5
  //--------------------------------------------
  Wire.beginTransmission(0x52);
  Wire.write(highByte(addrEE));
  Wire.write(lowByte(addrEE));
  Wire.write(serverPort, sizeof(serverPort));
  byte busStatus = Wire.endTransmission();
  //Serial.println(busStatus);
  delay(5);

  Wire.beginTransmission(0x52);
  Wire.write(highByte(addrEE));
  Wire.write(lowByte(addrEE));
  Wire.endTransmission();

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

  for (int i = 0; i < 5; i++)
  {
    serverPortR[i] = Wire.read();
    Serial.print(serverPortR[i]);
  }
}

void loop()
{

  Serial.print("OK");
  delay(1000);

}

The Screenshot:
sm106.png

sm106.png

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.