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
No Data
Related