Can not read strings from nvs partition after reboot (ESP_ERR_NVS_NOT_FOUND)

gianluca.siano
Posts: 12
Joined: Wed Sep 13, 2017 5:27 pm

Can not read strings from nvs partition after reboot (ESP_ERR_NVS_NOT_FOUND)

Postby gianluca.siano » Thu Jan 17, 2019 2:30 pm

Hi,

I can not read strings from the nvs partition after reboot, but I can see the written data if I dump the nvs partition over uart (with esp tool).
If I write int8 data then it works, but not with strings or blob data, I always become ESP_ERR_NVS_NOT_FOUND after reboot.

Here is my Debug output, after every reboot:

[0;31mE (485) eep: Error, NO data![0m
[0;32mI (485) eep: Setting Defaults ...[0m
[0;32mI (695) eep: Serial Number:'1234-1234'[0m


Her is my code:
  1. static const char* NvsESP_info = "espInfo";
  2. static const char* TAG = "eep";
  3. char Serial_Number[10];
  4. /******************************************************
  5.  *               Function Definitions
  6.  ******************************************************/
  7. uint8_t device_settings_eep_get(void)
  8. {
  9.     uint8_t result = NULL;
  10.     memcpy(Serial_Number, "0000-0000", sizeof(Serial_Number));  // Reset Serial Number
  11.  
  12.     // Open
  13.     nvs_handle my_handle;
  14.     nvs_open("nvs", NVS_READWRITE, &my_handle);
  15.  
  16.     // Read
  17.     size_t required_size = sizeof(Serial_Number);
  18.     // Read Serial Number from nvs partition
  19.     nvs_get_str(my_handle, NvsESP_info, &Serial_Number[0], &required_size);
  20.  
  21.     // Close
  22.     nvs_close(my_handle);
  23.  
  24.     // Compare Serial Number from nvs partition with Default (1234-1234)
  25.     if(!memcmp(&Serial_Number[0], "1234-1234", 9))
  26.     {
  27.         result = 1;
  28.         ESP_LOGI(TAG, "Serial Number:'%s'", &Serial_Number[0]);
  29.     }
  30.  
  31.     return result;
  32. }
  33.  
  34. void device_settings_eep_set(const char *key, char *value)
  35. {
  36.     nvs_handle my_handle;
  37.  
  38.     // Open
  39.     nvs_open("nvs", NVS_READWRITE, &my_handle);
  40.  
  41.     // Write
  42.     nvs_set_str(my_handle, key, value);
  43.  
  44.     // Commit
  45.     nvs_commit(my_handle);
  46.  
  47.     // Close
  48.     nvs_close(my_handle);
  49. }
  50.  
  51.  
  52. void device_settings_default_set(void)
  53. {
  54.     ESP_LOGI(TAG, "Setting Defaults ...");
  55.  
  56.     nvs_flash_erase();
  57.     nvs_flash_init();
  58.  
  59.     memcpy(Serial_Number, "1234-1234", sizeof(Serial_Number));
  60.  
  61.     device_settings_eep_set(NvsESP_info,(char*)&Serial_Number);
  62. }
  63.  
  64. void Init_nvs_partition(void)
  65. {
  66.     uint8_t value;
  67.     nvs_flash_init();
  68.     value = device_settings_eep_get();
  69.  
  70.     switch (value)
  71.     {
  72.         case 1:
  73.             ESP_LOGE(TAG, "ESP nvs was successfully initialized!");
  74.             break;
  75.         default :
  76.             ESP_LOGE(TAG, "Error, NO data!");
  77.             device_settings_default_set();
  78.             device_settings_eep_get();
  79.             break;
  80.     }
  81. }
  82.  

gianluca.siano
Posts: 12
Joined: Wed Sep 13, 2017 5:27 pm

Re: Can not read strings from nvs partition after reboot (ESP_ERR_NVS_NOT_FOUND)

Postby gianluca.siano » Tue Jan 29, 2019 10:11 am

Still doesn't function.

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

Re: Can not read strings from nvs partition after reboot (ESP_ERR_NVS_NOT_FOUND)

Postby ESP_Sprite » Wed Jan 30, 2019 3:55 am

Code: Select all

    device_settings_eep_set(NvsESP_info, --> (char*)&Serial_Number <-- );
This probably doesn't do what you want it to do.

mauriciofarina
Posts: 2
Joined: Wed Apr 03, 2019 11:39 am

Re: Can not read strings from nvs partition after reboot (ESP_ERR_NVS_NOT_FOUND)

Postby mauriciofarina » Wed Nov 20, 2019 12:40 am

Hello!

Did you figure out how to solve it? I'm having the same problem. If I write a string or blob and read it without rebooting everything works great, but after rebooting they go missing...

Oldeneill
Posts: 1
Joined: Sun Nov 15, 2020 3:57 pm

Re: Can not read strings from nvs partition after reboot (ESP_ERR_NVS_NOT_FOUND)

Postby Oldeneill » Sun Nov 15, 2020 4:14 pm

Well aware that this thread is pretty dated - but it pops up high in Google, so I thought I'd share in case someone still stumbles accross this issue as I did today :D

Had the exact same issue (did the write/commit/close-thingey as per the documentation) but rebooting kept giving me ESP_ERR_NVS_NOT_FOUND.

Turns out there's a little Comma-seperated names of partitions to clear on factory reset in the menuconfig's Bootloader config that said nvs as value - nice little "Aaahhhhhhh!"-moment..

Obvious course of action - just remove those 3 nasty little characters and you should be good to go :mrgreen:

Side note: I'm not knowledgeable enough to tell if there's a good reason for clearing the default nvs partition after a factory reset; might be that the proper solution is to add a custom NVS partition via a partition csv or something. Going into the "cross that bridge when we get to it"-bin.

Who is online

Users browsing this forum: Bing [Bot] and 62 guests