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(...

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine: threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration: TeamCity .NET 4.51 EF 6.0.2 VS2013 It see...

Navigating Cloud Strategy after Azure Central US Region Outage

 Looking back, July 19, 2024, was challenging for customers using Microsoft Azure or Windows machines. Two major outages affected customers using CrowdStrike Falcon or Microsoft Azure computation resources in the Central US. These two outages affected many people and put many businesses on pause for a few hours or even days. The overlap of these two issues was a nightmare for travellers. In addition to blue screens in the airport terminals, they could not get additional information from the airport website, airline personnel, or the support line because they were affected by the outage in the Central US region or the CrowdStrike outage.   But what happened in reality? A faulty CrowdStrike update affected Windows computers globally, from airports and healthcare to small businesses, affecting over 8.5m computers. Even if the Falson Sensor software defect was identified and a fix deployed shortly after, the recovery took longer. In parallel with CrowdStrike, Microsoft provi...