How to read and save cjson data from and to nvs
Posted: 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
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
- char* ErrorReporting(esp_err_t NodeError)
- {
- ESP_LOGI(TAG, "NVS Error Reporting %d", NodeError);
- esp_err_t err;
- cJSON *cJNodeError = NULL;
- cJSON *cJnodeErrorObject = cJSON_CreateObject(); //Create a cjson object
- cJNodeError = cJSON_CreateNumber(NodeError); //Create a cjson number to be added to the object
- cJSON_AddItemToObject(cJnodeErrorObject, "NodeError", cJNodeError); //Adding the cjson number created to the object
- char *pNodeError = cJSON_PrintUnformatted(cJnodeErrorObject); // Saving the cjson object into a variable.
- int LengthOfcJson = strlen(pNodeError); // Getting length of cJson
- ESP_LOGI(TAG, "Size of cJsonString... %d", LengthOfcJson); //Printing the length of cjson
- // Open
- ESP_LOGI(TAG, "Opening Non-Volatile Storage (NVS) handle...");
- nvs_handle nvsHandle; //Creating nvshandle
- err = nvs_open("nvsStorage", NVS_READWRITE, &nvsHandle); //Opening the nvsStorage space for read and write
- if (err != ESP_OK)
- {
- ESP_LOGI(TAG, "Error (%s) opening NVS handle!", esp_err_to_name(err)); //Checking for errors in opening nvs
- }
- else
- {
- ESP_LOGI(TAG, "NVS Storage opened");
- // Read from NVS
- ESP_LOGI(TAG, "Reading string from NVS ...");
- size_t required_size = 0;
- err = nvs_get_blob(nvsHandle, "nvsbuffer", NULL, &required_size); //This reads if the nvsbuffer is initialized before or not
- int req_size = LengthOfcJson + required_size; //Calculating the total size of the data to be written
- char *run_buffer = (char*)malloc(req_size*sizeof(int)); //creating a buffer to read data from
- if (required_size > 0)
- {
- // The contents of nvsbuffer is saved in run_buffer and then is read from it
- err = nvs_get_blob(nvsHandle, "nvsbuffer", run_buffer, &required_size);
- required_size += LengthOfcJson;
- //Writing cJnodeErrorObject json string to nvsbuffer
- err = nvs_set_blob(nvsHandle, "nvsbuffer", cJnodeErrorObject, required_size);
- char *string = cJSON_Print(cJnodeErrorObject);
- printf("The cjson value is: %s", string); //print cJson
- if (err != ESP_OK)
- {
- ESP_LOGI(TAG, "Error Setting string");
- }
- // Commit written value.
- ESP_LOGI(TAG, "Committing updates in NVS ...");
- err = nvs_commit(nvsHandle);
- if (err != ESP_OK)
- {
- ESP_LOGI(TAG, "Error Committing to NVS");
- }
- // Close the handle
- free(run_buffer);
- nvs_close(nvsHandle);
- return run_buffer;
- }
- }
- return 0;
- }