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

Mqtt simple nrf9160, send JSON as utf-8

Hi,

I have configured the MQTT simple sample from nordic to send data to our broker, this works just fine at the moment.

I would like to send Json formated data to the broker ----->

{
"sensor_id": "c5278fbf-c6d5-4c48-b51d-652beb609b34",
"value": 13.0
}

When I subscribe to topic, I only see "{" as incoming data.

How can I make sure that the Json data is correctly formated i code ?

Any assistance would be appreciated.

I am using the <cJSON.h> library.

char field_name[80], value[80], *out;
                cJSON *root,*car;



                //char str1[80] = "{'sensor_id:' 'c5278fbf-c6d5-4c48-b51d-652beb609b34','value:' 13.0}";

                root  = cJSON_CreateObject();
                car=  cJSON_CreateArray();

                cJSON_AddItemToObject(root, "sensor_id", cJSON_CreateString("c5278fbf-c6d5-4c48-b51d-652beb609b34"));
                cJSON_AddItemToObject(root, "value", cJSON_CreateString("12"));
                cJSON_AddItemToArray(car, root);

                out = cJSON_Print(root);
                printk(out);
                //char out1 = {"sensor_id"":""c5278fbf-c6d5-4c48-b51d-652beb609b34","value"":""13.0"};
                k_sleep(K_MSEC(5000));
                data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, car, sizeof(car));
                k_sleep(K_MSEC(5000));

David

Parents Reply Children
  • I want it like this --->

    {
    "sensor_id": "c5278fbf-c6d5-4c48-b51d-652beb609b34",
    "value": "13.0"
    }

    without the quotes around 13.0

    David

  • Hi again, David!

    I've rewritten it to your configuration. However, there is an issue with the cJSON library that prevents it from handling doubles from what I could tell. Therefore you need to convert from string (i.e. "13.0") to float (i.e. 13.0) on the cloud end. 

    static void transmit()
    {
    	char *out;
    	char *id1 = "c5278fbf-c6d5-4c48-b51d-652beb609b34";
    
    	char value_buf[6];
    	double value = 13.0;
    
    	snprintf(value_buf, sizeof(value_buf), "%.1f",
    	value);
    
    	printk("Value: %s\n", value_buf);
    
    	cJSON *root;
    
    	/* create root node and array */
    	root = cJSON_CreateObject();	
    
    	/* add sensor data */
    	cJSON_AddItemToObject(root, "sensor_id", cJSON_CreateString(id1));
    	cJSON_AddItemToObject(root, "value", cJSON_CreateString(value_buf));
    
    	/* print everything */
    	out = cJSON_Print(root);
    	if (out == NULL) {
    		printk("Failed print...\n");
    	}
        printk("%s\n", out);
    
    	/* Publish with MQTT */
    	data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE,
       			out, strlen(out));
    
    	free(out);
    
    	/* free all objects under root and root itself */
    	cJSON_Delete(root);
    
    	return;
    }


    I hope this works out!

    Best regards,
    Carl Richard

  • Hi again, David!

    If you want to use numbers with the cJSON library, have a look at my answer here. The important part is to include cJSON_OS.h and call cJSON_Init at the start of main.
    Remember that CONFIG_HEAP_MEM_POOL_SIZE and CONFIG_MAIN_STACK_SIZE must be non-zero values.

    Best regards,
    Carl Richard

Related