nrf9160: Publish a custom topic to Azure IoT Hub using Nordic Azure IoT Library

Hi everyone,

from our nRF9160 device I need to send data to our customers Azure IoT Hub instance, but I have no luck to receive anything on the cloud side so far. The data is a protobuf message which should be included within the MQTT message. This message should sent (published) to a certain topic specified by the custome like this:

devices/<device-id>/messages/events/device_id=<device-id>

For e.g. the topic would look like this:

devices/350914768187078/messages/events/device_id=350914768187078

Now I have spent days to find out, how I can alter the MQTT topic to my needs when using Nordics Azure IoT Library. I use a "azure_iot_hub_message" structure (from Nordics Azure IoT Hub Library), in which I have changed the topic name to

devices/350914768187078/messages/events/

and added a property (key/value pair) with

key = "device_id"

value="350914768187078"

Is this the correct approche to change the topic to our needs? Here the code snippet to show how I have done it in code:

    static uint32_t msg_id = 0;
    int err;
    uint8_t* payload = NULL;
    ssize_t len;

    len = protobuf_EncodeUsageUpdateObject(&payload); // payload holds the packed protobuf message (UsageUpdate object)

    char topic[] = "devices/350914768187078/messages/events/";

    char key1_string[] = "device_id";
    char value1_string[] = "350914768187078";

    struct azure_iot_hub_buf key1 = {
        .ptr = key1_string,
        .size = sizeof(key1_string),
    };

    struct azure_iot_hub_buf value1 = {
        .ptr = value1_string,
        .size = sizeof(value1_string),
    };

    struct azure_iot_hub_property prop = {
        .key = key1,
        .value = value1,
    };

    struct azure_iot_hub_msg msg = {
        .topic.type = AZURE_IOT_HUB_TOPIC_EVENT,
        .topic.name = topic,
        .topic.name.size = sizeof(topic),
        .topic.properties = &prop,
        .topic.property_count = 1,
        .payload.ptr = payload,
        .payload.size = len,
        .message_id = ++msg_id,
        .qos = MQTT_QOS_1_AT_LEAST_ONCE,
    };

Thanks in advance.

BR

Thomas

Parents Reply Children
Related