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*'.
/** @brief Abstracts binary strings. */ struct mqtt_binstr { u8_t *data; /**< Pointer to binary stream. */ u32_t len; /**< Length of binary stream. */ }; /** @brief Parameters for a publish message. */ struct mqtt_publish_message { struct mqtt_topic topic; /**< Topic on which data was published. */ struct mqtt_binstr payload; /**< Payload on the topic published. */ }; /** @brief Parameters for a publish message. */ struct mqtt_publish_param { /** Messages including topic, QoS and its payload (if any) * to be published. */ struct mqtt_publish_message message; /** Message id used for the publish message. Redundant for QoS 0. */ u16_t message_id; /** Duplicate flag. If 1, it indicates the message is being * retransmitted. Has no meaning with QoS 0. */ u8_t dup_flag : 1; /** Retain flag. If 1, the message shall be stored persistently * by the broker. */ u8_t retain_flag : 1; };
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:
az iot hub monitor-events --hub-name {hostname} -t 0 --cg {consumer-group}
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