【问题标题】:Not able to send Cloud to Device Message to Azure IoT hub无法将云到设备消息发送到 Azure IoT 中心
【发布时间】:2022-01-14 01:58:47
【问题描述】:

所以,我正在尝试使用 Python 将消息发送到 Azure IoT 中心。我正在关注this 文档,但是当我运行脚本时:

from azure.iot.hub import IoTHubRegistryManager

RECEIVED_MESSAGES = 0

CONNECTION_STRING = "<IoT Hub Connection String>"
DEVICE_ID = "<IoT Edge Device ID>"

def iothub_messaging_sample_run():
    
    # Create IoTHubRegistryManager
    registry_manager = IoTHubRegistryManager(CONNECTION_STRING)
    i = 0

    while True:
        i += 1
        print(f'Sending message: {i}')
        data = f'NodeId : Node{i}, Message : Message{i}'
        registry_manager.send_c2d_message(DEVICE_ID, data)

if __name__ == '__main__':
    print('Starting the Python IoT Hub C2D Messaging service sample...')

    iothub_messaging_sample_run()

它首先打印这个: Sending message: 1
然后抛出这个错误: Exception: C2D message send failure
其余错误日志不包含任何其他信息,仅包含对 Azure IoT 中心包文件的引用。

我已经在防火墙上启用了Inbound Port 8883,但我仍然无法将数据发送到 Azure IoT Hub。

配置:

  • Python 版本 - 3.7.3
  • 包 - azure-iot-hub==2.6.0
  • 操作系统 - Windows 10 家庭版
  • IoT 中心 - 标准层 S1
  • Azure 订阅 - Azure 学生版
  • IoT 中心单元数量 - 1
  • IoT 中心位置 - 美国东部
  • 最低 TLS 版本 - 1.0
  • 设备数量 - 1(IoT Edge 设备)

【问题讨论】:

    标签: python azure mqtt azure-iot-hub


    【解决方案1】:

    通过链接的教程工作,我注意到您的实现完全不同。尽管如此,当我复制您的代码时,它确实可以在我的机器上运行,但我知道有两个原因会导致此错误:

    1. 您输入了错误的设备 ID。
    2. 设备的 C2D 队列已满。 C2D 消息队列有 50 条消息的限制。确保您的设备(或教程中的设备 Python 文件)正在侦听清空队列。您的代码没有延迟,因此您可以在几秒钟内填满队列,下次启动程序时,它可能会在收到第一条消息时崩溃。

    我还注意到你写道:

    DEVICE_ID = "<IoT Edge Device ID>"
    

    Azure IoT Edge 设备不支持 C2D 消息,虽然不会导致错误,但您将无法在设备上接收消息。

    【讨论】:

    • 所以这个过程不能在我的 IoT 中心的 Azure IoT 设备和相当简单的设备上执行?
    • 相反,只能在 Azure IoT 设备上执行。只是不是Azure IoT Edge 设备。
    猜你喜欢
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多