problem to value of nvs_get_str ..... beginner issue

franzel61
Posts: 4
Joined: Mon Aug 06, 2018 6:44 pm

problem to value of nvs_get_str ..... beginner issue

Postby franzel61 » Mon Aug 06, 2018 7:45 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

Code: Select all

char* Ergebnis="predefinition";

/*****************************************************
 * NVS handling ...... just partial copied out
 *
 *****************************************************/
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);
}

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

Re: problem to value of nvs_get_str ..... beginner issue

Postby ESP_Sprite » Tue Aug 07, 2018 4:08 am

I've taken the liberty to put some code tags around your code, this way the forum doesn't chew up the indentation.

With that being said: what are you trying to achieve here? I can't make heads or tails from your code, and I'm sure that when we know what you're trying to do, we can tell you how to implement it in a way that makes more sense and has less code duplication.

franzel61
Posts: 4
Joined: Mon Aug 06, 2018 6:44 pm

Re: problem to value of nvs_get_str ..... beginner issue

Postby franzel61 » Tue Aug 07, 2018 10:38 am

I would like to create a function in ESP-IDF where I can
SelectFunction
A. Write a Integer,String or Blob to the NVS
B. Read out integer,string or blob
C. Delete the total content of the partition
D. Delete the content of a Key

ResultOfFunction
Like OK,NOK or Value on Read

Option: to create/select a specific partiton

For example

ResultOfFunction = NVS( SelectedFunction( as described above), Key, Value( as Integer,String or Blob));

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

Re: problem to value of nvs_get_str ..... beginner issue

Postby ESP_Sprite » Tue Aug 07, 2018 1:03 pm

Why are you trying to pull all of this through one single function, instead of having multiple functions to do the multiple things you want?

franzel61
Posts: 4
Joined: Mon Aug 06, 2018 6:44 pm

Re: problem to value of nvs_get_str ..... beginner issue

Postby franzel61 » Tue Aug 07, 2018 5:25 pm

The idea was to make it easy like neil did it on the Arduino IDE ...

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

Re: problem to value of nvs_get_str ..... beginner issue

Postby ESP_Sprite » Wed Aug 08, 2018 3:06 am

The difference is that the Arduino code is C++, and as such you have objects which can have different methods. Your intentions seem good, but you're trying to emulate this C++ solution with plain old C without giving consideration to how C is meant to work, and like pushing a square peg through a round hole, it's a bad fit and won't work. I suggest you either switch up to C++ so you can do things like resultstring=NVS.get("bla") or rewrite your code to have a separate function (nvs_writeint, nvs_writestr, nvs_deletekey, nvs_deletepart) for every thing you want to do.

Note that this, unfortunately, isn't the only issue with your code. You're also doing things wrong wrt returning stack-allocated variables (which is most likely your original issue... if you define variables inside your function, like you do with nvs_result, they disappear when you exit the function, so returning a pointer to those will not work) as well as 'weird' things like opening and closing the partition every time you do something with NVS. You may first want to try to use the NVS interface as is in whatever you're trying to develop, following the routes the examples and other existing code take to get done what they need to do, before trying to implement your own API on top of this one. Not trying to discourage you and there's nothing wrong with being a beginner, but it's usually more useful to learn from others' mistakes than to struggle through making them all yourself ;)

franzel61
Posts: 4
Joined: Mon Aug 06, 2018 6:44 pm

Re: problem to value of nvs_get_str ..... beginner issue

Postby franzel61 » Wed Aug 08, 2018 11:06 am

Thanks ESP-Sprite for the explanation, may your correct .... I will change my code

Who is online

Users browsing this forum: No registered users and 5 guests