Page 1 of 1
Storing a strucutre insde NVS
Posted: Sat Nov 10, 2018 11:48 am
by kartikkman
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.
Re: Storing a strucutre insde NVS
Posted: Sat Nov 10, 2018 2:54 pm
by tommeyers
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
Re: Storing a strucutre insde NVS
Posted: Sat Nov 10, 2018 4:01 pm
by WiFive
Make your struct into a 64bit value which is supported by nvs. Hours and minutes don't need to be 32bit values.
Re: Storing a strucutre insde NVS
Posted: Sat Nov 10, 2018 4:35 pm
by kartikkman
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
Re: Storing a strucutre insde NVS
Posted: Wed Nov 14, 2018 4:34 pm
by 0xffff
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
Posted: Wed Nov 14, 2018 8:47 pm
by mzimmers
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
Posted: Thu Nov 15, 2018 3:51 am
by axellin
mzimmers wrote: ↑Wed Nov 14, 2018 8:47 pm
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.
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
Posted: Thu Nov 15, 2018 3:16 pm
by mzimmers
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.
Re: Storing a strucutre insde NVS
Posted: Thu Feb 03, 2022 11:35 am
by KupavtsevSergey