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, 2019 in IoT (Internet of Things) by Shubham
• 13,490 points
3,845 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!!

If you need to know more about Azure, enroll with Microsoft Azure training course today.

Thank you!!

answered Jan 4, 2019 by Upasana
• 8,620 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,130 points
1,350 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,250 points
3,905 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,240 points
999 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 21, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,103 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,240 points
2,437 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,240 points
1,592 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,240 points
735 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,620 points
2,076 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP