ESP32 nvs_get_blob() help

blavoie
Posts: 16
Joined: Sat Jan 21, 2017 6:41 pm

ESP32 nvs_get_blob() help

Postby blavoie » Mon Mar 27, 2017 11:43 am

I'm starting to work with the NVS on my product and I'm getting an error with trying to retrieve values using nvs_get_blob(), error code being returned is 0x102 ESP_ERR_INVALID_ARG. I'm just starting to use these API's so I admit I'm not familiar with using them yet, but I'm following an example that I found in the examples directory in esp-idf.

I plan to use the NVS for holding some configuration data for the application to use for initialization.

I've started with a couple of simple parameters for the softAP wifi interface. I've created a structure that holds ssid and password.
//header file
typedef struct storedConfig
{
char ssidVal[20];
char pass[20];
}storedConfig_t;
//c file
storedConfig_t cnf={
.ssidVal="ESP32",
.pass="password",
};
storedConfig_t *pcnf;

My API usage looks like the following;
// Step 1. Open the file
err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
if (err != ESP_OK) return err;
//step 2. check to see if the file has been initialized with the SSID key has been written, if not write and commit
size_t required_size = 0; // value will default to 0, if not set yet in NVS
err = nvs_get_blob(my_handle, "ssid", NULL, &required_size);
if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND)
{
printf("opening first time file write init\r\n");
required_size = strlen(cnf.ssidVal);
err = nvs_set_blob(my_handle, "ssid", cnf.ssidVal, required_size);
required_size = strlen(cnf.pass);
err = nvs_set_blob(my_handle, "password", cnf.pass, required_size);
// Commit
err = nvs_commit(my_handle);
if (err != ESP_OK) return err;
}
//step 3. read the previously configured values assuming the file has been written
err = nvs_get_blob(my_handle, "ssid", cnf.ssidVal, &required_size);
printf("get_configuration_values() ssid:%s err:%d reqsize:%d\r\n",cnf.ssidVal,err,(int)required_size);
err = nvs_get_blob(my_handle, "pass", cnf.pass, &required_size);
printf("get_configuration_values() pass:%s err:%d reqsize:%d\r\n",cnf.pass,err,(int)required_size);
//if (err != ESP_OK) return err;

Another question about clearing the entire space to start over, while in development mode
//use the erase all and then close? Doesn't seem to be working.
nvs_erase_all(my_handle);
// Close
nvs_close(my_handle);

I'm hoping someone more familiar with this can spot what I'm doing wrong. Any help is greatly appreciated.

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: ESP32 nvs_get_blob() help

Postby ESP_igrr » Mon Mar 27, 2017 12:13 pm

I ran your code, and it prints the following:

Code: Select all

get_configuration_values() ssid:ESP32 err:0x1102 reqsize:0
get_configuration_values() pass:password err:0x1102 reqsize:0
Note that the error is 0x1102 (not 0x102), which is ESP_ERR_NVS_NOT_FOUND.

Looking at the code, it makes sense, because 'nvs_set_blob' will only get executed iff err != ESP_OK AND err != ESP_ERR_NVS_NOT_FOUND. First nvs_get_blob returns ESP_ERR_NVS_NOT_FOUND, so we skip the part which does nvs_set and try to read again... which fails with the same ESP_ERR_NVS_NOT_FOUND error. So there's something odd with the logic in this code snippet.

Other problems with your code:
- you use "password" key in nvs_set_blob and later you use "pass" key in nvs_get_blob.
- you are not setting required_size variable to the size of destination buffer before your nvs_get_blob calls. What you probably need to do looks like this:

Code: Select all

    required_size = sizeof(cnf.ssidVal);
    err = nvs_get_blob(my_handle, "ssid", cnf.ssidVal, &required_size);
    printf("get_configuration_values() ssid:%s err:0x%x reqsize:%d\r\n",cnf.ssidVal,err,(int)required_size);
    required_size = sizeof(cnf.pass);
    err = nvs_get_blob(my_handle, "password", cnf.pass, &required_size);
    printf("get_configuration_values() pass:%s err:0x%x reqsize:%d\r\n",cnf.pass,err,(int)required_size);

blavoie
Posts: 16
Joined: Sat Jan 21, 2017 6:41 pm

Re: ESP32 nvs_get_blob() help

Postby blavoie » Wed Mar 29, 2017 11:07 am

Thank you very much for the help with pointing that out, I'll try what you mentioned here and post back with the results.

Who is online

Users browsing this forum: iParcelBox and 84 guests