0

I have sent messages from device to Azure IoT Hub using MQTT client (like mosquitto). But I want to add custom properties to these messages like the standard SDK of Microsoft.

Azure IoT Message custom properties

Is there any suggestion for that?

Any idea will be really appreciated. Thanks in advance

Anh Tu
  • 75
  • 7
  • 1
    you can use a *topic* for publishing a custom property, for instance: **devices/mydeviceid/messages/events/compression=gzip&temperatureAlert=true** – Roman Kiss Feb 21 '23 at 21:20
  • 1
    you can use the below code # Define the message callback function def message_callback(message, user_context): print("Received message: {}".format(message)) print("Custom properties: {}".format(message.custom_properties)) print("Body: {}".format(message.data)) – Rajesh Mopati Feb 22 '23 at 05:33
  • # Send messages with custom properties while True: # Simulate telemetry values temperature = random.uniform(15, 30) humidity = random.uniform(20, 80) # Build the message with simulated telemetry values msg_txt = '{{"temperature": {temperature},"humidity": {humidity}}}' message = Message(msg_txt.format(temperature=temperature, humidity=humidity)) # Add a custom application property to the message if temperature > 38: message.custom_properties["temperatureAlert"] = "true" else: message.custom_properties["temperatureAlert"] = "false" – Rajesh Mopati Feb 22 '23 at 05:33
  • # Send the message print("Sending message: {}".format(message)) client.send_message(message) # Increment the counter counter += 1 – Rajesh Mopati Feb 22 '23 at 05:33
  • @RomanKiss Thank you. I have tried but it didn't work. Actually I only want to have compression=gzip. Should I format the custom properties to RFC2396 Standard like this document https://github.com/Azure/iotedge/blob/main/doc/edgehub-api.md? – Anh Tu Feb 23 '23 at 21:25
  • @RajeshM Thank you but it doesn't help me a lot. – Anh Tu Feb 23 '23 at 21:26
  • Hi, I have tried again and the solution of @RomanKiss is correct. Thank you so much again Roman :) – Anh Tu Feb 24 '23 at 00:49

1 Answers1

0

The solution could be found in

Sending device-to-cloud messages

https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support

Thank @Roman for your suggestion

Anh Tu
  • 75
  • 7