How to read and save cjson data from and to nvs

master_mac
Posts: 2
Joined: Sat Sep 25, 2021 2:45 am

How to read and save cjson data from and to nvs

Postby master_mac » Sat Sep 25, 2021 2:50 am

Hi,

I have been trying to save a simple cjson data to nvs and read it back. I am kind of lost and would love if you could help me figure out what I am doing wrong. I want to add error (sent through parameter in the function) to be appended to the cjson message after each function call and print the cjson stored in nvs to see if everything is correct.

Thank you.

I have pasted the code in pastebin in the link below as well. I have commented it for clarity as well:

https://pastebin.com/4pNbF29b


  1. char* ErrorReporting(esp_err_t NodeError)
  2. {
  3.     ESP_LOGI(TAG, "NVS Error Reporting %d", NodeError);
  4.     esp_err_t err;
  5.        
  6.     cJSON *cJNodeError = NULL;
  7.  
  8.     cJSON *cJnodeErrorObject = cJSON_CreateObject();                     //Create a cjson object
  9.     cJNodeError = cJSON_CreateNumber(NodeError);                         //Create a cjson number to be added to the object
  10.     cJSON_AddItemToObject(cJnodeErrorObject, "NodeError", cJNodeError);  //Adding the cjson number created to the object
  11.     char *pNodeError = cJSON_PrintUnformatted(cJnodeErrorObject);        // Saving the cjson object into a variable.
  12.     int LengthOfcJson = strlen(pNodeError);                              // Getting length of cJson
  13.  
  14.     ESP_LOGI(TAG, "Size of cJsonString... %d", LengthOfcJson);           //Printing the length of cjson
  15.  
  16.     // Open
  17.     ESP_LOGI(TAG, "Opening Non-Volatile Storage (NVS) handle...");
  18.     nvs_handle nvsHandle;                                                //Creating nvshandle
  19.     err = nvs_open("nvsStorage", NVS_READWRITE, &nvsHandle);             //Opening the nvsStorage space for read and write
  20.     if (err != ESP_OK)
  21.     {
  22.         ESP_LOGI(TAG, "Error (%s) opening NVS handle!", esp_err_to_name(err));     //Checking for errors in opening nvs
  23.     }
  24.     else
  25.     {
  26.         ESP_LOGI(TAG, "NVS Storage opened");                            
  27.         // Read from NVS
  28.         ESP_LOGI(TAG, "Reading string from NVS ...");
  29.         size_t required_size = 0;
  30.         err = nvs_get_blob(nvsHandle, "nvsbuffer", NULL, &required_size); //This reads if the nvsbuffer is initialized before or not
  31.         int req_size = LengthOfcJson + required_size;                     //Calculating the total size of the data to be written
  32.         char *run_buffer = (char*)malloc(req_size*sizeof(int));           //creating a buffer to read data from
  33.         if (required_size > 0)
  34.         {
  35.             // The contents of nvsbuffer is saved in run_buffer and then is read from it
  36.             err = nvs_get_blob(nvsHandle, "nvsbuffer", run_buffer, &required_size);
  37.             required_size += LengthOfcJson;
  38.            
  39.             //Writing cJnodeErrorObject json string to nvsbuffer
  40.             err = nvs_set_blob(nvsHandle, "nvsbuffer", cJnodeErrorObject, required_size);
  41.  
  42.             char *string = cJSON_Print(cJnodeErrorObject);          
  43.             printf("The cjson value is: %s", string);         //print cJson
  44.  
  45.             if (err != ESP_OK)
  46.             {
  47.                 ESP_LOGI(TAG, "Error Setting string");
  48.             }
  49.  
  50.             // Commit written value.
  51.             ESP_LOGI(TAG, "Committing updates in NVS ...");
  52.             err = nvs_commit(nvsHandle);
  53.             if (err != ESP_OK)
  54.             {
  55.                 ESP_LOGI(TAG, "Error Committing to NVS");
  56.             }
  57.  
  58.             // Close the handle
  59.             free(run_buffer);
  60.             nvs_close(nvsHandle);
  61.             return run_buffer;
  62.         }
  63.        
  64.     }
  65.     return 0;
  66. }

ESP_YJM
Posts: 300
Joined: Fri Feb 26, 2021 10:30 am

Re: How to read and save cjson data from and to nvs

Postby ESP_YJM » Mon Sep 27, 2021 3:01 am

I think if you want to append error to old cjson stored in nvs, you need nvs_get_blob cjson data and append error to the buffer and than call nvs_set_blob to store buffer to nvs.

Who is online

Users browsing this forum: No registered users and 68 guests