This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

cJSON_print() issue

I'm using nRF9160DK and in that cJSON formation give me some error because of that my device will restart again and again

as in code if I'm input cJSON_AddNumberToObject(Data,"Humidity",22); that time its happening but I thing the problem with cJSON_print()

cJSON_AddItemToObject(Device_data1, "data", Data = cJSON_CreateObject());
cJSON_AddStringToObject(Data,"Temperature","67");
cJSON_AddStringToObject(Data,"Humidity","44");
//cJSON_AddNumberToObject(Data,"Humidity",22);

Attribute_json_Data = cJSON_Print(Attribute_json);

????

Parents Reply Children
  • I want publish my JSON data by MQTT to cloud

    Could you try to use this as a reference?

    I tried that way too but when I return cJSON that send blank value in string

  • Please share the error output and more of your code.

    void main(void)
    {
    	
    	if (!IS_ENABLED(CONFIG_AT_HOST_LIBRARY)) {
    		/* Stop the UART RX for power consumption reasons */
    		NRF_UARTE0_NS->TASKS_STOPRX = 1;
    		NRF_UARTE1_NS->TASKS_STOPRX = 1;
    	}
    
    
          cJSON *Attribute_json = NULL;
          cJSON *Device_data1 = NULL;
          cJSON *Device_data2 = NULL;
          cJSON *Data = NULL;
          cJSON *Sensor = NULL;
          char *out = NULL;
    
        Attribute_json = cJSON_CreateArray();
        cJSON_AddItemToArray(Attribute_json, Device_data1 = cJSON_CreateObject());
        cJSON_AddStringToObject(Device_data1, "uniqueId","uniqueId");
        cJSON_AddStringToObject(Device_data1, "time","2020-04-01T06:23:23.000Z");
        cJSON_AddItemToObject(Device_data1, "data", Data = cJSON_CreateObject());
        cJSON_AddNumberToObject(Device_data1,"Humidity", 45);
        //cJSON_AddStringToObject(Device_data1,"Humidity", "45");
    
        cJSON_AddNumberToObject(Data,"Temperature",23);
    
       
        out = cJSON_PrintUnformatted(Attribute_json);
        //out = cJSON_Print(Attribute_json);
    
        while(1){
           printk("\n >> %s \n",out);
           // printk("\n >> %s \n",cJSON_PrintBuffered(Attribute_json, 2000,0));
           k_sleep(5000);
           }
    
    }
    
    

    I shared main function try to in that I just want to print that Json I tried two three way you can see in comments parts

    tried with making different function and pass there may be that will work but result is same

    cJSON_AddNumberToObject(Device_data1,"Humidity", 45);

    if I use that way to add a Number in JSON that will restart again and again my device

    but if I used String on that case its working fine as below comment code    

    //    cJSON_AddStringToObject(Device_data1,"Humidity", "45");

    even if add a single json number in object that isn't working fine   ????

  • Have you tried setting up a debug session to see where in the code it triggers the reset?

Related