ESP32 nvs_get_blob() help
Posted: 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.
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.