iothub直接方法mqtt无法正常工作 [英] iothub direct method mqtt not working

查看:121
本文介绍了iothub直接方法mqtt无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在iothub消息上打开一个监听器(从云到deivce的消息),但我无法订阅直接方法。我正在尝试使用mqtt支持,而不是iothub库 

i was able to open a listener on iothub messages (messages from cloud to deivce) but i am unable to subscribe to direct method . I am trying to use mqtt support , not the iothub library 

我已关注https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub -devguide-direct-methods#mqtt

但是必须有一个小细节,我还没有设法做到正确但是b
$ b这个是我的代码(python)



I have followed https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-direct-methods#mqtt
but there must be a small detail that i haven't managed to do correctly yet

this is my code (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=443)

client.subscribe("iothub/methods/POST/")

client.loop_forever()







推荐答案

见这里:  https://docs.microsoft。 com / zh-CN / azure / iot-hub / iot-hub-mqtt-support #response-a-direct-method

See here: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support#respond-to-a-direct-method

我相信您的代码应该看起来喜欢:

I believe your code should look like:

client.subscribe("


iothub / methods / POST /#"
iothub/methods/POST/#")

谢谢!


这篇关于iothub直接方法mqtt无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆