Skip to content

Latest commit

 

History

History
111 lines (69 loc) · 5.88 KB

File metadata and controls

111 lines (69 loc) · 5.88 KB
author SoniaLopezBravo
ms.author sonialopez
ms.service azure-iot-hub
services iot-hub
ms.devlang nodejs
ms.topic include
ms.date 03/28/2025
ms.custom
mvc
mqtt
Role: Cloud Development
devx-track-azurecli
sfi-ropc-nochange

This quickstart uses two Node.js applications:

  • A simulated device application that responds to direct methods called from a back-end application. To receive the direct method calls, this application connects to a device-specific endpoint on your IoT hub.
  • A back-end application that calls the direct methods on the simulated device. To call a direct method on a device, this application connects to a service-specific endpoint on your IoT hub.

Prerequisites

  • An Azure account with an active subscription. Create one for free.

  • Node.js 12+.

    You can verify the current version of Node.js on your development machine using the following command:

    node --version
    
  • Clone or download the Azure IoT Node.js samples from GitHub.

  • Make sure that port 8883 is open in your firewall. The device sample in this quickstart uses MQTT protocol, which communicates over port 8883. This port might be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see the Connect to IoT Hub section of Communicate with an IoT hub using the MQTT protocol.

[!INCLUDE azure-cli-prepare-your-environment.md]

[!INCLUDE iot-hub-cli-version-info]

Create an IoT hub

[!INCLUDE iot-hub-include-create-hub-cli]

Register a device

[!INCLUDE iot-hub-include-create-device-cli]

Retrieve the service connection string

You also need your IoT hub's service connection string to enable the back-end application to connect to your IoT hub and retrieve the messages. The following command retrieves the service connection string for your IoT hub:

YourIoTHubName: Replace this placeholder in the following command with the name you chose for your IoT hub.

az iot hub connection-string show \
  --policy-name service --hub-name {YourIoTHubName} --output table

Make a note of the service connection string, which looks like:

HostName={YourIoTHubName}.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey={YourSharedAccessKey}

You use this value later in the quickstart. This service connection string is different from the device connection string you noted in the previous step.

Simulate a device

The simulated device application connects to a device-specific endpoint on your IoT hub, sends simulated telemetry, and listens for direct method calls from your hub. In this quickstart, the direct method call from the hub tells the device to change the interval at which it sends telemetry. The simulated device sends an acknowledgment back to your hub after it executes the direct method.

  1. In a local terminal window, navigate to the root folder of the sample Node.js project. Then navigate to the iot-hub\Quickstarts\simulated-device-2 folder.

  2. Open the SimulatedDevice.js file in a text editor of your choice.

    Replace the value of the connectionString variable with the device connection string you made a note of earlier. Then save your changes to SimulatedDevice.js.

  3. In the local terminal window, run the following commands to install the required libraries and run the simulated device application:

    npm install
    node SimulatedDevice.js
    

    The following screenshot shows the output as the simulated device application sends telemetry to your IoT hub:

    :::image type="content" source="./media/quickstart-control-device-node/simulated-device-telemetry-iot-hub.png" alt-text="Screenshot of a terminal window that shows the output of the simulated device application.":::

Call the direct method

The back-end application connects to a service-side endpoint on your IoT hub. The application makes direct method calls to a device through your IoT hub and listens for acknowledgments. An IoT Hub back-end application typically runs in the cloud.

  1. In another local terminal window, navigate to the root folder of the sample Node.js project. Then navigate to the iot-hub\Quickstarts\back-end-application folder.

  2. Open the BackEndApplication.js file in a text editor of your choice.

    Replace the value of the connectionString variable with the service connection string you made a note of earlier. Then save your changes to BackEndApplication.js.

  3. In the local terminal window, run the following commands to install the required libraries and run the back-end application:

    npm install
    node BackEndApplication.js
    

    The following screenshot shows the output as the application makes a direct method call to the device and receives an acknowledgment:

    :::image type="content" source="./media/quickstart-control-device-node/direct-method-device-call.png" alt-text="Screenshot of a terminal window that shows the result of the direct method call from the service application.":::

    After you run the back-end application, you see a message in the local terminal window running the simulated device, and the rate at which it sends messages changes:

    :::image type="content" source="./media/quickstart-control-device-node/simulated-device-message-change.png" alt-text="Screenshot of a terminal window that shows the direct message result and updated output from the simulated device application.":::