I am trying to connect with Azure IoT-Hub with MQTT and send and receive messages.
I am following the official documentation given here
But it always get disconnected with result code: 1, though it never goes inside the on_connect function. But if I try to publish it outside the functions (The commented out line after connection string), it goes inside the on_publish method.
The deivce I am using here is a simulated device I created in the Azure IoT Suite
Here is the code I am using
from paho.mqtt import client as mqtt
def on_connect(client, userdata, flags, rc):
print "Connected with result code: %s" % rc
client.subscribe("devices/MyTestDevice02/messages/devicebound/#")
client.publish("devices/MyTestDevice02/messages/events", "Hello World!")
def on_disconnect(client, userdata, rc):
print "Disconnected with result code: %s" % rc
def on_message(client, userdata, msg):
print " - ".join((msg.topic, str(msg.payload)))
client.publish("devices/MyTestDevice02/messages/events", "REPLY", qos=1)
def on_publish(client, userdata, mid):
print "Sent message"
client = mqtt.Client("MyTestDevice02", mqtt.MQTTv311)
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
client.on_publish = on_publish
client.username_pw_set(username="USERNAME.azure-devices.net/MyTestDevice02",
password="SharedAccessSignature=SharedAccessSignature sr=USERNAME.azure-devices.net%2fdevices%2fMyTestDevice02&sig=xxxxxx5rRr7c%3d&se=1492318301")
client.tls_insecure_set(True) # You can also set the proper certificate using client.tls_set()
client.connect("USERNAME.azure-devices.net", port=8883)
#client.publish("devices/MyTestDevice02/messages/events", "Hello World!")
client.loop_forever()
Any help is appreciated. And I dont want to use the sdk which is why I am trying to publish it directly.