To myself...

A Brief History of Time

Before the idea of device twin, it is historically difficult to query the device information in the field. A lot of network hops or even database synchronisation can happen to retrieve this information. But looking closer at the problem, most of the use case is to show the information in a dashboard for monitoring or controlling certain assets of an organisation - there's no hard real-time requirement. Hence, with this clarity of the problem, the device twin was born in Azure.

With device twin, the backend services can interact with Azure IoT Hub to query the metadata and data of each devices. This capability enables scenarios like device reporting on dashboards or monitoring long-running jobs across many devices.

It is important to note that because of the nature of device twin updates is asynchronous in nature, it is not guaranteed that the values you get from the query are real-time. What you will actually read are the last reported values. Most organisations will accept this latency for the majority of their use cases.

What is Device Twin?

A device twin is a json document that contains device-specific information. The document has a size limit of 8KB and a maximum depth of 5 levels. The format is:

"identity": {   ... }, "tags": {    ... }, "properties": {    "desired": {       "status": <>,       "$version": <>,    },    "reported": {       "status": <>,       "$version": <>,    }, },

Field permissions during device twin interaction

field backend service device
identity r none
tags rw none
desired properties rw r
reported properties r rw

Example of device twin json file content

{    "deviceId":"device-2222",    "etag":"AAAAAAAAAAc=",    "status":"enabled",    "statusReason":"provisioned",    "statusUpdateTime":"0001-01-01T00:00:00",    "connectionState":"connected",    "lastActivityTime":"2015-02-30T16:24:48.789Z",    "cloudToDeviceMessageCount":0,    "authenticationType":"sas",    "x509Thumbprint":{       "primaryThumbprint":null,       "secondaryThumbprint":null    },    "version":2,    "tags":{       "$etag":"123",       "deploymentLocation":{          "building":"43",          "floor":"1"       }    },    "properties":{       "desired":{          "telemetryConfig":{             "sendFrequency":"5m"          },          "$metadata":{             "..."          },          "$version":1       },       "reported":{          "telemetryConfig":{             "sendFrequency":"5m",             "status":"success"          }"batteryLevel":55,          "$metadata":{             "..."          },          "$version":4       }    } }

This free site is ad-supported. Learn more