azure iothub直接方法未按预期工作

标签 azure mqtt azure-iot-hub

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

我已关注https://learn.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()

最佳答案

客户端应该订阅该主题:

$iothub/methods/POST/#

以下是其他订阅主题:

$iothub/twin/res/#
$iothub/twin/PATCH/properties/desired/#
devices/{myDeviceId}/messages/devicebound/#
devices/{myDeviceId}/modules/{myModuleId}/messages/devicebound/#

以及设备流(当前处于预览状态)

$iothub/streams/POST/#

关于azure iothub直接方法未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54880165/

相关文章:

azure - 如何在 Azure 中将多个 CloudBlockBlob 对象连接成一个 CloudBlockBlob 对象?

python - 部署到 Azure 应用服务后,Flask 与 Azure SQL 数据库的连接失败

python - Google IoT 端到端示例中的连接错误

c# - 定期监视 "connectionState"以识别 Azure IOTHub 中已连接设备的最佳方法

azure-functions - 如何在 Azure Functions 中的 EventHub 输出绑定(bind)上设置 systemProperties

azure - 为什么我对 Azure 的调用会杀死 HttpContext.Current

azure - 服务主体凭据,设置自定义标识符?

c++ - 使用 Paho MQTT C++ 连接到 AdafruitIO

docker - 如何输出在docker中运行的mqtt的自定义日志

Azure IoT 中心 - 如何在 IoT 中心使用表存储?