Author Topic: ESP-IDF programming using NVS  (Read 1128 times)

0 Members and 1 Guest are viewing this topic.

Offline franzel61Topic starter

  • Newbie
  • Posts: 1
  • Country: de
ESP-IDF programming using NVS
« on: August 06, 2018, 03:24:53 pm »
Hallo Community,

it seems a simple programming question keeps me busy for a couple of days...... may someone can bring a torch to the dark. The Situation is on selection 2 (read the field)... I want to get the result shown in the field Char* Ergebnis. as result in selection 2 the field nvs_ret_data is a pointer

Hope for a solution
All the best Franzel61

/*****************************************************
 * NVS handling
 *
 *****************************************************/
Char* nvs(int nvs_select,char* nvs_key, char* nvs_data)
   {
   char* nvs_result="abababababababa";
   char* nvs_ret_data="bababababababababab";
   /
   printf("Selection %i\n",nvs_select);
   /*
    * write to NVS String --Select 1
    *
    */
   if (nvs_select==1)
         {
         esp_err_t err = nvs_flash_init();
         if (err != ESP_OK) printf("NO NVS Initialization\n");

         const esp_partition_t* nvs_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
         if(!nvs_partition) printf("FATAL ERROR: No NVS partition found\n");

         nvs_handle my_nvshandle;
         
         err = nvs_open(nvs_partition, NVS_READWRITE, &my_nvshandle);
         if (err != ESP_OK) printf("FATAL ERROR: Unable to open NVS\n");


         printf("wOS: Saving collected data into NVS...\n");
         printf("%s\n", nvs_data ); //Prints the correct value
         err = nvs_set_str(my_nvshandle, nvs_key, nvs_data );
         printf((err != ESP_OK) ? "wOS: Failed to save Collected Data\n" : "wOS: Successfully saved Collected Data!\n");
         
         esp_err_t nvs_commit(nvs_handle my_nvshandle);

         }
      /*
       * read from NVS String --Select 2
       *
       */
   if (nvs_select== 2)
         {
         esp_err_t err = nvs_flash_init();
         if (err != ESP_OK) printf("NO NVS Initialization\n");

         const esp_partition_t* nvs_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
         if(!nvs_partition) printf("FATAL ERROR: No NVS partition found\n");

         nvs_handle my_nvshandle;

         err = nvs_open(nvs_partition, NVS_READWRITE, &my_nvshandle);
         if (err != ESP_OK) printf("FATAL ERROR: Unable to open NVS\n");
         size_t nvs_required_size;
            err = nvs_get_str(my_nvshandle, nvs_key, NULL, &nvs_required_size );
            switch (err) {
                   case ESP_OK:
                        printf("wOS: Successfully grabbed size %i, loading in value..\n",nvs_required_size);
                        size_t nvs_ret_data = malloc(nvs_required_size);
                        err = nvs_get_str(my_nvshandle, nvs_key, &nvs_ret_data, &nvs_required_size );
                        printf( "Downloaded Data>: %s\n", nvs_ret_data );  // ****** the correct value will be shown

                     }

            return(nvs_ret_data);
}

      printf("result> %s < \n",nvs_ret_data);

   }
******************* and of NVS


void app_main()

   {
   /*
    * *******************************************
    * Initialization
    *********************************************
    */
   nvswait(10);
//write string
   nvs(1,"key4","merlin");
   nvswait(5);
//read string
   Ergebnis= nvs(2,"key3",NULL);
   printf("result 1st %s\n",Ergebnis);
   Ergebnis=nvs(2,"key4",NULL);
   printf("result 2nd - %s\n",Ergebnis);
}

« Last Edit: August 06, 2018, 03:26:26 pm by franzel61 »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf