Go Down

Topic: Storing Struct in EEPROM Arduino ESP8266 (Read 426 times) previous topic - next topic

cjohnweb

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.

Juraj

#1
May 01, 2019, 11:25 am Last Edit: May 01, 2019, 11:27 am by Juraj
I have this for read at startup and write not too often
(events is an array of structs)

Code: [Select]
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(EVENTS_EEPROM_ADDR, events);
EEPROM.end();


Code: [Select]
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(EVENTS_EEPROM_ADDR, events);
EEPROM.end();


reference: https://arduino-esp8266.readthedocs.io/en/latest/libraries.html#eeprom

GolamMostafa

#2
May 01, 2019, 06:19 pm Last Edit: May 02, 2019, 07:31 am by GolamMostafa
@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:
Code: [Select]
#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:

marco_c

Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com

GolamMostafa

#4
May 02, 2019, 07:20 am Last Edit: May 02, 2019, 07:30 am by GolamMostafa
This link provides an example for storing data into the internal EEPROM Space of the ESP8266/NodeMCU.

Go Up