Hi,
so I am trying to determine how I will proceed to start saving data into eeprom/flash in a ESP32. I tried using the Arduino EEPROM.h library but it didn't work.
I was thinking about using external eeprom i2c chips, but I plan on saving data into structs, so I'd have to serialize my data into bytes before sending the via i2c wire interface... Seems like I am losing compared to the EEPROM library that could save any custom type with a simple put funciton.
So, I have read about the preferences.h library, but the tutorials are a bit limited. In my project, I plan on saving a scalable amount of data, for instance the user could save test1, and later on test2, etc.
The preferences.h method seems clunky in that regards, as it does not seem to let me create keys in a scalable manner, I would have to create string names from a counter to generate keys named testX, x being the counter's value...
My first question is: is there a more simple way to scale my preferences.h keys? How are we supposed to proceed in that case? I wouldn't like to have to create like test1 to test5 static keys and store data in them.
And I am also worried about the fact that keys cannot be erased. We need to use this code to clean it, is it possible to trigger this code within a program? so that I can resume saving different keys and what not when the memory is full
Hi
I still have little experience with "preferences.h" and I can't help you on this item.
But you said that with the EEPROM library it didn't work.
Can you better explain this "didn't work."?
I am using preferences.h on ESP32 for a while and the results are ok - at least for my applications.
It is true that preferences uses strings to identify namespaces and names for data you like to store there. To a way this is like the good old Windows ini-files ...
There is also a function to remove keys from the opened namespace
preferences.remove(key);
However there seems to be no function to completely remove a namespace, so that the sketch you posted above is recommended to clear the whole non-volatile storage.
For me it works fine. I can store non-volatile data, retrieve them after reboot, read and change them online via Serial (or if Serial is redirected also via TCP wireless from any computer in my local network).
It might be helpful if you could elaborate a little bit more about what you want to achieve ...
Preferences.h is definitely the preferred way and it will use behind the scenes wear leveling and memory management.
The preferences.h method seems clunky in that regards, as it does not seem to let me create keys in a scalable manner, I would have to create string names from a counter to generate keys named testX, x being the counter's value...
My first question is: is there a more simple way to scale my preferences.h keys? How are we supposed to proceed in that case? I wouldn't like to have to create like test1 to test5 static keys and store data in them.
Bytes can be put/get in namespace directly see prefs2struct example. You do not need a specific key name.
Here's a simple example
#include <Preferences.h>
Preferences prefs;
int dataStore[3] = {12345, 45689, 78901};
int dataRetrieve[3];
void setup() {
Serial.begin(115200);
prefs.begin("IntegerArray"); //namespace
//bytes can be put/get in namespace directly see prefs2struct example
//prefs.putBytes("IntegerArray", (byte*)(&dataStore), sizeof(dataStore));
//SavedIntegers is Key
prefs.putBytes("SavedIntegers", (byte*)(&dataStore), sizeof(dataStore));
prefs.getBytes("SavedIntegers", &dataRetrieve, sizeof(dataRetrieve));
Serial.println(dataRetrieve[0]);
Serial.println(dataRetrieve[1]);
Serial.println(dataRetrieve[2]);
}
void loop() {}
If you prefer to manage a character string key name, then appending a number does not seem like a major issue in any case. It is a common occurrence in incrementing SD card file name.
I'm not clear about deleting data, and freeing memory, but I'm certain you will find an example somewhere.