Skip to main content

Why Device Twins from Azure IoT Hub are important for us

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)

  • 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:

  1. Tags can be set, read and accessed only by backend 
  2. Reported Properties are set by device can be read by backend
  3. Desired Properties are set by backend and can be read by backend
  4. Use version and lastUpdated properties to detect updates when necessary


Comments

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(

Azure AD and AWS Cognito side-by-side

In the last few weeks, I was involved in multiple opportunities on Microsoft Azure and Amazon, where we had to analyse AWS Cognito, Azure AD and other solutions that are available on the market. I decided to consolidate in one post all features and differences that I identified for both of them that we should need to take into account. Take into account that Azure AD is an identity and access management services well integrated with Microsoft stack. In comparison, AWS Cognito is just a user sign-up, sign-in and access control and nothing more. The focus is not on the main features, is more on small things that can make a difference when you want to decide where we want to store and manage our users.  This information might be useful in the future when we need to decide where we want to keep and manage our users.  Feature Azure AD (B2C, B2C) AWS Cognito Access token lifetime Default 1h – the value is configurable 1h – cannot be modified

What to do when you hit the throughput limits of Azure Storage (Blobs)

In this post we will talk about how we can detect when we hit a throughput limit of Azure Storage and what we can do in that moment. Context If we take a look on Scalability Targets of Azure Storage ( https://azure.microsoft.com/en-us/documentation/articles/storage-scalability-targets/ ) we will observe that the limits are prety high. But, based on our business logic we can end up at this limits. If you create a system that is hitted by a high number of device, you can hit easily the total number of requests rate that can be done on a Storage Account. This limits on Azure is 20.000 IOPS (entities or messages per second) where (and this is very important) the size of the request is 1KB. Normally, if you make a load tests where 20.000 clients will hit different blobs storages from the same Azure Storage Account, this limits can be reached. How we can detect this problem? From client, we can detect that this limits was reached based on the HTTP error code that is returned by HTTP