how do I save variables into the nvs?

HyperUniverse
Posts: 33
Joined: Fri Oct 29, 2021 11:20 pm

how do I save variables into the nvs?

Postby HyperUniverse » Wed Dec 08, 2021 11:37 pm

Hi,

How do I save a variable in the nvs?
I'm looking at the sample nvs_value_example_main, and I can't understand a thing!!!

Say I've got 5 different variables that I want to save into the nvs, and retrieve them whenever I want.
variable_1
variable_2
variable_3
variable_4
variable_5
Can someone give me an example on how to do it?

In the example supplied there is just one single int32_t restart_counter
But I can't understand the address where is being written, and where to read it from whenever you want.

Or is it that you don't assign them an address, but the operating system is looking just for the name of the variable?

Can someone illuminate me please, I'm in total darkness.

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: how do I save variables into the nvs?

Postby ESP_Sprite » Thu Dec 09, 2021 2:04 am

NVS variables aren't stored by address, they're stored by key, where a 'key' is a short string. For instance, in the example:

Code: Select all

nvs_get_i32(my_handle, "restart_counter", &restart_counter);
the 2nd argument is the key. The key and the variable are called the same, which may be confusing, so let me change it a bit:

Code: Select all

int32_t myvar;
nvs_get_i32(my_handle, "restart_counter", &myvar);
printf("Restart counter is %d\n", myvar);
You're effectively saying "Hey nvs, fetch the value I stored under "restart_counter" and dump it into the 'myvar' variable".

HyperUniverse
Posts: 33
Joined: Fri Oct 29, 2021 11:20 pm

Re: how do I save variables into the nvs?

Postby HyperUniverse » Thu Dec 09, 2021 11:38 am

Thanks ESP_Sprite

Very good explanation, and easy to understand.

When running the supplied example in nvs_value_example_main everything works perfectly.
But obviously, nobody is using reading and writing to-from NVS in one single go, as in the example supplied.
Everybody writes now, and reads it back much later.

So I have tried to split it in two separate parts:
one Reading
and one writing.
And now writing is not working.
Now I get FAILED when trying to write into the NVS.
Why?

variables defined globally:

Code: Select all

int32_t user_msg1_length = 0; // value will default to 0, if not set yet in NVS
esp_err_t err ;
reading from NVS:

Code: Select all

    // Initialize NVS
    err = nvs_flash_init();
    if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        // NVS partition was truncated and needs to be erased
        // Retry nvs_flash_init
        ESP_ERROR_CHECK(nvs_flash_erase());
        err = nvs_flash_init();
    }
    ESP_ERROR_CHECK( err );
    /*****************************************/


    // Open
    printf("\n");
    printf("Opening Non-Volatile Storage (NVS) handle to Read only... ");
    nvs_handle_t my_handle;
    err = nvs_open("storage", NVS_READONLY, &my_handle);
    if (err != ESP_OK) {
        printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
    } else {
        printf("Done\n");



    /* Read from NVS:     */
    printf("Reading length of first User Message from NVS ... ");

    err = nvs_get_i32(my_handle, "user_msg1_length", &user_msg1_length);
    switch (err) {
        case ESP_OK:
            printf("Done\n");
            printf("First User Message length = %d\n", user_msg1_length);
            break;
        case ESP_ERR_NVS_NOT_FOUND:
            printf("The value is not initialized yet!\n");
            break;
        default :
            printf("Error (%s) reading!\n", esp_err_to_name(err));
    }
    // Close
    nvs_close(my_handle);
    }
Writing to the NVS:

Code: Select all

   // Open
   printf("\n");
   printf("Opening Non-Volatile Storage (NVS) handle to Write ... ");
   nvs_handle_t my_handle;
   err = nvs_open("storage", NVS_READWRITE, &my_handle);
   if (err != ESP_OK) {
       printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
   } else {
       printf("Done\n");
   // Write value into the NVS
   printf("Updating the length of first User Message in NVS ... ");

   user_msg1_length = flash_wr_size;
   err = nvs_set_i32(my_handle, "user_msg1_length", user_msg1_length);
   printf((err != ESP_OK) ? "Failed!\n" : "Done\n");

   // Commit written value.
   // After setting any values, nvs_commit() must be called to ensure changes are written
   // to flash storage. Implementations may write to storage at other times,
   // but this is not guaranteed.
   printf("Committing updates in NVS ... ");
   err = nvs_commit(my_handle);
   printf((err != ESP_OK) ? "Failed!\n" : "Done\n");

   // Close
   nvs_close(my_handle);
   }

my flash_wr_size is 327680 in this case

what is "storage" in:

Code: Select all

err = nvs_open("storage", NVS_READWRITE, &my_handle);

markkuk
Posts: 38
Joined: Wed Mar 27, 2019 11:50 am

Re: how do I save variables into the nvs?

Postby markkuk » Thu Dec 09, 2021 12:50 pm

The maximum length of a NVS key is 15 characters: https://docs.espressif.com/projects/esp ... and-values, the key "user_msg1_length" you used is 16 characters.

HyperUniverse
Posts: 33
Joined: Fri Oct 29, 2021 11:20 pm

Re: how do I save variables into the nvs?

Postby HyperUniverse » Thu Dec 09, 2021 2:03 pm

Thanks markkuk
works perfect now.

Who is online

Users browsing this forum: No registered users and 341 guests