This post is focusing on Device Twins that are available on Azure IoT Hub.
If it is your first time when you hear about Azure IoT Hub, than you should know that Azure IoT Hub is a powerfull serviced offered by Azure that can connect, manage and monitor your devices. More information about it can be found on Azure site
Why Device Twins?
When we are working with devices, we will find all the time information that is specific to individual device. Information like device serial number, customer information, location, device configuration is part of this type of information.
In an ideal world, we should have a unique location where all this data is stored. No custom code of functionality should be developed by us. Azure IoT Hub is offering this to us through Device Twins.
What are Device Twins?
It’s a collection of properties that specific for each individual device. Device twins contains 3 types of structure (where the last two are similar, but have different roles)
You might find odd that there are 2 types of properties/structures in device twin – tags and desired/reported properties. From the usability point of view, this is useful and handy.
Tags are used when you want to store information related to a device on the backend without sending it to the device. A good example is customer ID or device location. There is no need to send this data to device if the device doesn’t need this data.
In contrast, desired/reported properties are all the time send/received from device. Properties like device configuration, current status or different counters can be found inside them.
Format
All information related to Device Twins is stored in JSON format. Tags, Desired Properties and Reported Properties are stored separately in the same JSON, as different nodes.
Not all the time, the desired and reported properties will have the same value, we will see why later on.
The keys for properties are case-sensitive and can contain most of the characters (except ‘ ‘, ‘.’, ‘$’, C0 and C1 segments from Unicode control chars).
The values of properties can be any JSON type like string, int, boolean and even object. When you are playing with object you shall remember that the maximum depth of a property is 5. Arrays are not supported, but can be mapped easily as multiple properties.
Tracking of Device Twins
We can see Device Twin as a clone of our Device in the cloud. We can know all the time what is the current state of the device and what configuration was received by the device and what not.
For tracking, we will find under each type of Device Twin a node called $metadata, where information related to when was the last update of each property can be found. All the properties that are added by Azure IoT Hub are beginning with ‘$’ (‘$lastUpaded’, ‘version’).
The $version property is automatically added to each property structure like desired properties. The value of version is incremented with 1 each time when a change is done (a new value is added, updated or removed). Don’t forget that the version value is independently incremented for each structure. This means that the version of desired properties can be at 1400 and the reported property version can be at 4. There is no connection between them.
In the above example we can obsever that the logsFrequency property value is not the same. Also, we have additional one new property in the reported properties - securityLevel.
The maximum size of properties value is 512 bytes. Overall, the maximum size of each structure can be maximum 8KB. 8KB for tags, 8KB for desired properties and 8KB for reported properties.
Actions
From the backend, we can retrieve the device twins based on device ID. Once we get this data, we can add or updates tags and desired properties. The reported properties can be only read on the backend.
At device level, we can retrieve the device twin for our current device, access desired properties and add or update the reported properties. At device level, we can device to be notified when a desired property is update or added (so called observe desired properties). Tags are not accessible on device.
To remove an existing property from tags, desired properties or reported properties we need to set the value to null.
Communication and reconciliation
If we are on backend, the story is simple. We retrieve from Azure IoT Hub the last know device twins, with all the property’s values that are available. There is no magic required on this side.
On the device, the story is a little different. First of all, the communication for device twins is done over the same TLS connection. When a desired value is changed, the notification is sent to the device.
But we shall remember that notification is not queened in the command queue for C2D. It means that even if we have commands in the queue for that specific device, the desired properties update notification will be received by the device – using a different MQTT topic.
This is great, because we can use device twins when we want to send an action with a very high priority – like stopping the device.
The device twin notification is not persisted by Azure IoT Hub. If our device is offline when the notification is send, the notification will not reach the device. It means, that once a device state changes from offline to online, on the device we need to retrieve the desired properties and check the version of it. If the version is higher than the one that we have on the device, it means that one or more properties value changed and we need to check the values.
Conclusion
Device Twins are a powerful feature of Azure IoT Hub. If they are used smart, can change the way how you are using and communicate with your backend. Before jumping to code, try to understand them and what are the main characteristic of each of them.
Remember:
If it is your first time when you hear about Azure IoT Hub, than you should know that Azure IoT Hub is a powerfull serviced offered by Azure that can connect, manage and monitor your devices. More information about it can be found on Azure site
Why Device Twins?
When we are working with devices, we will find all the time information that is specific to individual device. Information like device serial number, customer information, location, device configuration is part of this type of information.
In an ideal world, we should have a unique location where all this data is stored. No custom code of functionality should be developed by us. Azure IoT Hub is offering this to us through Device Twins.
What are Device Twins?
It’s a collection of properties that specific for each individual device. Device twins contains 3 types of structure (where the last two are similar, but have different roles)
- Tags: Device specific information that resides on the Azure IoT Hub and never reach the device
- Desire Properties: Properties set on Azure IoT Hub (by backend) and are delivered to the device
- Reported Properties: Properties set by Device and are delivered to Azure IoT Hub (to backend)
You might find odd that there are 2 types of properties/structures in device twin – tags and desired/reported properties. From the usability point of view, this is useful and handy.
Tags are used when you want to store information related to a device on the backend without sending it to the device. A good example is customer ID or device location. There is no need to send this data to device if the device doesn’t need this data.
In contrast, desired/reported properties are all the time send/received from device. Properties like device configuration, current status or different counters can be found inside them.
Format
All information related to Device Twins is stored in JSON format. Tags, Desired Properties and Reported Properties are stored separately in the same JSON, as different nodes.
"tags": {
"$etag": "32423",
"cliendId": "2323232",
"location": {
"country": "Romania"
"city": "Cluj-Napoca",
"zipCode": "400001"
}
},
"properties": {
"desired": {
"logsFrequency": 1000ms,
"status": 1,
"$metadata" : {...},
"$version": 4
},
"reported": {
"logsFrequency": 1000ms,
"status": 1,
"$metadata" : {...},
"$version": 54
}
}
Not all the time, the desired and reported properties will have the same value, we will see why later on.
The keys for properties are case-sensitive and can contain most of the characters (except ‘ ‘, ‘.’, ‘$’, C0 and C1 segments from Unicode control chars).
The values of properties can be any JSON type like string, int, boolean and even object. When you are playing with object you shall remember that the maximum depth of a property is 5. Arrays are not supported, but can be mapped easily as multiple properties.
Tracking of Device Twins
We can see Device Twin as a clone of our Device in the cloud. We can know all the time what is the current state of the device and what configuration was received by the device and what not.
For tracking, we will find under each type of Device Twin a node called $metadata, where information related to when was the last update of each property can be found. All the properties that are added by Azure IoT Hub are beginning with ‘$’ (‘$lastUpaded’, ‘version’).
The $version property is automatically added to each property structure like desired properties. The value of version is incremented with 1 each time when a change is done (a new value is added, updated or removed). Don’t forget that the version value is independently incremented for each structure. This means that the version of desired properties can be at 1400 and the reported property version can be at 4. There is no connection between them.
"properties": {
"desired": {
"logsFrequency": 1000ms,
"status": 1,
"$metadata" : {...},
"$version": 3
},
"reported": {
"logsFrequency": 5000ms,
"status": 1,
"securityLevel": 9
"$metadata" : {...},
"$version": 94
}
}
In the above example we can obsever that the logsFrequency property value is not the same. Also, we have additional one new property in the reported properties - securityLevel.
The maximum size of properties value is 512 bytes. Overall, the maximum size of each structure can be maximum 8KB. 8KB for tags, 8KB for desired properties and 8KB for reported properties.
Actions
From the backend, we can retrieve the device twins based on device ID. Once we get this data, we can add or updates tags and desired properties. The reported properties can be only read on the backend.
At device level, we can retrieve the device twin for our current device, access desired properties and add or update the reported properties. At device level, we can device to be notified when a desired property is update or added (so called observe desired properties). Tags are not accessible on device.
To remove an existing property from tags, desired properties or reported properties we need to set the value to null.
"properties": {
"desired": {
"logsFrequency": 1000ms,
"status": 1,
"securityLevel": null
"$metadata" : {...},
"$version": 4
},
"reported": {
"logsFrequency": 5000ms,
"status": 1,
"securityLevel": 0
"$metadata" : {...},
"$version": 94
}
}
Communication and reconciliation
If we are on backend, the story is simple. We retrieve from Azure IoT Hub the last know device twins, with all the property’s values that are available. There is no magic required on this side.
On the device, the story is a little different. First of all, the communication for device twins is done over the same TLS connection. When a desired value is changed, the notification is sent to the device.
But we shall remember that notification is not queened in the command queue for C2D. It means that even if we have commands in the queue for that specific device, the desired properties update notification will be received by the device – using a different MQTT topic.
This is great, because we can use device twins when we want to send an action with a very high priority – like stopping the device.
The device twin notification is not persisted by Azure IoT Hub. If our device is offline when the notification is send, the notification will not reach the device. It means, that once a device state changes from offline to online, on the device we need to retrieve the desired properties and check the version of it. If the version is higher than the one that we have on the device, it means that one or more properties value changed and we need to check the values.
Conclusion
Device Twins are a powerful feature of Azure IoT Hub. If they are used smart, can change the way how you are using and communicate with your backend. Before jumping to code, try to understand them and what are the main characteristic of each of them.
Remember:
- Tags can be set, read and accessed only by backend
- Reported Properties are set by device can be read by backend
- Desired Properties are set by backend and can be read by backend
- Use version and lastUpdated properties to detect updates when necessary
Comments
Post a Comment