0
votes

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.

3

3 Answers

2
votes

You've not enabled TLS - you'll need to use tls_set() to pass the CA certificates to trust. Using tls_insecure_set() on its own does nothing.

0
votes

There is a similiar thread from MS TechNet https://social.technet.microsoft.com/Forums/en-US/0c2cc683-c50f-428c-90ca-eeff132ba5e9/python-azure-iot-hub-mqtt-direct-with-basic-library?forum=windowsazuredevelopment.

I suggest that you can try to use other support languages for Azure IoTHub, such as Node.js or Java.

Meanwhile, you can refer to my sugguestion for using Azure IoTHub with Python in the SO thread Python MQTT connection to Azure Iot Hub via extending Python with other support languages.

Any concern, please feel free to let me know.

0
votes

Finally resolved the issue. It was the problem with setting tls and I wasn't able to do that in python for some reason, so I used C# ,

Here is a simple version of it.

client = new MqttClient("MyTestHub.azure-devices.net", 8883, true, MqttSslProtocols.TLSv1_0, null, null);
byte code = client.Connect("MyDevice1", "MyTestHub.azure-devices.net/MyDevice123","Use My SAS token");
ushort msgId = client.Publish("devices/MyDevice1/messages/events/", Encoding.UTF8.GetBytes("Test Message"), 1, false);

I used Nuget library for MQTT.