Using NVS to save a configuration or settings
Posted: Tue Mar 28, 2023 12:52 am
Hi all,
I'm having some trouble implementing a way to save some settings that I want to be configurable.
These will rarely be changed (if ever), but having the option is very valuable.
It looks like blob storage is the most appropriate, but I haven't been able to find or adopt an example that works for my use-case.
I am trying to store the settings in a struct (is this the best method?) and saving the struct using blob storage, this part seems to work fine, but updating the table isn't occurring successfully.
The ability to save a configuration or settings to NVS seems like a common use-case, there must be examples out there that I haven't found yet? I require the settings to save across full power cycles.
Here is some code that I've attempted so far:
Save and load functions:
Code that gets initiated on a downlink (or any form of comms, could be serial)
The downlink receive code
The struct from main.h
This seems to update the var1/2 in the struct but doesn't appear to update the NVS, this is evident as the values are correctly updated when in normal operation (they receive the downlink and update accordingly), but then on full power reset they return to this values:
var1 = 1073478214
var2 = 1073478204
Which I believe are memory addresses? I just quickly tried a few things relating to this question with no change in outcome so I'll just post here for now.
Has anyone tried to save a configuration to blob NVS or similar? Am I taking the right approach?
Cheers,
Dylan
I'm having some trouble implementing a way to save some settings that I want to be configurable.
These will rarely be changed (if ever), but having the option is very valuable.
It looks like blob storage is the most appropriate, but I haven't been able to find or adopt an example that works for my use-case.
I am trying to store the settings in a struct (is this the best method?) and saving the struct using blob storage, this part seems to work fine, but updating the table isn't occurring successfully.
The ability to save a configuration or settings to NVS seems like a common use-case, there must be examples out there that I haven't found yet? I require the settings to save across full power cycles.
Here is some code that I've attempted so far:
Save and load functions:
Code: Select all
esp_err_t save_struct(Settings_t Settings)
{
nvs_handle_t my_handle;
esp_err_t err;
err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
if (err != ESP_OK) return err;
err = nvs_set_blob(my_handle, "Settings", &Settings, sizeof(Settings));
if (err != ESP_OK) return err;
err = nvs_commit(my_handle);
if (err != ESP_OK) return err;
nvs_close(my_handle);
return ESP_OK;
}
esp_err_t load_struct(Settings_t *Settings)
{
nvs_handle_t my_handle;
esp_err_t err;
err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
if (err != ESP_OK) return err;
size_t required_size = sizeof(Settings_t);
err = nvs_get_blob(my_handle, "Settings", Settings, &required_size);
if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) return err;
nvs_close(my_handle);
return ESP_OK;
}
Code: Select all
void save_settings(void)
{
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK( err );
printf("In Saving \n");
Settings_t Settings = {0};
err = load_struct(&Settings);
if (err == ESP_ERR_NVS_NOT_FOUND) {
Settings.var1= 15;
Settings.var2= 5;
} else if (err != ESP_OK) {
// Handle error
}
// vTaskDelay(pdMS_TO_TICKS(2000));
printf("var1 = %d\n", Settings.var1);
printf("var2 = %d\n", Settings.var2);
err = save_struct(Settings);
if (err != ESP_OK) {
// Handle error
}
}
Code: Select all
void messageReceived(const uint8_t* message, size_t length, ttn_port_t port)
{
printf("Message of %d bytes received on port %d:", length, port);
Settings.var1= message[1];
printf("var1%d\n", Settings.var1);
Settings.var2= message[2];
save_settings();
printf("After Saving \n");
}
Code: Select all
typedef struct {
int var1;
int var2;
} Settings_t;
extern Settings_t Settings;
var1 = 1073478214
var2 = 1073478204
Which I believe are memory addresses? I just quickly tried a few things relating to this question with no change in outcome so I'll just post here for now.
Has anyone tried to save a configuration to blob NVS or similar? Am I taking the right approach?
Cheers,
Dylan