ESP32 and ArduinoIDE: cannot store and read values using EEPROM.h

Hi Guys,

I've been trying the last couple of hours to get my code running with saving and reading EEPROM registers using Arduino IDE and the EEPROM.h library:

I've followed a few blog entries about using ESP32 with EEPROM.h:

https://www.electronics-lab.com/project ... a-storage/

I would like to go for data types larger than one byte, which is why I will use EEPROM.put and EEPROM.get instead of .write and .read in the following code example.

Please have a look at my minimum code example:

// ******Required for Wakeup***************
#include "driver/rtc_io.h"
#define uS_TO_S_FACTOR 1000000ULL  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  10

#include <EEPROM.h>
#define EEPROM_SIZE 100
int variable1 = 6;
int variable2 = 6;
int variable3 = 6;
int variable4 = 6;

void setup() {
Serial.println("Setup started: ");
Serial.begin(9600);
EEPROM.begin(EEPROM_SIZE);
delay(5000);
variable1 = EEPROM.get(0,variable1);
variable2 = EEPROM.get(1,variable2);
variable3 = EEPROM.get(2,variable3);
variable4 = EEPROM.get(3,variable4);
Serial.print("Variables in setup mode: "); Serial.print(variable1); Serial.print(variable2); Serial.print(variable3); Serial.println(variable4);
}

void loop() {
Serial.println("Loop started");
variable1 = 1;
variable2 = 2;
variable3 = 3;
variable4 = 4;
delay(5000);
EEPROM.put(0,variable1);
EEPROM.commit();
EEPROM.put(1,variable2);
EEPROM.commit();
EEPROM.put(2,variable3);
EEPROM.commit();
EEPROM.put(3,variable4);
EEPROM.commit();
variable1 = EEPROM.get(0,variable1);
variable2 = EEPROM.get(1,variable2);
variable3 = EEPROM.get(2,variable3);
variable4 = EEPROM.get(3,variable4);
Serial.print("Variables in loop mode: "); Serial.print(variable1); Serial.print(variable2); Serial.print(variable3); Serial.println(variable4);
Serial.println("now going to sleep");
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}

Which shows the following output:

23:00:37.159 -> Loop started
23:00:42.160 -> Variables in loop mode: 6730598526291410274
23:00:42.195 -> now going to sleep
23:00:52.101 -> *!⸮zY⸮b⸮⸮o⸮De⸮S⸮!⸮Variables in setup mode: 6730598526291410274
23:00:57.260 -> Loop started
23:01:02.250 -> Variables in loop mode: 6730598526291410274
23:01:02.283 -> now going to sleep

So basically the values are nowhere stored, not even in the loop.
Thanks for your help!

I've used following board:https://www.amazon.de/AZDelivery-NodeMCU-Development-Nachfolgermodell-ESP8266/dp/B074RGW2VQ/ref=sxts_sxwds-bia-wc-p13n1_0?__mk_de_DE=ÅMÅŽÕÑ&cv_ct_cx=esp32&dchild=1&keywords=esp32&pd_rd_i=B074RGW2VQ&pd_rd_r=e91ddea5-c56e-4b2f-9f2a-7f71ff63769d&pd_rd_w=99wBH&pd_rd_wg=EPNMu&pf_rd_p=8ee420ec-517c-4ea8-924d-a2dbf8ac371a&pf_rd_r=AKWC9JNY0BXX8SS31GE5&psc=1&qid=1604873557&sr=1-1-79e1db8b-ac0e-4e53-86a0-e4b4f9bb89cd

Hi,

im not a master but i have been trying the last few days to store data on the ESP32.
Im just pasting what Idahowalker wrote to me:
"The ESP32 does not have an EEPROM. The EEPROM library for the ESP32 has been depreciated. "
and later UKHeliBob suggested LittleFS.
That was my post

LittleFS was difficult to use for me in the beginning due to lack of knowledge on my side for char arrays and pointers etc.

I did several posts last few days:
Parse using LittleFS

You might find that heplful.
In any case people here will help you a lot, maybe i could also (ok, dont laugh lol)

int variable1 = 6;
int variable2 = 6;
int variable3 = 6;
int variable4 = 6;
  EEPROM.put(0, variable1);
  EEPROM.commit();
  EEPROM.put(1, variable2);
  EEPROM.commit();
  EEPROM.put(2, variable3);
  EEPROM.commit();
  EEPROM.put(3, variable4);
  EEPROM.commit();

Each int takes 2 bytes of storage but you have saved them only one byte apart

Can you see the problem ?

  variable1 = EEPROM.get(0, variable1);

And that is not the normal syntax for EEPROM.get() either

Take a closer look at https://www.arduino.cc/en/Reference/EEPROMGet

Each int takes 2 bytes of storage but you have saved them only one byte apart

On the ESP32, a variable declared as "int" will be 4 bytes.

You can declare as int16_t if you want to work with 2 byte values.

1 Like

On the ESP32, a variable declared as "int" will be 4 bytes.

You are right, but of course, that only makes the mistake made by the OP worse