Simplify IoT Device Updates with Azure Device Update for IoT Hub
December 17, 2024

Simplify IoT Device Updates with Azure Device Update for IoT Hub

When using IoT devices, sooner or later we will encounter a situation where we need to update the software installed on them. These devices may have intermittent connectivity, be distributed around the world, and be difficult to access.

On the last project I worked on, one of the challenges we faced was updating the operating system and software packages on the device without having to access them via SSH. To solve this problem, we leveraged Azure Device Updates from IoT Hub, which allows us to automatically update and remotely install Linux packages or seamlessly execute scripts (from Azure to the edge) without having to interact directly with the device.

In this article, I’ll show you how to set up your device to communicate and receive updates from Azure IoT Hub.


Prerequisites

  • Azure account
  • Azure IoT Hub
  • Initiate an Azure device update for the IoT Hub for the target IoT Hub
  • storage account


Set up device

First, to start using Azure Device Update, we need to install and configure it. In this tutorial, I will be using Ubuntu 22.04. So, let’s get started!

  • Install device updates

wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
sudo apt-get update
sudo apt-get install deviceupdate-agent
Enter full screen mode

Exit full screen mode

sudo nano /etc/adu/du-config.json
Enter full screen mode

Exit full screen mode

{
  "schemaVersion": "1.1",
  "aduShellTrustedUsers": [
    "adu",
    "do"
  ],
  "iotHubProtocol": "mqtt",
  "compatPropertyNames":"manufacturer,model,location,environment" ,
  "manufacturer": ,
  "model": ,
  "agents": [
    {
      "name": ,
      "runas": "adu",
      "connectionSource": {
        "connectionType": "string", //or “AIS”
        "connectionData": 
      },
      "manufacturer": ,
      "model": ,
      "additionalDeviceProperties": {
        "location": "usa",
        "environment": "development"
      }
    }
  ]
}

Enter full screen mode

Exit full screen mode

My test example:

{
  "schemaVersion": "1.1",
  "aduShellTrustedUsers": [
    "adu",
    "do"
  ],
  "iotHubProtocol": "mqtt",
  "manufacturer": "amazingdevice",
  "model": "xyz",
  "agents": [
    {
      "name": "main",
      "runas": "adu",
      "connectionSource": {
        "connectionType": "string",
        "connectionData": "your iot hub connection string"
      },
      "manufacturer": "amazingdevice",
      "model": "xyz"
    }
  ]
}
Enter full screen mode

Exit full screen mode

Finally restart the device update agent:

sudo systemctl restart deviceupdate-agent
Enter full screen mode

Exit full screen mode

  • Tag the device.

  • In the left panel, under Devices, find your IoT device and go to the device twin or module twin (if it’s Azure IoT Edge).

  • exist Device twindelete any existing device update flags by setting their value to null. If you are using device identification with the Device Update Agent, make these changes on the module twin of the Device Update Agent module.

  • The newly added device updates the tag value, as shown in the figure:

"tags": {
        "ADUGroup": ""
    },
Enter full screen mode

Exit full screen mode


Prepare to update

With the device ready to communicate with device updates in IoT Hub, we can finally get to work. We will install a program called exam cowsay is nothing special, the purpose is just to check the installation and running status of this package at the end of the process.


Prepare apt update and import manifest file

We need to prepare two archives to install cowsay from Azure IoT, apt manifest file There is name, version and other information. List import file. The last one should be generated using Azure Cli. example:

owsay-apt-manifest.json

{
    "name": "Install cowsay",
    "version": "1.0.0", 
    "packages": [
        {
            "name": "cowsay",
            "version": "3.03+dfsg2-8"
        }
    ]
}
Enter full screen mode

Exit full screen mode

After creating the apt manifest file, we can use the az command to generate the manifest import file:

az iot du update init v5 --update-provider me --update-name aptcowsay --update-version 1.0.0 --compat manufacturer=amazingdevice model=xyz --step handler=microsoft/apt:1 --file path=C:/Projects/adu/apt/cowsay-apt-manifest.json
Enter full screen mode

Exit full screen mode

Result content:

owsay-apt.importmanifest.json

{
    "compatibility": [
      {
        "manufacturer": "amazingdevice",
        "model": "xyz"
      }
    ],
    "createdDateTime": "2024-12-16T22:59:40Z",
    "files": [
      {
        "filename": "cowsay-apt-manifest.json",
        "hashes": {
          "sha256": "JNYocz3l4Ofwd94l14HT+2GvyHLtmWNMd+KbyYLcRBk="
        },
        "sizeInBytes": 179
      }
    ],
    "instructions": {
      "steps": [
        {
          "files": [
            "cowsay-apt-manifest.json"
          ],
          "handler": "microsoft/apt:1",
          "handlerProperties": {
            "installedCriteria": "1.0"
          },
          "type": "inline"
        }
      ]
    },
    "manifestVersion": "5.0",
    "updateId": {
      "name": "aptcowsay",
      "provider": "me",
      "version": "1.0.0"
    }
  }
Enter full screen mode

Exit full screen mode


Release new updates

After creating the two files required to prepare the update in the Azure portal, the next step is to upload them to your Azure storage account, configure the update, and publish.

  • Upload both files to your Azure storage account;
  • Go to Iot Hub, Device Management, Updates, Import New Update and select the manifest file from your storage account;

  • Select the Groups and Deployments tab and select the group name “adugrouptest” (this is the value of the label in the ADUGroup we defined earlier);

  • As you can see, there is a new update available, click Deploy, select Start Now and build;

  • Next page you can monitor updates

  • When success goes from 0 to 1, click “View Devices” and select your device:

If the process completes successfully, you should see:

Result code: 700

Deployment step results > Result code: 700


View results on device

Now it’s time to verify that the “cowsay” package is indeed installed on our device. To do this, connect to your device and enter: cowsay [some text].


Things to note

Here is a simple example that can be implemented Azure device updates for IoT Hub. In the demo scenario, we show suite-based updates, but it’s also possible to manage image-based and script-based updates, handle larger groups of devices, and explore many other features. For more details, see Official documentation..

2024-12-17 00:26:37

Leave a Reply

Your email address will not be published. Required fields are marked *