NVS issue, unable read stored data from NVS

masadf22
Posts: 1
Joined: Thu Dec 17, 2020 1:19 pm

NVS issue, unable read stored data from NVS

Postby masadf22 » Thu Dec 17, 2020 1:45 pm

Hi, I am able to write successfully but when I read from NVS it throws an error of "Error (ESP_ERR_NVS_INVALID_LENGTH) reading!"

Code: Select all

[Codebox=c file=Untitled.c]void set_nvs_string(char* data, char* key)
{
	nvs_handle my_handle;
	esp_err_t err = nvs_open("storage", NVS_READWRITE, &my_handle);
	if (err != ESP_OK) {
		printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
	}				
	err = nvs_set_str(my_handle, key, data);
	printf((err != ESP_OK) ? "Failed! Setting String from NVS\n" : "Done Setting String from NVS\n");
	printf("Committing updates in NVS ... ");
	err = nvs_commit(my_handle);
	printf((err != ESP_OK) ? "Failed! Commiting\n" : "Done Commiting\n");
}

esp_err_t get_nvs_string(char* key, char* data)
{
	nvs_handle my_handle;
	esp_err_t err = nvs_open("storage", NVS_READWRITE, &my_handle);
	if (err != ESP_OK) {
		printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
	}
	size_t size = strlen(data);
	err = nvs_get_str(my_handle, key, data, &size);
	switch (err) 
	{
	case ESP_OK:
		printf("Done Getting String from NVS\n");
		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));
	}
	return err;
}

ESP_LOGI(TAG_ap, "SSID Local: %s  PASSWORD Local: %s Inverter ID %s", ssidLocal, passwordLocal, InverterId);
	set_nvs_string(InverterId, "InverterId");
	
	
	if (strlen(ssidLocal) != 0 && strlen(passwordLocal) != 0)
	{

		set_nvs_string(ssidLocal, "ssid");
		set_nvs_string(passwordLocal, "password");
		wifi_config_t wifi_config;
		
		
		char* tempSsid = malloc(sizeof(char) * 32);
		char* tempPassword = malloc(sizeof(char) * 64);
	
	
		get_nvs_string("ssid", tempSsid);
		get_nvs_string("password", tempPassword);
	
	
		strcpy((char*)wifi_config.sta.ssid, tempSsid); 
		strcpy((char*)wifi_config.sta.password, tempPassword);

		ESP_LOGI("WIFI CHECK4", "Set SSID: %s", (char*)wifi_config.sta.ssid);
		ESP_LOGI("WIFI CHECK4", "Set Password: %s", (char*)wifi_config.sta.password);
	
		free(tempSsid);
		free(tempPassword);
			
		ESP_LOGI(TAG_ap, "=========== PARSED DATA ==========");
		ESP_LOGI(TAG_ap, "SSID: %s  PASSWORD: %s", wifi_config.sta.ssid, wifi_config.sta.password);
		ESP_LOGI(TAG_ap, "====================================");
		ESP_ERROR_CHECK(esp_wifi_stop());
		wifi_init_sta();
			
		
		s_retry_num = 0;
	}[/Codebox]


Output:

  1. I (146517) Hojaa Pls: SSID Local: ZongAltium1  PASSWORD Local: Xavor123 Inverter ID Schneider
  2. Done Setting String from NVS
  3. Committing updates in NVS ... Done Commiting
  4. Done Setting String from NVS
  5. Committing updates in NVS ... Done Commiting
  6. Done Setting String from NVS
  7. Committing updates in NVS ... Done Commiting
  8. Done Getting String from NVS
  9. Error (ESP_ERR_NVS_INVALID_LENGTH) reading!
  10. I (146557) wifi: station: 9e:62:e3:2e:66:f5 leave, AID = 1, bss_flags is 134243
  11. I (146567) WIFI CHECK4: Set SSID: ZongAltium1
  12. I (146567) wifi: n:1 0, o:1 0, ap:1 1, sta:0 0, prof:1
  13. I (146567) WIFI CHECK4: Set Password: ???
  14. I (146577) Hojaa Pls: =========== PARSED DATA ==========
  15. I (146587) Hojaa Pls: SSID: ZongAltium1  PASSWORD: ???
  16. I (146587) Hojaa Pls: ====================================
  17. I (146637) wifi: flush txq
  18. I (146637) wifi: stop sw txq
  19. I (146637) wifi: lmac stop hw txq
  20. Error (ESP_ERR_NVS_INVALID_LENGTH) reading!
[/color][/color]

dkaufmann
Posts: 24
Joined: Wed May 17, 2017 9:06 am

Re: NVS issue, unable read stored data from NVS

Postby dkaufmann » Wed Jun 29, 2022 6:37 am

Hello. I had a very similar problem. In my case the initialization of the size was wrong
You could try:

Code: Select all

size_t size = strlen(data)+1;
best regards
Dominik

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

Re: NVS issue, unable read stored data from NVS

Postby ESP_igrr » Wed Jun 29, 2022 1:48 pm

Indeed, this part of the code leads to the issue:

Code: Select all

	size_t size = strlen(data);
	err = nvs_get_str(my_handle, key, data, &size);
The last argument of nvs_get_str has the following meaning (see docs):
  • as an input, it should contain the amount of space (number of bytes) available in "out_value" pointer.
  • as an output, if "out_value" is not NULL, it will be filled with the actual number of bytes written into "out_value", including the NULL terminator
So the code should look like this:

Code: Select all

// 1. add "out_data_size" argument to tell this function how much space is available in "out_data".
// 2. Also make "key" const, since it is not modified by this function.
esp_err_t get_nvs_string(const char* key, char* data, size_t data_size)
{
	nvs_handle my_handle;
	esp_err_t err = nvs_open("storage", NVS_READWRITE, &my_handle);
	if (err != ESP_OK) {
		printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
	}
	// 3. as input, "size" should contain the amount of space available
	size_t size = out_data_size;
	err = nvs_get_str(my_handle, key, data, &size);
	// 4. after this call, if err == ESP_OK, "size" will contain the number of bytes written into "data"
...
Then the caller of this function should pass the "data_size" value based on the size of the storage allocated for "data" variable.
For example:

Code: Select all

char value[32];
err = get_nvs_string("some_key", value, sizeof(value));

Who is online

Users browsing this forum: Google [Bot] and 68 guests