Python MQTT connection to Azure Iot Hub

后端 未结 4 670
粉色の甜心
粉色の甜心 2020-12-20 06:55

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 m         


        
相关标签:
4条回答
  • 2020-12-20 07:33

    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:

    1. create a device client and specifies MQTT for the protocol
    2. set the callback that will be called when a message is received
    3. use send_event_async to send messages to your Azure IoT Hub instance.
    from iothub_client import *
    
    def send_confirmation_callback(message, result, userContext):
        print "Confirmation[%d] received for message with result = %s" % (userContext, result)
    
    def receive_message_callback(message, counter):
        buffer = message.get_bytearray()
        size = len(buffer)
        print "Received Message"
        print "    Data: <<<%s>>> & Size=%d" % (buffer[:size], size)
        return IoTHubMessageDispositionResult.ACCEPTED
    
    iotHubClient = IoTHubClient(connectionString, IoTHubTransportProvider.MQTT)
    iotHubClient.set_message_callback(receive_message_callback, 0)
    iotHubClient.send_event_async(message, send_confirmation_callback, 0)
    
    0 讨论(0)
  • 2020-12-20 07:39

    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).

    0 讨论(0)
  • 2020-12-20 07:45

    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.

    1. Using Azure IoT SDK for C to extending Python, you can try to refer to https://docs.python.org/2/extending/extending.html to implement it.
    2. Using Azure IoT SDK for Java as Jython package imported, you can try to refer to http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html to know how to do it.
    3. The similar to the second way, integrating IronPython with Azure IoT SDK for C#/.Net, please refer to http://ironpython.net/documentation/dotnet/.
    4. The Azure IoT SDK for NodeJS support MQTT via the same Eclipse project 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.

    0 讨论(0)
  • 2020-12-20 07:49

    Here is how to use paho (mosquitto) to connect to the Azure IoT Hub over standard MQTT:

    from paho.mqtt import client as mqtt
    
    
    def on_connect(client, userdata, flags, rc):
        print "Connected with result code: %s" % rc
        client.subscribe("devices/<YOUR DEVICE ID>/messages/devicebound/#")
    
    
    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)))
        # Do this only if you want to send a reply message every time you receive one
        client.publish("devices/<YOUR DEVICE ID>/messages/events", "REPLY", qos=1)
    
    
    def on_publish(client, userdata, mid):
        print "Sent message"
    
    
    client = mqtt.Client(cleint_id=<YOUR DEVICE ID>, protocol=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="<YOUR NAMESPACE>.azure-devices.net/<YOUR DEVICE ID>",
                           password="<YOUR SHARED ACCESS SIGNATURE FOR THE DEVICE>")
    client.tls_insecure_set(True) # You can also set the proper certificate using client.tls_set()
    client.connect("<YOUR NAMESPACE>.azure-devices.net", port=8883)
    client.loop_forever()
    
    0 讨论(0)
提交回复
热议问题