Skip to content Skip to sidebar Skip to footer

Azure Iothub Devicemessage And Route Filter

I use python and paho.mqtt for sending messages to cloud I set up endpoint and route. When I set query string to true, everything works fine messageDict = {} systemPropertiesDict =

Solution 1:

The route not working because the content encoding type is not set. All the "systemProperties" in your code actually as message body not system properties. Content encoding type set by this method doesn't take effect.

Add "$.ct=application%2Fjson&$.ce=utf-8" to the topic. Then it will look like this:

devices/{yourDeviceId}/messages/events/$.ct=application%2Fjson&$.ce=utf-8

But to make the route query works on your message you need use this query string: $body.message.body.id = 1

Two edits to make:

First, change body = '{id:1}' to body = {"id":1} to make the id as a string.

Second, change topicName value to this one:

devices/{yourDeviceId}/messages/events/$.ct=application%2Fjson&$.ce=utf-8

If possible, it is suggesting to use Azure IoT SDK for Python to communicate with Azure IoT Hub.

Solution 2:

If you are using a third-party library (like paho-mqtt) you have to specify the content type and the encoding of the message. IoT Hub route messages with content type "application/json" and content encoding: "utf-8" or "utf-16" or "utf-32". Using MQTT protocol you can set this info with $.ct and $.ce.

topic example:

devices/{MY_DEVICE_ID}/messages/events/%24.ct=application%2fjson&%24.ce=utf-8

URL encoding of

devices/{MY_DEVICE_ID}/messages/events/$.ct=application/json&$.ce=utf-8

Here you could found more info: https://azure.microsoft.com/it-it/blog/iot-hub-message-routing-now-with-routing-on-message-body/

Post a Comment for "Azure Iothub Devicemessage And Route Filter"