Automate Device Updates with Device Twins in Azure IOT Hub
Device twins are JSON documents that have device state information including metadata, configurations, and conditions. A device twin is created and managed for each device connected to your IOT project that is added to the IoT Hub. The following article summarizes the key parts and functions of the Device Twin. For more detailed information visit the cited Microsoft Documentation.
Role of the Device twin
The Device twin is used for several key applications and these include:
- Storing metadata on a device in the field, for example what is the exact geo location of an IoT device such as a weather sensor.
- Reporting on the current status and capabilities of applications running on a device such as whether or not the device is connected via a cellular link to the IoT Hub.
- Synchronizing the state of established workflows linking the device apps to the back end such as jobs where the back end specifies that a new firmware update is available for the edge device, and then the device app reports back on the various stages of the updating process.
- Querying the device state information, device configuration or other metadata.
Structure of the Device twin
The Device Twin as a JSON document consists of the following elements: device twin tags for the backend to read and write to, desired properties which can be set so the device app can read these desired properties and apply those accordingly. The device app can also receive notifications of changes to the desired properties. In conjunction with desired properties, reported properties synchronize device configuration and settings. The device can also present reported properties so that the backend can read and query them. And the root of the JSON document contains device identity properties stored in the device identity registry which holds the information about which modules and devices are allowed to connect to IoT Hub.
Usage of the Device twin
On the high level the Device twin is a way to configure the device and to query information flowing from the device to the backend. In the typical scenario one needs two components: an application running on the device and the solution backend.
The solution backend can retrieve the Device twin from a given device by unique device ID together with tags and reported properties. Tags and desired properties can also be partially updated by the solution backend where the partial update is expressed as a JSON. The solution backend can also replace desired properties, tags and receive notifications as and when the Device twin is modified.
The device can retrieve the Device twin with desired properties which are supposed to be applied and processed by the device app to configure the device. In response, the device app can send its status as a set of the reported properties which are processed by the backend to update the latest device configuration and status. The Device twin and its reported properties should be only used though in scenarios when the backend is supposed to be updated with the information about the state of the device, not as a way of sending more frequent telemetry data for which different solutions should be used.
A full tutorial on how to get started with Device twins is available in the Microsoft Documentation.
Mender and Azure IoT Hub
Mender has an existing integration with Azure to synchronize device credentials for simplifying the device provisioning and management including device updates amongst other things. This integration is currently being further improved so that Mender users can provision and inspect devices registered in Azure IoT Hub through the Mender Web UI, and synchronize information about the device such as its software update status or configuration with the Azure Device Twin. This means that devices only need to be provisioned and managed in one place while the users get the benefits of both Mender and Azure for device fleet management at scale.
If you would like a sneak preview of these upcoming integration improvements, please visit our preview page and leave your email address.
To learn about device updating and device provisioning in Azure IoT, check our the following articles: