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

Change Device Properties with IoT Hub Direct Methods

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 backend applications to our devices is by using direct methods. Direct methods are used for communications that require immediate confirmation of the result and are often used for interactive control of devices. In this hands-on lab, we will create a device in a preprovisioned 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 a feature in the Azure portal to send a direct method to the device, forcing it to change the interval by which it provides telemetry.

Azure icon

Path Info

Clock icon Intermediate
Clock icon 1h 0m
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 a Device in Azure IoT Hub

    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 a device. Name the device whatever you choose.
    4. Copy the Primary Connection String from the device to a text document.

      Tip: Make sure you copy the Primary Connection String and not just the Primary Key.

  2. Challenge

    Configure and Run 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 Advanced settings and add the storage account and file share. Add a new storage account. Do not use an existing one. 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, below. This sample application acts as a simulated device. it includes a method that handles the direct method call from the hub or another backend application.
      git clone 
    5. Change to the directory for the Device Twin lab:
      cd azure-iot-samples-node/iot-hub/Quickstarts/simulated-device-2
    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: azure-iot-samples-node/iot-hub/Quickstarts/simulated-device-2

      Note: Make sure you are in the right directory. There are duplicate files with the same names in other directories — but with different code.

    8. Open SimulatedDevice.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.

    9. You now need to find the name of the method name that you can use to send a remote command using a direct method in the Azure IoT Hub.


      • The purpose of the method is to change the interval by which telemetry is sent. It currently defaults to 1 second.
      • There are no parameters on the method.
      • You are looking for a handler of the direct method call from the backend application. The code does not directly expose the method.
    10. Once you find the method, copy and paste it into a text document for use later.
    11. Save your changes. > Tip: Look for the "..." in the upper right of the editor window to save changes.
    12. Back in the command prompt, run the following command to launch the application and run your simulated device:
      node SimulatedDevice.js

    If the device has successfully connected to the hub, you will see telemetry flowing in the terminal window.

  3. Challenge

    Execute a Direct Method on the Simulated Device

    1. Leave the terminal window open and the simulated device running in a big enough pane to see the telemetry flowing.
    2. Return to the device registration you created in the Azure portal, and execute a direct method on the device, using the method name you found in the code. Recall the purpose of the method is to change the telemetry interval from the default of 1 second, so the payload for the method is the digit 5.
    3. Verify that the messages in the terminal have slowed down substantially.
    4. Execute the direct method again, using 0.5 for the payload.
    5. Verify that the messages in the terminal have sped up substantially.
    6. Feel free to try other values.
    7. When you are done, you can stop the code from running by returning to the terminal and executing CTRL-C.

      Tip: Suppose the direct method name you found in the code was runMe, then you would use runMe for the method name and the digit 5 for the payload — not runMe(5).

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