Hi !
In my previous post I wrote about the libraries that we will use to access to Twin Properties in an Azure IoT Edge Module. Let’s go for it.
Configuration
We will use 3 main settings to read and write Twin information
# connect using IoTHub connection string and device id with the module identity name
IOTHUB_CONNECTION_STRING = "IoT Hub Connection String"
DEVICE_ID = 'Device Id'
MODULE_ID = 'Module Id'
Read Twin Properties
Let’s read twin properties with a couple of lines
# read twin
iothub_registry_manager = IoTHubRegistryManager(IOTHUB_CONNECTION_STRING)
module_twin = iothub_registry_manager.get_module_twin(DEVICE_ID, MODULE_ID)
twin_properties = "{0}".format(module_twin.properties)
print ( twin_properties )
Update Twin Properties
And, if we need to update this, similar code.
iothub_registry_manager = IoTHubRegistryManager(IOTHUB_CONNECTION_STRING)
module_twin = iothub_registry_manager.get_module_twin(DEVICE_ID, MODULE_ID)
twin_original = "{0}".format(module_twin.properties)
# Update twin
twin_patch = Twin()
twin_patch.properties = TwinProperties(desired={"doorState": 0, "doorStateSource": "python test app"})
updated_module_twin = iothub_registry_manager.update_module_twin(DEVICE_ID, MODULE_ID, twin_patch, module_twin.etag)
# display changes
twin_updated = "{0}".format(updated_module_twin .properties)
print (f" original: {twin_original} ")
print (f" updated: {twin_updated} ")
Conclusion
Once you find the right libraries and code sample, read and update Twin Properties is easy. Now I’m ready to write a couple of Azure Functions that will interact with my Azure IoT Edge Modules Twin Properties !
References
Happy coding!
Greetings
El Bruno
More posts in my blog ElBruno.com.
More info in https://beacons.ai/elbruno
¿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:
- Si tienes ganas de ponerte al día con Front End (ES6, Typescript, React, Angular, Vuejs…) te recomendamos nuestros Máster Front End: https://lemoncode.net/master-frontend#inicio-banner
- Si te quieres poner al día en Backend (stacks .net y nodejs), te aconsejamos nuestro Bootcamp Backend: https://lemoncode.net/bootcamp-backend#bootcamp-backend/banner
- Y si tienes ganas de meterte con Docker, Kubernetes, CI/CD…, tenemos nuestro Bootcamp Devops: https://lemoncode.net/bootcamp-devops#bootcamp-devops/inicio
Reblogged this on .
LikeLike