NVS not saving blob correctly for me
Posted: 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:
Here is the code I use to save the array 'schedules':
And here is the code I use to retrieve the saved 'schedules' array:
Can anyone see what I am doing wrong?
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];
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);
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);