NVS not saving blob correctly for me

clarkster
Posts: 47
Joined: Sat Sep 23, 2017 12:36 pm

NVS not saving blob correctly for me

Postby clarkster » Thu Oct 05, 2017 12:59 am

I'm using NVS to save an array of structures. My code is based on the ESP32 example project that saves an int and a blob. The code works and I get no errors but when I retrieve the data it does not match the data I saved. I have even tried to save an empty array (all values were zero) but when I do this almost every byte I read from NVS contained some number besides zero.

The data I am trying to save is as follows:

Code: Select all

#define NUMBER_SCHEDULES 50
typedef struct {
	uint8_t solenoid;		// 1 to 4
	uint8_t dayWeek;			// bit 0 - Sunday, bit 1 - Monday, etc.
	uint8_t hour;			// 0 to 23
	uint8_t minute;			// 0 to 59
	uint8_t duration;		// in minutes
} timer_schedule;
timer_schedule schedules[NUMBER_SCHEDULES]; 
Here is the code I use to save the array 'schedules':

Code: Select all

        nvs_handle my_handle;
	esp_err_t err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
	if (err != ESP_OK) return err;
        size_t scheduleSize = sizeof(schedules);
	err = nvs_set_blob(my_handle, "schedule", &schedules, scheduleSize);
	if (err != ESP_OK) return err;
	err = nvs_commit(my_handle);
	if (err != ESP_OK) return err;
	nvs_close(my_handle);
And here is the code I use to retrieve the saved 'schedules' array:

Code: Select all

 nvs_handle my_handle;
	esp_err_t err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
	if (err != ESP_OK) return err;
	size_t required_size = 0;  
	err = nvs_get_blob(my_handle, "schedule", NULL, &required_size);
	if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) return err;
	if (required_size == 0) {
		printf("Schedule not saved yet!\n");
	} else {
		size_t scheduleSize = sizeof(schedules);
		err = nvs_get_blob(my_handle, "schedule", &schedules, &scheduleSize);
		if (err != ESP_OK) return err;
	}
       // *******  print out 'schedules' array here and find garbage in it  ********
	nvs_close(my_handle);
Can anyone see what I am doing wrong?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: NVS not saving blob correctly for me

Postby kolban » Thu Oct 05, 2017 1:19 am

Could it be this line?

Code: Select all

err = nvs_set_blob(my_handle, "schedule", &schedules, scheduleSize);
My coding is not great but if you defined:

Code: Select all

timer_schedule schedules[NUMBER_SCHEDULES];
Then schedules is already of type pointer.

Maybe you could test with:

Code: Select all

err = nvs_set_blob(my_handle, "schedule", schedules, scheduleSize);
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

clarkster
Posts: 47
Joined: Sat Sep 23, 2017 12:36 pm

Re: NVS not saving blob correctly for me

Postby clarkster » Thu Oct 05, 2017 2:02 am

Thank you, Neil, for your help. That was the cause of my problem. I sure need to read more about pointers. I sorta understand them, but obviously not well enough.

I appreciate your willingness to help us beginners. I also appreciate your book. I use it every day [because I'm obsessed with the ESP32!].

Who is online

Users browsing this forum: No registered users and 118 guests