Azure IoT Hub : Sending messages using Python via mqtt

0 votes

So, I created IoT hub, device, topic and subscription post setting up my Azure. Now I want to send a message via mqtt using a python script. What should I do to see it on the azure page? 

This is my code.

def on_connect(client, userdata, flags, rc):
  print ("Device connected with result code: " + str(rc))
def on_disconnect(client, userdata, rc):
  print ("Device disconnected with result code: " + str(rc))
def on_publish(client, userdata, mid):
  print ("Device sent message")

client = mqtt.Client(client_id=device_id, protocol=mqtt.MQTTv311)

client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_publish = on_publish

client.username_pw_set(username=iot_hub_name+".azure-devices.net/" + device_id, password=sas_token)

client.tls_set(certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None)
client.tls_insecure_set(False)

client.connect(iot_hub_name+".azure-devices.net", port=8883)

client.publish("devices/" + device_id + "/messages/events/", "{id=123}", qos=1)
client.loop_forever()

The issue being, I don't know how to specify the topic.

client.publish("devices/" + device_id + "/mytopicname/messages", "{id=123}", qos=1)

This thing I tried disconnects with code 1.

Jan 4 in IoT (Internet of Things) by Shubham
• 13,350 points
818 views

1 answer to this question.

0 votes

The thing is that IoT Hub is not a general-purpose pub-sub messaging broker. It's meant to support only the documented topic names and filters. 

Try reading this doc (Communicate with your IoT hub using the MQTT protocol).

Hope this helps.

answered Jan 4 by Upasana
• 8,550 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

Displaying Table Schema using Power BI with Azure IoT Hub

Answering your first question, Event Hubs are ...READ MORE

answered Aug 1, 2018 in IoT (Internet of Things) by nirvana
• 3,060 points
114 views
0 votes
1 answer

Testing if messages from my Device is reaching the Azure IoT Hub?

Download the Device Explorer open source tool ...READ MORE

answered Aug 10, 2018 in IoT (Internet of Things) by DataKing99
• 8,130 points
716 views
0 votes
1 answer

Control LED Blinking Using Java in Azure IoT Hub

Your description of what you did is ...READ MORE

answered Sep 5, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
175 views
0 votes
1 answer
0 votes
1 answer

Need of mqtt broker in IoT Application

This is an architectural choice. IoT applications ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
200 views
0 votes
1 answer

Integrate IoT device to Azure using MQTT

Today there isn't an official support for ...READ MORE

answered Sep 24, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
810 views
0 votes
1 answer

Server connection to iot device

It depends on the kind of socket, ...READ MORE

answered Oct 3, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
98 views
0 votes
1 answer

Bridging an Internet Server with IoT Module

I did something similar with an IoT ...READ MORE

answered Oct 29, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
49 views
0 votes
1 answer

Azure IoT: Message from cloud to device using Rest API

Firstly, If you get No content response(error ...READ MORE

answered Dec 31, 2018 in IoT (Internet of Things) by Upasana
• 8,550 points
363 views
0 votes
1 answer