【问题标题】:How to send dynamic reported properties for Device twin using Python如何使用 Python 发送设备孪生的动态报告属性
【发布时间】:2021-03-19 04:36:50
【问题描述】:

我想定期更新 IoT 设备所需的属性。我发送的所需属性如下:

desired = {
    "para1" : {"time": [11,22,33]},
    "para2" : {"site":"demo.com"}
}

根据this 文档,以下是在设备内部运行的脚本

import time
import threading
from azure.iot.device import IoTHubModuleClient

CONNECTION_STRING = "IOTHUB Device Connection String"


def twin_update_listener(client):
    while True:
        patch = client.receive_twin_desired_properties_patch()  # blocking call
        print("Twin patch received:")
        print(patch)

def iothub_client_init():
    client = IoTHubModuleClient.create_from_connection_string(CONNECTION_STRING)
    return client

def iothub_client_sample_run():
    try:
        client = iothub_client_init()

        twin_update_listener_thread = threading.Thread(target=twin_update_listener, args=(client,))
        twin_update_listener_thread.daemon = True
        twin_update_listener_thread.start()

        # Send reported
        print ( "Sending data as reported property..." )
        reported_patch = {"connectivity": "cellular"}
        client.patch_twin_reported_properties(reported_patch)
        print ( "Reported properties updated" )

        while True:
            time.sleep(1000000)
    except KeyboardInterrupt:
        print ( "IoT Hub Device Twin device sample stopped" )

if __name__ == '__main__':
    print ( "Starting the Python IoT Hub Device Twin device sample..." )
    print ( "IoTHubModuleClient waiting for commands, press Ctrl-C to exit" )
    iothub_client_sample_run()

这里将reported_properties 更新为静态值(reported_patch = {"connectivity": "cellular"}),但我想上传与所需值相同的报告值:

{
        "para1" : {"time": [11,22,33]},
        "para2" : {"site":"demo.com"}
    }

请告诉我如何实现这一目标。

【问题讨论】:

  • 所以您要上传reported_properties,其值与之前上传的期望值相同?
  • 是的,我希望报告的值具有与所需完全相同的数据

标签: python azure-iot-hub


【解决方案1】:

试试这个,将desired 的值上传到reported 一次desired 的值更改:

import time
import threading
from azure.iot.device import IoTHubModuleClient

CONNECTION_STRING = ""


def twin_update_listener(client):
    while True:
        patch = client.receive_twin_desired_properties_patch()  # blocking call
        print("Twin patch received")
        
        patch.pop('$version',None)
        print ( "Sending Twin as reported property..." )
        print(patch)
        client.patch_twin_reported_properties(patch)
        print ( "Reported properties updated" )
        

def iothub_client_init():
    client = IoTHubModuleClient.create_from_connection_string(CONNECTION_STRING)
    return client

def iothub_client_sample_run():
    try:
        client = iothub_client_init()

        twin_update_listener_thread = threading.Thread(target=twin_update_listener, args=(client,))
        twin_update_listener_thread.daemon = True
        twin_update_listener_thread.start()      
       
        while True:
            time.sleep(1000)
    except KeyboardInterrupt:
        print ( "IoT Hub Device Twin device sample stopped" )

if __name__ == '__main__':
    print ( "Starting the Python IoT Hub Device Twin device sample..." )
    print ( "IoTHubModuleClient waiting for commands, press Ctrl-C to exit" )
    iothub_client_sample_run()

【讨论】:

  • 非常感谢 Stanley,现在一切正常。
  • @vishruti,很高兴为您提供帮助 :)
猜你喜欢
  • 1970-01-01
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
  • 2021-09-17
  • 1970-01-01
  • 2023-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多