vishruti
vishruti

Reputation: 475

How to update Azure Device Twin desired and reported properties

I am having a JSON payload which I want to pass to the Azure device twin with the help of API and python script integration. The sample payload is as follows:

para1 = {"time": [0,23,30]}
para2 = {"site":"test"}

I want to pass this data to the device twin, so that even if the device is offline, as soon as it comes online the information will be updated. Please help me achieve this flow.

Upvotes: 3

Views: 1494

Answers (1)

Stanley Gong
Stanley Gong

Reputation: 12153

If you want to pass some params to Azure IOT device twin Desired properties, so that your device could receive it later, try the code below :

import sys
from time import sleep
from azure.iot.hub import IoTHubRegistryManager
from azure.iot.hub.models import Twin, TwinProperties

IOTHUB_CONNECTION_STRING = ""
DEVICE_ID = ""


iothub_registry_manager = IoTHubRegistryManager(IOTHUB_CONNECTION_STRING)

desired = {
    "para1" : {"time": [0,23,30]},
    "para2" : {"site":"test"}
}

twin = iothub_registry_manager.get_twin(DEVICE_ID)

twin_patch = Twin(properties= TwinProperties(desired=desired))

iothub_registry_manager.update_twin(DEVICE_ID, twin_patch, twin.etag)

Result: enter image description here

UPDATE: Seems there is something wrong with your connection string, you can find it here:

enter image description here

Upvotes: 2

Related Questions