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

nRF9160 mqtt event message formatting for Azure IoT Hub

Hello,

I am trying to use the nRF9160 DK to publish some sensor data to an Azure IoT Hub. I have been able to successfully connect to the mqtt broker and publish simple strings as an D2C event message, but I am wanting to publish a json structure instead to take advantage of existing handling on our IoT Hub that I am not involved with in terms of development.

I have successfully recieved this d2c message:

{
    "event": {
        "origin": "{device-id}",
        "payload": "a"
    }
}

I want this:

{
    "event": {
        "origin": "{device-id}",
        "payload": {
            "published_at": "XXXX-XX-XXXXX:XX:XX.XXXX",
            "device_id": "{device-id}",
            "data": "0.710180:1.796526:48.254000:58.799999:988.430000",
            "event": "demo"
        }
    }
}

But I am getting this:

{
    "event": {
        "origin": "{device-id}",
        "payload": "{\n\"published_at\": \"XXXX-XX-XXXXX:XX:XX.XXXX\",\n\"device_id\": \"{device-id}\"\n,\"data\":\"0.710180:1.796526:48.254000:58.799999:988.430000\",\n\"event\": \"demo"
    }
}

Does anybody more familiar with zephyr's mqtt library or Azure's IoT Hub know what my options would be for formatting my message going into the mqtt_publish_param in mqtt.h? I feel like my hands are tied since the program is configured to send messages composed as type 'u8_t*'.

I included a copy of my project (mqtt_simple.zip), leaving out any specific information about my device or iot hub hostname if it would help anybody. Just find and replace all mentions of '{device-id}' with your proper device-id, replace '{hostname}' with the proper hostname of your iot hub, and replace '{shared-access-token}' with your generated token for your device. Do this for '\src\main.c', '\Kconfig', and '\prj.conf'.

I am using the command-line tool for Azure IoT Hub to monitor events:

In the command to monitor events, replace '{hostname}' with the proper hostname for your IoT Hub and {consumer-group} with a proper consumer group on your IoT Hub.

If I left out any important information that would be helpful, please let me know and I will provide it ASAP.

Thanks!

Isaac

Parents Reply Children
  • I think I'm on the right track for using cJSON in my program, but I'm still running into a similar issue, and that thread you linked doesn't seem to have been verified to work as a solution by the user, or at least there was no working implementation acknowledged or given.

    This is what I am doing with cJSON:

    The object printed to the terminal correctly:

    This code succesfully creates a cJSON object, but when I attempt to publish it via mqtt, it is still not formatted correctly.

    I'm not exactly sure where I should be looking to solve this, because I'm not sure what exactly the problem is. Any advice for where to start?

  • I now have the messages posting in json format using cJSON and mqtt. It turns out that you have to append the publishing topic in mqtt with tags denoting that the message is of the application/json type and it is UTF-8.

    I changed the publishing topic in prj.conf to the following:

    And now it works.

    Thanks for the help!