I want to connect to Azure Iot Hub, with Python MQTT.
An username and SAS token is required by Iot Hub. This is my code:
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("$SYS/#")
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set("myHub.azure-devices.net/device1", "mySASToken")
client.connect("myHub.azure-devices.net", 1883, 60)
client.loop_forever()
But after running for a while, this exception is thrown:
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Does somebody know why I can't connect to the Iot Hub?
Here is how to use paho (mosquitto) to connect to the Azure IoT Hub over standard MQTT:
As @FarukCelik said, there was no Azure IoT SDK for Python.
However, per my experience, I think there are four practicable ways using the existing SDK for Azure IoTHub in Python.
Paho
for JavaScript Client, so I think you can try to refer to the source code of Azure NodeJS IoT SDK on GitHub to know how to correctly using paho Python client for Azure IoTHub.Meanwhile, there is a unoffical Python library for Azure IoTHub Device on GitHub https://github.com/bechynsky/AzureIoTDeviceClientPY. That you can concern about this project repository, but it's still on the deveploment stage by now.
Hope it helps. Best Regards.
Referring to https://azure.microsoft.com/en-gb/documentation/articles/iot-hub-sdks-summary/ Azure IoT SDK does not contain Pyhon "yet". This is already raised by other customers in https://feedback.azure.com/forums/321918-azure-iot . (Direct link : https://feedback.azure.com/forums/321918-azure-iot/suggestions/10522101-add-python-client-sdk).
There is now an official Python SDK to connect devices to Azure IoT Hub: https://github.com/Azure/azure-iot-sdks/tree/master/python/device
This sample demonstrates how to connect using the MQTT protocol.
Basically, here's how it works: