Storing a strucutre insde NVS

kartikkman
Posts: 15
Joined: Wed Jul 19, 2017 11:27 am

Storing a strucutre insde NVS

Postby kartikkman » Sat Nov 10, 2018 11:48 am

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.

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: Storing a strucutre insde NVS

Postby tommeyers » Sat Nov 10, 2018 2:54 pm

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
IT Professional, Maker
Santiago, Dominican Republic

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Storing a strucutre insde NVS

Postby WiFive » Sat Nov 10, 2018 4:01 pm

Make your struct into a 64bit value which is supported by nvs. Hours and minutes don't need to be 32bit values.

kartikkman
Posts: 15
Joined: Wed Jul 19, 2017 11:27 am

Re: Storing a strucutre insde NVS

Postby kartikkman » Sat Nov 10, 2018 4:35 pm

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

0xffff
Posts: 41
Joined: Tue Jun 19, 2018 1:53 am

Re: Storing a strucutre insde NVS

Postby 0xffff » Wed Nov 14, 2018 4:34 pm

You can also try to convert to JSON and store as string, and convert back from JSON upon retrieval.

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: Storing a strucutre insde NVS

Postby mzimmers » 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.

axellin
Posts: 197
Joined: Mon Sep 17, 2018 9:09 am

Re: Storing a strucutre insde NVS

Postby axellin » Thu Nov 15, 2018 3:51 am

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’."

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: Storing a strucutre insde NVS

Postby mzimmers » Thu Nov 15, 2018 3:16 pm

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.

KupavtsevSergey
Posts: 2
Joined: Thu Feb 03, 2022 11:33 am

Re: Storing a strucutre insde NVS

Postby KupavtsevSergey » Thu Feb 03, 2022 11:35 am

You can see example how store struct at https://github.com/SergeyKupavtsev/nvs_component

Who is online

Users browsing this forum: No registered users and 113 guests