#AzureIoT – Working with Digital Twin Settings on an Azure IoT Edge Module

Hi !

In my previous series of posts I wrote about how to create custom modules to work using Custom Vision projects, and also sensors in a Raspberry Pi. Today, I’ll pick my Door demo, and share some insights about how to work work with Twin properties.

Define Twin Properties

Let’s start defining our twin properties. We can do this in our Visual Studio project, adding the information to the [deployment.template.json] file. Or, we can also do this on the module definition, on the Azure IoT Portal.

Once we are setting the module properties, we can add the necessary information in the [Module Twin Settings] tab.

azure iot define twin properties for custom iot edge module

Once the module is success fully deployed, we can access to the twin properties and check the desired and reported states.

azure iot twin properties as part of the module

Reading Digital Twin properties changes

Now it’s time to create a new thread.. In this thread, we will check for changes in the Digital Twin properties and work with those changes. The following code is based on the references suggested code.

  • Code will iterate and check the DT changes every N seconds
  • When changes are received, the code will get specific values for door state and door state description
def twin_patch_receive_messages():
global twin_callbacks
global module_client
global read_twin_sleep_interval
# Define behavior for receiving twin desired property patches
def twin_patch_handler(twin_patch):
try:
# sample received data
# {'doorState': 1, 'doorStateDesc': '3', 'doorStateSource': 'digital twin new', '$version': 88}
doorState = twin_patch["doorState"]
source = twin_patch["doorStateSource"]
print (f" New Door state : {doorState} – source : {source}")
except Exception as ex:
print ( "Unexpected error in twin_patch_handler: %s" % ex )
while True:
try:
# Set handlers on the client
module_client.on_twin_desired_properties_patch_received = twin_patch_handler
twin_callbacks += 1
time.sleep(read_twin_sleep_interval) # settings to define how often refresh dt information
except Exception as ex:
print ( "Unexpected error in twin_patch_listener: %s" % ex )

The printed log is similar to the following sample:

2021-09-08 20:51:10.836744 | Twin patch received - loops : 23
2021-09-08 20:51:10.837610 | {'doorState': 0, 'doorStateDesc': '', 'doorStateSource': 'digital twin', '$version': 89}
2021-09-08 20:51:10.837690 |  New Door state : 0 - source : digital twin
2021-09-08 20:51:10.837727 | start close - source digital twin
2021-09-08 20:51:12.240233 | start send message

Updating Twin properties

In order to update twin properties, we can use the following code as reference.

def update_device_state(results, source, doorState, doorStateDesc):
global module_client
try:
# update twin properties
reported_patch = {"doorState": doorState}
asyncio.run(module_client.patch_twin_reported_properties(reported_patch))
reported_patch = {"doorStateDesc": doorStateDesc}
asyncio.run(module_client.patch_twin_reported_properties(reported_patch))
reported_patch = {"doorStateSource": source}
asyncio.run(module_client.patch_twin_reported_properties(reported_patch))
except Exception as e:
AzureIoTLogger.Log ( "update_device_state – Unexpected error %s " % e )
raise

On the Module definition, we can check the reported properties.

azure iot updated and reported properties on azure iot edge module

References

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno



Azure ☁ IoT

Create an Azure IoT Module using Raspberry Pi and Grove Sensors

  1. Raspberry Pi + Grove Sensors, read temperature and humidity values
  2. Raspberry Pi + Grove Sensors, send temperature and humidity values as telemetry to Azure IoT Hub
  3. Raspberry Pi + Grove Sensors, create a Azure IoT Module to send temperature and humidity values as telemetry to Azure IoT Hub
  4. Raspberry Pi + Grove Sensors, publish and use the Azure IoT Module
  5. Raspberry Pi + Grove Sensors, notes on build and configuration
  6. Raspberry Pi + Grove Sensors, details on how to send a telemetry message and sample messages

Create an Azure IoT Module from Azure Custom Vision project


¿Con ganas de ponerte al día?

En Lemoncode te ofrecemos formación online impartida por profesionales que se baten el cobre en consultoría:

Advertisement

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: