Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Azure icon
Labs

Leverage Automatic Device Management with IoT Device Twins in Azure

Managing 1 or 2 IoT devices is really no big deal. But what if you had 100 devices? 1000? Then, things get a little more interesting — and by interesting, we mean excruciatingly painful! But help is on the way, in the form of automatic device management (ADM) in Azure IoT Hub. We can use this handy feature to manage properties and tags on hundreds of IoT devices at the same time! In this lab you will: - Register 2 devices with Azure IoT Hub and edit the JSON device twins. - Set up an ADM configuration to target 1 or more devices, based on device twin properties. - Download, modify, and run a Node.js app that simulates an IoT device. - Verify that the ADM configuration properly updated the targeted device(s).

Azure icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 45m
Published
Clock icon Feb 25, 2022

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Register and Configure Devices in Azure IoT Hub

    NOTE: As an additional challenge, the precise JSON you should add to each twin is not provided here. Check the lab guide to see what the JSON should look like in the device twins once you have completed step 4, below.

    1. Open the link for this lab in an incognito or InPrivate browser window.
    2. Enter the credentials assigned to this lab (they will be different from the credentials you see in the solution video; each student gets a unique lab session and credentials).
    3. Navigate to the only IoT Hub already deployed, and register 2 devices. You can name the devices whatever you choose, but in the video and lab guide, we refer to them as myTargetDevice and myOtherDevice to make their purposes more clear .
    4. In each device twin, add a tags section and a location tag to the device twin, just before the desired properties section. Tip: Don't forget the comma after closing bracket of the tags object.
      • On one device, assign "Green River" to the location.
      • On the second device, assign "Rock Springs" to the location.
    5. Copy the Primary Connection String from the "Green River" device to a text document. Tip: Make sure you copy the Primary Connection String and not just the primary key.
  2. Challenge

    Create an Automatic Device Management Configuration

    NOTE: As an additional challenge, the exact syntax for the desired twin settings and target device query are not provided, here. Check the lab guide for the proper syntax on both steps 2 and 3, below. Also, the order of steps in the wizard may change, so just ensure you cover all the settings, regardless of the order in which they are requested.

    1. In the Azure IoT Hub that contains your 2 devices, navigate to Configurations + Deployments, and add a new device twin configuration.
    2. Set the priority to 10.
    3. Set a desired property setting for conveyorCabinet with a maxTemp of 89 and a maxHumidity reading of 40. Hint: There is no set schema for device twins; it all depends on the application. In this case, the simulated device app will be looking under the properties.desired object for another object called settings, which holds the conveyorCabinet object and its 2 key-value pairs for maxTemp and maxHumidity. The object names are all lowercase.
    4. Create a query to target any devices with the location tag assigned to "Green River."
    5. Review and save the configuration.
  3. Challenge

    Verify ADM Configuration Targeted the Correct Device

    NOTE: As an additional challenge, the JSON you should see in the twin is not provided, here. Check the lab guide to see what the JSON should look like in the targeted device.

    1. Navigate to the device twin for each of the 2 devices you created in the first objective.
    2. Verify that the desired properties are updated for only the device located in Green River and not the device located in Rock Springs.
  4. Challenge

    Run the Simulated Device in Cloud Shell

    1. Launch Cloud Shell from within the Azure Portal, choosing the Bash environment in order to use Azure CLI.

    2. When prompted to initialize storage for Cloud Shell, select Show advanced settings, and add the storage account and file share. Note that the storage account name must be universally unique; you might want to use the random characters at the beginning of the resource group name as a part of your storage account name.

    3. Install the Azure IoT extension for Azure CLI:

      az extension add --name azure-iot
      
    4. Download the sample Node.js application by cloning the Git repository for this course, below. This sample application acts as a simulated device. Once connected to the hub, it reads the desired properties from the device twin and echos back the desired properties as reported properties on the twin. This is how we use device twins to synchronize desired properties (from the backend application) with the reported properties (maintained by the IoT devices).

      git clone https://github.com/linuxacademy/content-AZ-220.git
      
    5. Change to the directory for the Device Twins lab:

      cd content-AZ-220/samples/DeviceTwins/
      
    6. Install the Node dependencies for the sample application:

      npm install
      
    7. In the top menu of the Cloud Shell terminal window, select the curly brackets icon to open the editor. Navigate to this directory: content-AZ-220/samples/DeviceTwins.

    8. Open test_device_twins.js and find the connectionString variable, near the top of the code. Replace the value assigned to that variable with the device connection string you copied in the first objective.

    Tip: Make sure the value is wrapped in single quotes.

    1. Save your changes.

    Tip: Look for the "..." in the upper right of the editor window to save changes. You can also use CTRL-s.

    1. Back at the command prompt, run the following command to launch the application and run your simulated device:
      node test_device_twins.js
      

      There will be output regarding the progress of the application. Once the application has reported successful changes to reported properties, exit it by pressing CTRL+C.

  5. Challenge

    Verify the Targeted Device Updated the Twin's Reported Properties

    NOTE: As an additional challenge, the JSON you should see in the twin is not provided, here. Check the lab guide to see what the JSON should look like in the targeted device.

    1. Navigate to the device twin for each of the 2 devices you created in the first objective.
    2. Verify that the reported properties are updated for only the device located in Green River and not the device located in Rock Springs.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans