Мне удалось открыть прослушиватель сообщений iothub (сообщения из облака в устройство), но я не могу подписаться на прямой метод.Я пытаюсь использовать поддержку mqtt, а не библиотеку iothub
Я следовал https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-direct-methods#mqtt, но должна быть маленькая деталь, которую мне пока не удалось правильно сделать
это мой код (python)
from paho.mqtt import client as mqtt
import ssl
import token_generator #a script that exports the token , it is working fine for device messages
path_to_root_cert = "cert.cer"
device_id = "mydevice_id "
endpoint ="myiot_hub_name.azure-devices.net/devices/mydevice_id "
policyKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
sas_token = token_generator.generate_sas_token(endpoint ,policyKey ,None)
iot_hub_name = "myiot_hub_name"
def on_connect(client, userdata, flags, rc):
print ("Device connected with result code: " + str(rc))
def on_disconnect(client, userdata, rc):
print ("Device disconnected with result code: " + str(rc))
def on_publish(client, userdata, mid):
print ("Device sent message")
def on_message(client, userdata, msg):
print("Message received at: " + msg.topic+" with payload: "+str(msg.payload))
client = mqtt.Client(client_id=device_id, protocol=mqtt.MQTTv311)
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_publish = on_publish
client.on_message = on_message
client.username_pw_set(username=iot_hub_name+".azure-devices.net/" + device_id, password=sas_token)
client.tls_set(ca_certs=path_to_root_cert, certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None)
client.tls_insecure_set(False)
client.connect(iot_hub_name+".azure-devices.net", port=8883 )
client.subscribe("iothub/methods/POST/")
client.loop_forever()