I am using the <Preferences.h> library,
but I haven't figured out how it works yet.
I use this simple sketch:
/*
* 22 aprile 2025
* Writes and reads into the EEPROM of the ESP32
*/
#include <Preferences.h>
Preferences prefs;
int dataStore[8] = {123, 456, 789, 444, 15, 6, 70, 800};
int dataRetrieve[8];
void setup() {
Serial.begin(115200);
prefs.begin("MyArray"); // namespace
prefs.putBytes("MyArray", (byte*)(dataStore), sizeof(dataStore));
}
void loop() {
for (int i = 0; i <= 7; i++) {
prefs.getBytes("MyArray", &dataRetrieve, sizeof(dataRetrieve));
Serial.println(dataRetrieve[i]);
delay(10);
}
while(1);
}
Once the sketch is installed on the ESP32, it does not show me the data saved in the EEPROM on the Serial Monitor.
To display them after installation, I have to press the reset key on the ESP32 and then I see the data saved in the EEPROM on the Serial Monitor.
Where am I going wrong?
Here is a complete tuto on how to use this library.
Also,
The for loop should be used only to print the element. You don't need to request the bytes several times as long as you ask to get all of them once (thanks to the argument 'sizeof')
I viewed both the “Random Nerd Tutorial” examples and the “docs. espressi.”
But they did not clear my doubts.
However, I solved the problem of the sketch I put up here by removing in the void loop() the “while 1” and added a delay(5000), to see what I was reading in the EEPROM of the ESP32.
I start from this sketch to create one where I first save data read from a sensor and another sketch where at a later time I can read it.
Thank you anyway for the advice you have given me.