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