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

Using IoT Hub Direct Methods to Change Device Properties

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.

Azure icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 15m
Published
Clock icon Apr 24, 2020

Contact sales

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

Table of Contents

  1. Challenge

    Register the Device with an Azure IoT Hub

    1. Access Azure Cloud Shell using the credentials provided in the lab.

    2. Choose the Bash environment.

    3. Select Show advanced settings.

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

    5. Install the Azure IoT extension for CLI:

      az extension add --name azure-iot
      
    6. 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
      
    7. Obtain the connection string for the device application (XXXXX is the five-character suffix for the lab resources):

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

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

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

  2. Challenge

    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

  3. Challenge

    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

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