EEPROM ESP32: how to assign stored data to integer variable

Dear all

I have managed to store (and read) defined strings to the ESP32 EEPROM. Upon start-up / reboot I would like to assign certain parts of the stored data to integer variables, like

Stored string in EEPROM: “IR1152a03 76 03 00 00”

int variables to be built upon boot / reading the eeprom:

four_digit_no_from_eeprom = 1152
two_digit_no_from_eeprom = 76

How can I form these integers from the EEPROM data above?

Many thanks

#include <Arduino.h>
#include <BluetoothSerial.h>
#include <EEPROM.h>

#define EEPROM_SIZE 64

int addr = 0;
char eeprom_train_id[64];

void setup()
{

  Serial.begin(115200);

  // EEPROM

  Serial.println("EEPROM starting now...");

  if (!EEPROM.begin(EEPROM_SIZE))
  {
    Serial.println("failed to init EEPROM");
    delay(5000);
  }

  // reading byte-by-byte from EEPROM
  for (int i = 0; i < EEPROM_SIZE; i++)
  {
    byte readValue = EEPROM.read(i);

    if (readValue == 0)
    {
      break;
    }

    char readValueChar = char(readValue);
    Serial.print(readValueChar);
    Serial.println("");
  }
  

    int four_digit_no_from_eeprom = ? ? ? ? ?;    //what to do to assign 1152 as integer from eeprom string)
    int two_digit_no_from_eeprom = ? ? ? ? ?;     //what to do to assign 76 as integer from eeprom string)
}

void loop()
{

  // Sample string to store in EEPROM
  char eeprom_last_train_id[64] = "IR1152a03 76 03 00 00";
  for (int i = 0; i < EEPROM_SIZE; i++)
  {
    EEPROM.write(addr, eeprom_last_train_id[i]);
    addr += 1;
  }
  EEPROM.commit();
}

Why are you saving the data as a string ?

It looks like you should consider putting your data in a struct and saving/loading that. Then it becomes easy to access the members of the struct

I have no idea what the data actually represents so I invented the names. Note the allowance for the terminating zero in the size of the strings

#include <EEPROM.h>

struct dataLayout
{
  char type[3];
  int number1;
  char id[4];
  byte number2;
  byte number3;
  byte number4;
  byte number5;
};

dataLayout last_train_id;
dataLayout anotherStruct;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  strcpy(last_train_id.type, "IR");  //populate the struct data
  last_train_id.number1 = 1152;
  strcpy(last_train_id.id, "a03");
  last_train_id.number2 = 76;
  last_train_id.number3 = 3;
  last_train_id.number4 = 0;
  last_train_id.number5 = 0;
  EEPROM.put(0, last_train_id); //put example data in EEPROM
  EEPROM.get(0, anotherStruct);  //get the data back to another struct to prove that it works
  Serial.println(anotherStruct.number1);
  Serial.println(anotherStruct.number2);
}

void loop()
{
}

Many thank UKHeliBob.

I think that is that path to follow. However on my Test sketch witch your code I only get two zeros as result:

PS I added the “EEPROM.commit();” command. As I understand that is needed for the ESP32 to write into the EEPROM.

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:8896
load:0x40080400,len:5828
entry 0x400806ac
0
0

#include <Arduino.h>
#include <BluetoothSerial.h>
#include <EEPROM.h>

BluetoothSerial Serial_BT;

#define EEPROM_SIZE 64

struct dataLayout
{
  char type[3];
  int number1;
  char id[4];
  byte number2;
  byte number3;
  byte number4;
  byte number5;
};

dataLayout last_train_id;
dataLayout anotherStruct;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  strcpy(last_train_id.type, "IR");  //populate the struct data
  last_train_id.number1 = 1152;
  strcpy(last_train_id.id, "a03");
  last_train_id.number2 = 76;
  last_train_id.number3 = 3;
  last_train_id.number4 = 0;
  last_train_id.number5 = 0;
  EEPROM.put(0, last_train_id); //put example data in EEPROM
  EEPROM.commit();
  EEPROM.get(0, anotherStruct);  //get the data back to another struct to prove that it works
  Serial.println(anotherStruct.number1);
  Serial.println(anotherStruct.number2);
}

void loop()
{
}

I wrote and tested the code on a Nano as that was what was on hand and plugged in at the time. I meant to mention that when I posted the example but it slipped my mind

I will dig out an ESP32 and give it a try

You forget the

EEPROM.begin(EEPROM_SIZE);

when you tried my example on the ESP32

Thanks a lot.
Just to give you a sound understanding and to be more precise on what I would like to achieve. Variables to be (stored and) retrieved on start-up in with the following format are my final goal to initialize my project on start-up:

Example stored in EEPROM: IR1152a03 76 04 12 99

variable1: 6 bytes //example variable1=IR1152 (char or string format as applicable)
variable2: 9 bytes //example variable2=IR1152a03 (char or string format as applicable)
variable3: 2 bytes //example variable3=76 (integer)
variable4: 2 bytes //example variable4=04 (integer)
variable5: 2 bytes //example variable5=12 (integer)
variable6: 2 bytes //example variable6=99 (integer)

On the ESP32, EEPROM has been depreciated. Use SPIFF's, which are still supported.

Idahowalker:
On the ESP32, EEPROM has been depreciated. Use SPIFF's, which are still supported.

And SPIFFS in its turn has been deprecated in favour of LittleFS !

On the ESP32, EEPROM has been depreciated. Use SPIFF’s, which are still supported.
And SPIFFS in its turn has been deprecated in favour of LittleFS !

Using the Arduino IDE and its support for EEPROM.h on the ESP32 in a separate area of memory, I do see the reason to complicate a beginners life withSPIFFS/LittleFS for small pieces of data storage.

@v_beach_1985

I’m not clear why you want the variables as you say you do, but here is UKHeliBob’s code modified per your requested format.

variable1: 6 bytes //example variable1=IR1152 (char or string format as applicable)
variable2: 9 bytes //example variable2=IR1152a03 (char or string format as applicable)
variable3: 2 bytes //example variable3=76 (integer)
variable4: 2 bytes //example variable4=04 (integer)
variable5: 2 bytes //example variable5=12 (integer)
variable6: 2 bytes //example variable6=99 (integer)

#include <Arduino.h>
#include <BluetoothSerial.h>
#include <EEPROM.h>

BluetoothSerial Serial_BT;

#define EEPROM_SIZE 64

struct dataLayout
{
  //char type[3];
  //int number1;
  //char id[4];
  //byte number2;
  //byte number3;
  //byte number4;
  //byte number5;
  char variable1[7]; //six chars plus null terminator
  char variable2[10];//nine chars plus null terminator
  int16_t variable3;//2 bytes
  int16_t variable4;
  int16_t variable5;
  int16_t variable6;

};

dataLayout last_train_id;
dataLayout anotherStruct;

void setup()
{
  EEPROM.begin(EEPROM_SIZE);
  Serial.begin(115200);
  Serial.println("starting");
  while (!Serial);
  strcpy(last_train_id.variable1, "IR1152");  //populate the struct data
  //last_train_id.number1 = 1152;
  strcpy(last_train_id.variable2, "IR1152a03");
  last_train_id.variable3 = 76;
  last_train_id.variable4 = 4;
  last_train_id.variable5 = 12;
  last_train_id.variable6 = 99;
  EEPROM.put(0, last_train_id); //put example data in EEPROM
  EEPROM.commit();
  EEPROM.get(0, anotherStruct);  //get the data back to another struct to prove that it works
  Serial.println(anotherStruct.variable1);
  Serial.println(anotherStruct.variable2);
  Serial.println(anotherStruct.variable3);
  Serial.println(anotherStruct.variable4);
  Serial.println(anotherStruct.variable5);
  Serial.println(anotherStruct.variable6);
}

void loop()
{
}

Nice thing about SPIFF's is they got an assignment of ram that the EEPROM library will not gain access too. Instead EEPROM will consume program space, whiles leaving a large piece of memory off limits to the project, that is used to store SPIFF's.

I do see the reason to complicate a beginners life withSPIFFS/LittleFS for small pieces of data storage.

I believe that something is missing from this sentence :slight_smile:

I believe that something is missing from this sentence :slight_smile:

oops.

It did not get by you.

I do not see the reason to complicate a beginners life withSPIFFS/LittleFS for small pieces of data storage.

I do not see the reason to complicate a beginners life withSPIFFS/LittleFS for small pieces of data storage.

Neither do I

EEPROM is simple to use, and the use of a struct to hold the data makes saving, loading and using the data a breeze

Thank you all very! much for your support, effort and time spending with me as a beginner. Although my very first ESP32 TFT project controlled by a Smartphone app works so far to my satisfaction, as you easily identified I am still really inexperienced and do not yet understand many concepts - like storing/reading data efficiently to the EEPROM where I am obviously struggling.
To sum it up, all I want to achieve is to have the data of my 6 variables ready again from the EEPROM when I start-up the ESP32. I now fully understand that the way I intend to do it is probably complicated and inefficient but I don't understand the procedure to do it any better.
I also understand that you - as experts - are not really satisfied the way I am doing it. So if any of you could show/explain me the way to store and read the data the proper way, I will be extremely grateful and will adapt my code accordingly. I acknowledge that I didn't really understand the EEPROM handling in general, even after reviewing several tutorials and examples. I also learned now that there are even newer libraries to do so.
Nevertheless, I have a starting point for time being with the code kindly provided by UKHeliBob and cattledog, although I understand that it is probably not the efficient way to do so. I am any time open to adapt the code.
Despite some setbacks like this, it’s a lot of fun and am really excited about the options of the Arduino/C++/ESP32 world and I am committed to go through a steep learning curve...

The path of the steep learning curve, ESP32, can be had by going through some of the ESP32's API, such as how to use SPIFF's. I've been able to use about 80% of the ESP32 in the Arduino IDE.

I now fully understand that the way I intend to do it is probably complicated and inefficient

You are making progress, learning something, and doing fine.

Storing and retrieving data in the eeprom space of flash memory using eeprom.h library with .put() and .get() with a data structure is not an issue.

With the ESP32 and eeprom.h the max number stored bytes is 512 so a little inefficiency in what you store is not important. More important is that the data you store and retrieve is in the form you want it to be in for easy use in your program.

Thank you all once again VERY MUCH.
It now works perfetely fine with my project! All required data is available on start-up.

I will look into SPIFF's with my progress.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.