Using IoT Hub Direct Methods to Change Device Properties

1.25 hours
  • 3 Learning Objectives

About this Hands-on Lab

The primary purpose for an Azure IoT hub is to be somewhere the devices, or “things,” can send telemetry. But did you know it can talk *back* to the things as well? Standard Tiers of Azure IoT Hub allow for cloud-to-device communication. One of the options by which we can communicate from our back-end applications to our devices is by using direct methods. Direct methods are used for communications that require immediate confirmation of the result. Direct methods are often used for interactive control of devices. In this hands-on lab, we will create a device in a pre-provisioned Azure IoT hub and then use a Node.js application to simulate the physical device. As our “device” sends telemetry information back to the IoT hub, we will use another Node.js application to send a direct method to the device, forcing it to change the interval in which it provides telemetry.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Register the Device with an Azure IoT Hub
  1. Access Azure Cloud Shell using the credentials provided in the lab.

    1. Choose the Bash environment.
  2. Select Show advanced settings.

  3. Use the existing storage account that resides in the resource group location and the existing cloudshell file share name.

  4. Install the Azure IoT extension for CLI:

    az extension add --name azure-iot
  5. Register the device (XXXXX is the five-character suffix for the lab resources):

    az iot hub device-identity create --device-id IoTDevice1 --hub-name iothub-XXXXX
  6. Obtain the connection string for the device application (XXXXX is the five-character suffix for the lab resources):

    az iot hub device-identity show-connection-string --hub-name iothub-XXXXX --device-id IoTDevice1 --output table
  7. Copy the connection string to your clipboard and paste it into a text document.

  8. Obtain the connection string for the back-end application (XXXXX is the five-character suffix for the lab resources):

    az iot hub show-connection-string --policy-name service --name iothub-XXXXX --output table
  9. Copy the connection string to your clipboard and paste it into a text document.

Configure and Launch the Simulated Device
  1. Download the sample Node.js IoT project from Github by typing the following:

    wget https://github.com/Azure-Samples/azure-iot-samples-node/archive/master.zip
  2. Unzip the project into your home directory:

    unzip master.zip
  3. Change to the directory for the simulated device Node.js application:

    cd azure-iot-samples-node-master/iot-hub/Quickstarts/simulated-device-2
  4. Edit the SimulatedDevice.js file, replacing the value of the connectionString variable with the device connection string you made a note of earlier. Save the file.

  5. Install the required libraries and run the simulated device application:

    npm install
    node SimulatedDevice.js
  6. Verify the "device" is sending telemetry data as shown in the following screenshot:

    Device Telemetry

Configure and Launch the Back-End Application
  1. Change to the directory for the back-end Node.js application:

    cd azure-iot-samples-node-master/iot-hub/Quickstarts/back-end-application
    1. Edit the BackEndApplication.js file, replacing the value of the connectionString variable with the service connection string you made a note of earlier, and replacing the value of the deviceId variable to IoTDevice1. Save the file.
  2. Install the required libraries and run the back-end application:

    npm install
    node BackEndApplication.js
  3. Verify the back-end application has changed the telemetry interval for the device to 10 seconds:

    App Telemetry Interval

    Back-end Application Response

    Device Telemetry Interval

    Device Response

Additional Resources

You have recently been hired by Parktronics, a monitoring company that works with local, state, and national park services. They have decided to use Azure IoT to develop Gatekeeper, a solution that will monitor entry and exit gates to ensure they are closed and locked at closing time.

You've been tasked with a proof-of-concept of the Gatekeeper solution. Prior to shipping out a device for use in your proof-of-concept, you decide to use a simulated device to test connectivity to an Azure IoT hub.

You will need to:

  • Register a device with an Azure IoT hub.
  • Configure a Node.js application to act as the simulated device.
  • Send telemetry data to the Azure IoT hub from the simulated device.
  • Configure a second Node.js application as a back-end application to change the telemetry interval of the device via a direct method.

If you get stuck, feel free to check out the solution video or the detailed instructions under each objective. Good luck!

Tools

You will be using the Azure CLI to perform most of the tasks in this lab. You can access the CLI by using Azure Cloud Shell. When accessing the shell, select Show advanced settings, and use the existing storage account that resides in the West US location.

We will be using the sample IoT Node.js project provided by Microsoft to simulate the IoT device as well as the back-end application. This project can be downloaded from GitHub.

Lab Suffix

Pre-deployed lab resources will all end with a unique five-character suffix. Make note of this suffix, as you may need it when naming resources during the lab.

WARNING: Be Prepared for UI Changes

Given the fluid nature of Microsoft cloud tools, you may experience user interface (UI) changes that were made following the development of this hands-on lab that do not match up with lab instructions. When any such changes are brought to our attention, we will attempt to update the content accordingly. However, if changes occur, students will have to adapt to the changes and work through them in the hands-on labs as needed.

What are Hands-on Labs

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.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?