I need to store a Structure inside the NVS of ESP32, is it possible with the current APIs available somehow ??
Actual Problem is: I need to store the Alarms inside the ESP-32, which will be of following structure :
typedef struct
{
int minutes;
int hours;
int date ;
}Alarm ;
I will be storing multiple alarms inside the NVS & then fetching them at the startup.
Storing a strucutre insde NVS
Re: Storing a strucutre insde NVS
Why do you *need* to store it as a struct? What happened when you tried that? What else have you tried?
I recommend you begin here: https://math.hawaii.edu/home/pdf/putnam ... olveIt.pdf
Seriously, Tom Meyers
I recommend you begin here: https://math.hawaii.edu/home/pdf/putnam ... olveIt.pdf
Seriously, Tom Meyers
IT Professional, Maker
Santiago, Dominican Republic
Santiago, Dominican Republic
Re: Storing a strucutre insde NVS
Make your struct into a 64bit value which is supported by nvs. Hours and minutes don't need to be 32bit values.
-
- Posts: 15
- Joined: Wed Jul 19, 2017 11:27 am
Re: Storing a strucutre insde NVS
Hi , Thanks for looking into my issue .
Are you suggesting me to convert the structure into byte & then saving that byte data into NVS?
At the Time of Retrieving, I cast that byte into the structure.
Like this : https://stackoverflow.com/questions/137 ... -to-struct
Are you suggesting me to convert the structure into byte & then saving that byte data into NVS?
At the Time of Retrieving, I cast that byte into the structure.
Like this : https://stackoverflow.com/questions/137 ... -to-struct
Re: Storing a strucutre insde NVS
You can also try to convert to JSON and store as string, and convert back from JSON upon retrieval.
Re: Storing a strucutre insde NVS
In the app I'm finishing up, I store a struct of about 1000 bytes in NVS. Just store it as a blob, and when you retrieve it, map it to a pointer to your struct.
Re: Storing a strucutre insde NVS
That means update any field of a struct will update the whole blob.
Just wondering if that is a recommend way to use NVS.
BTW, the document says "NVS works best for storing many small values, rather than a few large values of type ‘string’ and ‘blob’."
Re: Storing a strucutre insde NVS
Yeah, I read that too. My application rarely changes the values to the values in the struct (it's primarily used for startup configuration data), so I won't destroy the NVS by writing to it too frequently.
I wasn't trying to say it was the best way to go, merely that it was feasible. And in my app, the blob/struct is a private member of an object, so the setter routines include the write back to NVS. It's worked conveniently for me so far.
I wasn't trying to say it was the best way to go, merely that it was feasible. And in my app, the blob/struct is a private member of an object, so the setter routines include the write back to NVS. It's worked conveniently for me so far.
-
- Posts: 2
- Joined: Thu Feb 03, 2022 11:33 am
Re: Storing a strucutre insde NVS
You can see example how store struct at https://github.com/SergeyKupavtsev/nvs_component
Who is online
Users browsing this forum: axellin, Baidu [Spider] and 137 guests