Introduction

In the series of Azure IoT basics (Internet of Things), we have covered all the basic details about the IoT and related terminology, including the Azure IoT Hub, device provisioning service, and related authentication. Now, in this article, we will learn how to control and manage the IoT device lifecycle with the help of device twin properties. If you want to read the previous articles, then please read them using the following links.

What is the Azure IoT Hub Device Twin?

Azure IoT Hub Device Twins are the properties for storing and retrieving metadata associated with an IoT device, commonly referred to as its "twin. This information can be used to configure and manage the device, monitor its status, and troubleshoot issues.

In this article, we will take a closer look at Azure IoT Hub Device Twin with an example of how it can be used in a real-world scenario. Every device has the following set of twin properties that describe the current state of the device, as well as a set of desired properties that specify the desired state of the device.

Azure IoT Hub Device

Let's learn about the properties and their uses. The real-time device twin section of the device metadata in the IoT Hub looks like the following image representation.

Device metadata

Let's learn about each property in detail.

Desired Properties

Desired properties in the Azure IoT Hub Device twins are like instructions for an IoT device. These are the settings and configurations that the application wants the device to follow. If the application needs to change these settings, it can send a message to the IoT hub. The device then reads these new instructions and adjusts its behavior accordingly. So, it's a way for the application to remotely control and tweak how the IoT device works.

Let's break it down into simpler terms to understand the desired properties.

What are they?

How Does It Work?

What Can You Do?

Flexibility

How do Devices understand?

JSON Structure for Desired Properties

Let's consider a scenario where an IoT device is attached to a moving truck transporting GoPure Sweets from the factory to the shop's outlet. The desired properties can be configured to monitor and manage various aspects of the truck's operation, ensuring the synchronization of various aspects during the journey. Here's an example JSON structure for such desired properties.

{
  "desired": {
    "temperatureThreshold": 5,
    "humidityThreshold": 80,
    "samplingInterval": 300,
    "truckSpeedLimit": 60,
    "destination": "B1-K08,Galaxy Blue Sapphire Plaza,Noida, UP"
  }
}

Explanation of preceding each desired property.

TemperatureThreshold

HumidityThreshold

SamplingInterval

TruckSpeedLimit

Destination

In this scenario, these desired properties provide a comprehensive configuration for monitoring and managing the transportation of Gopure sweets, covering temperature control, humidity levels, data reporting frequency, speed limits, and the destination of the truck.

{
  "desired": {
    "temperature": 27,
    "thermostatMode": "Heat"
  },
  "reported": {
    "temperature": 22,
    "thermostatMode": "Cool",
    "batteryLevel": 0.75,
    "firmwareVersion": "6.0.0"
  },
  "tags": {
    "location": "MCN-H-217-Noida-Bldg",
    "deviceType": "Thermostat"
  }
}

Key points about Desired Properties

Reported Properties

The reported properties in Azure IoT Hub Device Twins are the device's current state and configurations. These are the properties that the device is reporting back to the IoT hub, and they can be used to monitor the status and health of the device.

Twin Tags

Azure IoT Hub Device Twins also support a feature called "Twin Tags," which allows you to add metadata to the Device Twin properties, including the desired and reported properties. These tags can be used to filter, query, and identify the devices in your solution.

Key Points: IoT Hub Device Twin

Summary

I hope the preceding explanations helped you understand that Azure IoT Device Twins are a powerful feature that allows developers to synchronize the state of their IoT devices with the cloud. Each device twin contains a set of properties that describe the current state of the device, as well as a set of desired properties that specify the desired state of the device. The desired and reported properties can be updated at any time, making it easy to keep track of the state of your devices and take appropriate actions.

Related Articles