Quickstart: Send telemetry from a device to Azure IoT Hub

Applies to: Device application developers

In this quickstart, you learn a basic Azure IoT application development workflow. You use the Azure CLI to create an Azure IoT hub and a device. Then you use an Azure IoT device SDK sample to run a simulated temperature controller, connect it securely to the hub, and send telemetry.

Prerequisites

  • If you don't have an Azure subscription, create one for free before you begin.
  • Git.
  • Azure CLI. You have two options for running Azure CLI commands in this quickstart:
    • Use the Azure Cloud Shell, an interactive shell that runs CLI commands in your browser. This option is recommended because you don't need to install anything. If you're using Cloud Shell for the first time, log into the Azure portal. Follow the steps in Cloud Shell quickstart to Start Cloud Shell and Select the Bash environment.
    • Optionally, run Azure CLI on your local machine. If Azure CLI is already installed, run az upgrade to upgrade the CLI and extensions to the current version. To install Azure CLI, see Install Azure CLI.

Install the remaining prerequisites for your operating system.

Linux

The steps in this tutorial were tested using Ubuntu Linux 18.04.

To complete this quickstart on Linux, install the following software on your local Linux environment:

Install GCC, Git, cmake, and the required dependencies using the apt-get command:

sudo apt-get update
sudo apt-get install -y git cmake build-essential curl libcurl4-openssl-dev libssl-dev uuid-dev

Verify the version of cmake is above 2.8.12 and the version of GCC is above 4.4.7.

cmake --version
gcc --version

Windows

To complete this quickstart on Windows, install Visual Studio 2019 and add the required components for C and C++ development.

  1. For new users, install Visual Studio (Community, Professional, or Enterprise) 2019. Download the edition you want to install, and start the installer.

    Note

    For existing Visual Studio 2019 users, select Windows Start, type Visual Studio Installer, and start the installer.

  2. In the installer Workloads tab, select the Desktop Development with C++ workload.
  3. Run the installation.

Create an IoT hub

In this section, you use Azure CLI to create an IoT hub and a resource group. An Azure resource group is a logical container into which Azure resources are deployed and managed. An IoT hub acts as a central message hub for bi-directional communication between your IoT application and the devices.

To create an IoT hub and a resource group:

  1. Launch your CLI app. To run the CLI commands in the rest of this quickstart, copy the command syntax, paste it into your CLI app, edit variable values, and press Enter.

    • If you're using Cloud Shell, select the Try It button on the CLI commands to launch Cloud Shell in a split browser window. Or you can open the Cloud Shell in a separate browser tab.
    • If you're using Azure CLI locally, start your CLI console app and sign in to Azure CLI.
  2. Run az extension add to install or upgrade the azure-iot extension to the current version.

    az extension add --upgrade --name azure-iot
    
  3. In your CLI app, run the az group create command to create a resource group. The following command creates a resource group named MyResourceGroup in the eastus location.

    Note

    You can optionally set an alternate location. To see available locations, run az account list-locations. This tutorial uses eastus as shown in the example command.

    az group create --name MyResourceGroup --location eastus
    
  4. Run the az iot hub create command to create an IoT hub. It might take a few minutes to create an IoT hub.

    YourIotHubName. Replace this placeholder and the surrounding braces in the following command, using the name you chose for your IoT hub. An IoT hub name must be globally unique in Azure. Use your IoT hub name in the rest of this quickstart wherever you see the placeholder.

    az iot hub create --resource-group MyResourceGroup --name {YourIoTHubName}
    

    Tip

    After creating an IoT hub, in the rest of this quickstart you can continue to use CLI commands to interact with the hub. Optionally, you could use Azure IoT Explorer in place of the CLI commands. IoT Explorer is a GUI application that lets you connect to an existing IoT Hub and add, manage, and monitor devices. To learn more, see Install and use Azure IoT explorer.

Create a simulated device

In this section, you create a simulated IoT device that is connected to your IoT hub.

To create a simulated device:

  1. Run the az iot hub device-identity create command in your CLI shell. This creates the simulated device identity.

    YourIotHubName. Replace this placeholder below with the name you chose for your IoT hub.

    myDevice. You can use this name directly for the simulated device ID in the rest of this article. Optionally, use a different name.

    az iot hub device-identity create --device-id myDevice --hub-name {YourIoTHubName} 
    
  2. Run the az iot hub device-identity connection-string show command.

    az iot hub device-identity connection-string show --device-id myDevice --hub-name {YourIoTHubName}
    

    The connection string output is in the following format:

    HostName=<your IoT Hub name>.azure-devices.net;DeviceId=<your device id>;SharedAccessKey=<some value>
    
  3. Save the connection string in a secure location.

Note

Keep your CLI app open. You'll use it in later steps.

Run a simulated device

In this section, you use the C SDK to send messages from your simulated device to your IoT hub.

Build the sample

  1. Open a console to install the Azure IoT C device SDK, and run the code sample. For Windows, select Start, type Developer Command Prompt for VS 2019, and open the console. For Linux, open Bash.

    Note

    You should now have two console windows open: the one you just opened, and the Cloud Shell or CLI console that you used previously to enter CLI commands.

  2. In your C console, clone the Azure IoT C device SDK to your local machine:

    git clone https://github.com/Azure/azure-iot-sdk-c.git
    
  3. Navigate to the root folder of the SDK, and run the following command to update dependencies:

    cd azure-iot-sdk-c
    git submodule update --init
    

    This operation takes a few minutes.

  4. To build the SDK and samples, run the following commands:

    cmake -Bcmake -Duse_prov_client=ON -Dhsm_type_symm_key=ON -Drun_e2e_tests=OFF
    cmake --build cmake
    
  5. Set the following environment variables, to enable your simulated device to connect to Azure IoT.

    • Set an environment variable called IOTHUB_DEVICE_CONNECTION_STRING. For the variable value, use the device connection string that you saved in the previous section.
    • Set an environment variable called IOTHUB_DEVICE_SECURITY_TYPE. For the variable, use the literal string value connectionString.

    CMD

    set IOTHUB_DEVICE_CONNECTION_STRING=<your connection string here>
    set IOTHUB_DEVICE_SECURITY_TYPE=connectionString
    

    Note

    For Windows CMD there are no quotation marks surrounding the string values for each variable.

    Bash

    export IOTHUB_DEVICE_CONNECTION_STRING="<your connection string here>"
    export IOTHUB_DEVICE_SECURITY_TYPE="connectionString"
    

Run the code

  1. In your CLI app, run the az iot hub monitor-events command to begin monitoring for events on your simulated IoT device. Event messages print in the terminal as they arrive.

    az iot hub monitor-events --output table --hub-name {YourIoTHubName}
    
  2. Run the sample code, using the appropriate command for your console:

    CMD

    cmake\iothub_client\samples\pnp\pnp_temperature_controller\Debug\pnp_temperature_controller.exe
    

    Bash

    cmake/iothub_client/samples/pnp/pnp_temperature_controller/Debug/pnp_temperature_controller
    

    Note

    This code sample uses Azure IoT Plug and Play, which lets you integrate smart devices into your solutions without any manual configuration. By default, most samples in this documentation use IoT Plug and Play. To learn more about the advantages of IoT PnP, and cases for using or not using it, see What is IoT Plug and Play?.

    After your simulated device connects to your IoT Central application, it connects to the device instance you created in the application and begins to send telemetry. The connection details and telemetry output are shown in your console:

    Starting event monitor, use ctrl-c to stop...
    event:
      component: ''
      interface: dtmi:com:example:TemperatureController;1
      module: ''
      origin: myDevice
      payload: '{"workingSet":1251}'
    
    event:
      component: thermostat1
      interface: dtmi:com:example:TemperatureController;1
      module: ''
      origin: myDevice
      payload: '{"temperature":22.00}'
    

Prerequisites

Create an IoT hub

In this section, you use Azure CLI to create an IoT hub and a resource group. An Azure resource group is a logical container into which Azure resources are deployed and managed. An IoT hub acts as a central message hub for bi-directional communication between your IoT application and the devices.

To create an IoT hub and a resource group:

  1. Launch your CLI app. To run the CLI commands in the rest of this quickstart, copy the command syntax, paste it into your CLI app, edit variable values, and press Enter.

    • If you're using Cloud Shell, select the Try It button on the CLI commands to launch Cloud Shell in a split browser window. Or you can open the Cloud Shell in a separate browser tab.
    • If you're using Azure CLI locally, start your CLI console app and sign in to Azure CLI.
  2. Run az extension add to install or upgrade the azure-iot extension to the current version.

    az extension add --upgrade --name azure-iot
    
  3. In your CLI app, run the az group create command to create a resource group. The following command creates a resource group named MyResourceGroup in the eastus location.

    Note

    You can optionally set an alternate location. To see available locations, run az account list-locations. This tutorial uses eastus as shown in the example command.

    az group create --name MyResourceGroup --location eastus
    
  4. Run the az iot hub create command to create an IoT hub. It might take a few minutes to create an IoT hub.

    YourIotHubName. Replace this placeholder and the surrounding braces in the following command, using the name you chose for your IoT hub. An IoT hub name must be globally unique in Azure. Use your IoT hub name in the rest of this quickstart wherever you see the placeholder.

    az iot hub create --resource-group MyResourceGroup --name {YourIoTHubName}
    

    Tip

    After creating an IoT hub, in the rest of this quickstart you can continue to use CLI commands to interact with the hub. Optionally, you could use Azure IoT Explorer in place of the CLI commands. IoT Explorer is a GUI application that lets you connect to an existing IoT Hub and add, manage, and monitor devices. To learn more, see Install and use Azure IoT explorer.

Create a simulated device

In this section, you create a simulated IoT device that is connected to your IoT hub.

To create a simulated device:

  1. Run the az iot hub device-identity create command in your CLI shell. This creates the simulated device identity.

    YourIotHubName. Replace this placeholder below with the name you chose for your IoT hub.

    myDevice. You can use this name directly for the simulated device ID in the rest of this article. Optionally, use a different name.

    az iot hub device-identity create --device-id myDevice --hub-name {YourIoTHubName} 
    
  2. Run the az iot hub device-identity connection-string show command.

    az iot hub device-identity connection-string show --device-id myDevice --hub-name {YourIoTHubName}
    

    The connection string output is in the following format:

    HostName=<your IoT Hub name>.azure-devices.net;DeviceId=<your device id>;SharedAccessKey=<some value>
    
  3. Save the connection string in a secure location.

Note

Keep your CLI app open. You'll use it in later steps.

Run a simulated device

In this section, you configure your local environment, and run a sample that creates a simulated temperature controller.

To run the sample application in Visual Studio:

  1. In the folder where you unzipped the Azure IoT Samples for C#, open the azure-iot-samples-csharp-master\iot-hub\Samples\device\IoTHubDeviceSamples.sln" solution file in Visual Studio.

  2. In Solution Explorer, select the PnpDeviceSamples > TemperatureController project file, right-click it, and select Set as Startup Project.

  3. Right-click the TemperatureController project, select Properties, select the Debug tab, and add the following environment variables to the project:

    Name Value
    IOTHUB_DEVICE_SECURITY_TYPE connectionString
    IOTHUB_DEVICE_CONNECTION_STRING The connection string you saved previously.
  4. Save the updated TemperatureController project file.

  5. In your CLI app, run the az iot hub monitor-events command to begin monitoring for events on your simulated IoT device. Event messages print in the terminal as they arrive.

    az iot hub monitor-events --output table --hub-name {YourIoTHubName}
    
  6. In Visual Studio, press CTRL + F5 to run the sample.

    After your simulated device connects to your IoT Central application, it begins to send telemetry. The connection details and telemetry output appear in the console:

    Starting event monitor, use ctrl-c to stop...
    event:
      component: thermostat1
      interface: dtmi:com:example:TemperatureController;2
      module: ''
      origin: myDevice
      payload:
        temperature: 39.8
    
    event:
      component: thermostat2
      interface: dtmi:com:example:TemperatureController;2
      module: ''
      origin: myDevice
      payload:
        temperature: 36.7
    

Prerequisites

  • If you don't have an Azure subscription, create one for free before you begin.
  • Git.
  • A development machine with Java SE Development Kit 8 or later. You can download the Java 8 (LTS) JDK for multiple platforms from Download Zulu Builds of OpenJDK. In the installer, select the Add to Path option.
  • Apache Maven 3. After you extract the download to a local folder, add the full path to the Maven /bin folder to the Windows PATH variable.
  • Azure CLI. You have two options for running Azure CLI commands in this quickstart:
    • Use the Azure Cloud Shell, an interactive shell that runs CLI commands in your browser. This option is recommended because you don't need to install anything. If you're using Cloud Shell for the first time, log into the Azure portal. Follow the steps in Cloud Shell quickstart to Start Cloud Shell and Select the Bash environment.
    • Optionally, run Azure CLI on your local machine. If Azure CLI is already installed, run az upgrade to upgrade the CLI and extensions to the current version. To install Azure CLI, see Install Azure CLI.

Create an IoT hub

In this section, you use Azure CLI to create an IoT hub and a resource group. An Azure resource group is a logical container into which Azure resources are deployed and managed. An IoT hub acts as a central message hub for bi-directional communication between your IoT application and the devices.

To create an IoT hub and a resource group:

  1. Launch your CLI app. To run the CLI commands in the rest of this quickstart, copy the command syntax, paste it into your CLI app, edit variable values, and press Enter.

    • If you're using Cloud Shell, select the Try It button on the CLI commands to launch Cloud Shell in a split browser window. Or you can open the Cloud Shell in a separate browser tab.
    • If you're using Azure CLI locally, start your CLI console app and sign in to Azure CLI.
  2. Run az extension add to install or upgrade the azure-iot extension to the current version.

    az extension add --upgrade --name azure-iot
    
  3. In your CLI app, run the az group create command to create a resource group. The following command creates a resource group named MyResourceGroup in the eastus location.

    Note

    You can optionally set an alternate location. To see available locations, run az account list-locations. This tutorial uses eastus as shown in the example command.

    az group create --name MyResourceGroup --location eastus
    
  4. Run the az iot hub create command to create an IoT hub. It might take a few minutes to create an IoT hub.

    YourIotHubName. Replace this placeholder and the surrounding braces in the following command, using the name you chose for your IoT hub. An IoT hub name must be globally unique in Azure. Use your IoT hub name in the rest of this quickstart wherever you see the placeholder.

    az iot hub create --resource-group MyResourceGroup --name {YourIoTHubName}
    

    Tip

    After creating an IoT hub, in the rest of this quickstart you can continue to use CLI commands to interact with the hub. Optionally, you could use Azure IoT Explorer in place of the CLI commands. IoT Explorer is a GUI application that lets you connect to an existing IoT Hub and add, manage, and monitor devices. To learn more, see Install and use Azure IoT explorer.

Create a simulated device

In this section, you create a simulated IoT device that is connected to your IoT hub.

To create a simulated device:

  1. Run the az iot hub device-identity create command in your CLI shell. This creates the simulated device identity.

    YourIotHubName. Replace this placeholder below with the name you chose for your IoT hub.

    myDevice. You can use this name directly for the simulated device ID in the rest of this article. Optionally, use a different name.

    az iot hub device-identity create --device-id myDevice --hub-name {YourIoTHubName} 
    
  2. Run the az iot hub device-identity connection-string show command.

    az iot hub device-identity connection-string show --device-id myDevice --hub-name {YourIoTHubName}
    

    The connection string output is in the following format:

    HostName=<your IoT Hub name>.azure-devices.net;DeviceId=<your device id>;SharedAccessKey=<some value>
    
  3. Save the connection string in a secure location.

Note

Keep your CLI app open. You'll use it in later steps.

Run a simulated device

In this section, you use the Java SDK to send messages from your simulated device to your IoT hub.

Configure your environment

  1. Open a console to install the Azure IoT Java device SDK, build, and run the code sample.

    Note

    You should now have two console windows open: the one you just opened, and the Cloud Shell or CLI console that you used previously to enter CLI commands.

  2. Set the following environment variables, to enable your simulated device to connect to Azure IoT.

    • Set an environment variable called IOTHUB_DEVICE_CONNECTION_STRING. For the variable value, use the device connection string that you saved in the previous section.
    • Set an environment variable called IOTHUB_DEVICE_SECURITY_TYPE. For the variable, use the literal string value connectionString.

    CMD

    set IOTHUB_DEVICE_CONNECTION_STRING=<your connection string here>
    set IOTHUB_DEVICE_SECURITY_TYPE=connectionString
    

    Note

    For Windows CMD there are no quotation marks surrounding the string values for each variable.

    Bash

    export IOTHUB_DEVICE_CONNECTION_STRING="<your connection string here>"
    export IOTHUB_DEVICE_SECURITY_TYPE="connectionString"
    

Build the sample

  1. In your console, clone the Azure IoT Java device SDK to your local machine:
    git clone https://github.com/Azure/azure-iot-sdk-c.git
    
  2. Navigate to the root folder of the SDK, and run the following command to build the SDK and update the samples.
    cd azure-iot-sdk-java
    mvn install -T 2C -DskipTests
    
    This operation takes several minutes.

Run the code

  1. In your CLI app, run the az iot hub monitor-events command to begin monitoring for events on your simulated IoT device. Event messages print in the terminal as they arrive.

    az iot hub monitor-events --output table --hub-name {YourIoTHubName}
    
  2. In your Java console, navigate to the samples directory.

    cd device/iot-device-samples/pnp-device-sample/temperature-controller-device-sample
    
  3. In your Java console, run the following code sample. The sample creates a simulated temperature controller with thermostat sensors.

    mvn exec:java -Dexec.mainClass="samples.com.microsoft.azure.sdk.iot.device.TemperatureController"
    

    Note

    This code sample uses Azure IoT Plug and Play, which lets you integrate smart devices into your solutions without any manual configuration. By default, most samples in this documentation use IoT Plug and Play. To learn more about the advantages of IoT PnP, and cases for using or not using it, see What is IoT Plug and Play?.

    After your simulated device connects to your IoT hub, it connects to the device instance you created in the application and begins to send telemetry. After some initial provisioning details, the console starts to output the telemetry for the temperature controller.

    2021-05-13 22:11:05.639 DEBUG TemperatureController:518 - Telemetry - Response from IoT Hub: message Id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, status=OK_EMPTY
    2021-05-13 22:11:10.229 INFO  IotHubTransport:540 - Message was queued to be sent later ( Message details: Correlation Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] Message Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] )
    2021-05-13 22:11:10.229 INFO  IotHubTransport:1473 - Sending message ( Message details: Correlation Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] Message Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] )
    2021-05-13 22:11:10.231 DEBUG TemperatureController:465 - Telemetry: Sent - {"temperature": 22.8░C} with message Id xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
    2021-05-13 22:11:10.234 INFO  IotHubTransport:540 - Message was queued to be sent later ( Message details: Correlation Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] Message Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] )
    2021-05-13 22:11:10.236 DEBUG TemperatureController:465 - Telemetry: Sent - {"temperature": 35.3░C} with message Id xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
    

Prerequisites

  • If you don't have an Azure subscription, create one for free before you begin.
  • Git.
  • Node.js version 10 or later. To check your node version run node --version.
  • Azure CLI. You have two options for running Azure CLI commands in this quickstart:
    • Use the Azure Cloud Shell, an interactive shell that runs CLI commands in your browser. This option is recommended because you don't need to install anything. If you're using Cloud Shell for the first time, log into the Azure portal. Follow the steps in Cloud Shell quickstart to Start Cloud Shell and Select the Bash environment.
    • Optionally, run Azure CLI on your local machine. If Azure CLI is already installed, run az upgrade to upgrade the CLI and extensions to the current version. To install Azure CLI, see Install Azure CLI.

Create an IoT hub

In this section, you use Azure CLI to create an IoT hub and a resource group. An Azure resource group is a logical container into which Azure resources are deployed and managed. An IoT hub acts as a central message hub for bi-directional communication between your IoT application and the devices.

To create an IoT hub and a resource group:

  1. Launch your CLI app. To run the CLI commands in the rest of this quickstart, copy the command syntax, paste it into your CLI app, edit variable values, and press Enter.

    • If you're using Cloud Shell, select the Try It button on the CLI commands to launch Cloud Shell in a split browser window. Or you can open the Cloud Shell in a separate browser tab.
    • If you're using Azure CLI locally, start your CLI console app and sign in to Azure CLI.
  2. Run az extension add to install or upgrade the azure-iot extension to the current version.

    az extension add --upgrade --name azure-iot
    
  3. In your CLI app, run the az group create command to create a resource group. The following command creates a resource group named MyResourceGroup in the eastus location.

    Note

    You can optionally set an alternate location. To see available locations, run az account list-locations. This tutorial uses eastus as shown in the example command.

    az group create --name MyResourceGroup --location eastus
    
  4. Run the az iot hub create command to create an IoT hub. It might take a few minutes to create an IoT hub.

    YourIotHubName. Replace this placeholder and the surrounding braces in the following command, using the name you chose for your IoT hub. An IoT hub name must be globally unique in Azure. Use your IoT hub name in the rest of this quickstart wherever you see the placeholder.

    az iot hub create --resource-group MyResourceGroup --name {YourIoTHubName}
    

    Tip

    After creating an IoT hub, in the rest of this quickstart you can continue to use CLI commands to interact with the hub. Optionally, you could use Azure IoT Explorer in place of the CLI commands. IoT Explorer is a GUI application that lets you connect to an existing IoT Hub and add, manage, and monitor devices. To learn more, see Install and use Azure IoT explorer.

Create a simulated device

In this section, you create a simulated IoT device that is connected to your IoT hub.

To create a simulated device:

  1. Run the az iot hub device-identity create command in your CLI shell. This creates the simulated device identity.

    YourIotHubName. Replace this placeholder below with the name you chose for your IoT hub.

    myDevice. You can use this name directly for the simulated device ID in the rest of this article. Optionally, use a different name.

    az iot hub device-identity create --device-id myDevice --hub-name {YourIoTHubName} 
    
  2. Run the az iot hub device-identity connection-string show command.

    az iot hub device-identity connection-string show --device-id myDevice --hub-name {YourIoTHubName}
    

    The connection string output is in the following format:

    HostName=<your IoT Hub name>.azure-devices.net;DeviceId=<your device id>;SharedAccessKey=<some value>
    
  3. Save the connection string in a secure location.

Note

Keep your CLI app open. You'll use it in later steps.

Run a simulated device

In this section, you will use the Node.js SDK to send messages from your simulated device to your IoT hub.

  1. Open a new console window. You will use this console to install the Node.js SDK and work with Node.js sample code. You should now have two console windows open: the one you just opened, and the Cloud Shell or CLI console that you used previously to enter CLI commands.

  2. In your Node console, clone the Azure IoT Node.js SDK device samples to your local machine:

    git clone https://github.com/Azure/azure-iot-sdk-node
    
  3. Navigate to the azure-iot-sdk-node/device/samples/pnp directory:

    cd azure-iot-sdk-node/device/samples/pnp
    
  4. Install the Azure IoT Node.js SDK and necessary dependencies:

    npm install
    

    This command installs the proper dependencies as specified in the package.json file in the device samples directory.

  5. Set both of the following environment variables, to enable your simulated device to connect to Azure IoT.

    • Set an environment variable called IOTHUB_DEVICE_CONNECTION_STRING. For the variable value, use the device connection string that you saved in the previous section.
    • Set an environment variable called IOTHUB_DEVICE_SECURITY_TYPE. For the variable, use the literal string value connectionString.

    Windows (cmd)

    set IOTHUB_DEVICE_CONNECTION_STRING=<your connection string here>
    set IOTHUB_DEVICE_SECURITY_TYPE=connectionString
    

    Note

    For Windows CMD there are no quotation marks surrounding the string values for each variable.

    PowerShell

    $env:IOTHUB_DEVICE_CONNECTION_STRING='<your connection string here>'
    $env:IOTHUB_DEVICE_SECURITY_TYPE='connectionString'
    

    Bash (Linux or Windows)

    export IOTHUB_DEVICE_CONNECTION_STRING="<your connection string here>"
    export IOTHUB_DEVICE_SECURITY_TYPE="connectionString"
    
  6. In your CLI app, run the az iot hub monitor-events command to begin monitoring for events on your simulated IoT device. Event messages will be printed in the terminal as they arrive.

    az iot hub monitor-events --output table --hub-name {YourIoTHubName}
    
  7. In your Node console, run the code for the following sample file. This code accesses the simulated IoT device and sends a message to the IoT hub.

    To run the Node.js sample from the terminal:

    node pnpTemperatureController.js
    

    Note

    This code sample uses Azure IoT Plug and Play, which lets you integrate smart devices into your solutions without any manual configuration. By default, most samples in this documentation use IoT Plug and Play. To learn more about the advantages of IoT PnP, and cases for using or not using it, see What is IoT Plug and Play?.

As the Node.js code sends a simulated telemetry message from your device to the IoT hub, the message appears in your CLI app that is monitoring events:

Starting event monitor, use ctrl-c to stop...
event:
  component: thermostat1
  interface: dtmi:com:example:TemperatureController;2
  module: ''
  origin: myDevice
  payload:
    temperature: 70.5897683228018

event:
  component: thermostat2
  interface: dtmi:com:example:TemperatureController;2
  module: ''
  origin: myDevice
  payload:
    temperature: 52.87582619316418

Your device is now securely connected and sending telemetry to Azure IoT Hub.

Prerequisites

  • If you don't have an Azure subscription, create one for free before you begin.
  • Git.
  • Python version 3.7 or later. To check your Python version, run python --version.
  • Azure CLI. You have two options for running Azure CLI commands in this quickstart:
    • Use the Azure Cloud Shell, an interactive shell that runs CLI commands in your browser. This option is recommended because you don't need to install anything. If you're using Cloud Shell for the first time, log into the Azure portal. Follow the steps in Cloud Shell quickstart to Start Cloud Shell and Select the Bash environment.
    • Optionally, run Azure CLI on your local machine. If Azure CLI is already installed, run az upgrade to upgrade the CLI and extensions to the current version. To install Azure CLI, see Install Azure CLI.

Create an IoT hub

In this section, you use Azure CLI to create an IoT hub and a resource group. An Azure resource group is a logical container into which Azure resources are deployed and managed. An IoT hub acts as a central message hub for bi-directional communication between your IoT application and the devices.

To create an IoT hub and a resource group:

  1. Launch your CLI app. To run the CLI commands in the rest of this quickstart, copy the command syntax, paste it into your CLI app, edit variable values, and press Enter.

    • If you're using Cloud Shell, select the Try It button on the CLI commands to launch Cloud Shell in a split browser window. Or you can open the Cloud Shell in a separate browser tab.
    • If you're using Azure CLI locally, start your CLI console app and sign in to Azure CLI.
  2. Run az extension add to install or upgrade the azure-iot extension to the current version.

    az extension add --upgrade --name azure-iot
    
  3. In your CLI app, run the az group create command to create a resource group. The following command creates a resource group named MyResourceGroup in the eastus location.

    Note

    You can optionally set an alternate location. To see available locations, run az account list-locations. This tutorial uses eastus as shown in the example command.

    az group create --name MyResourceGroup --location eastus
    
  4. Run the az iot hub create command to create an IoT hub. It might take a few minutes to create an IoT hub.

    YourIotHubName. Replace this placeholder and the surrounding braces in the following command, using the name you chose for your IoT hub. An IoT hub name must be globally unique in Azure. Use your IoT hub name in the rest of this quickstart wherever you see the placeholder.

    az iot hub create --resource-group MyResourceGroup --name {YourIoTHubName}
    

    Tip

    After creating an IoT hub, in the rest of this quickstart you can continue to use CLI commands to interact with the hub. Optionally, you could use Azure IoT Explorer in place of the CLI commands. IoT Explorer is a GUI application that lets you connect to an existing IoT Hub and add, manage, and monitor devices. To learn more, see Install and use Azure IoT explorer.

Create a simulated device

In this section, you create a simulated IoT device that is connected to your IoT hub.

To create a simulated device:

  1. Run the az iot hub device-identity create command in your CLI shell. This creates the simulated device identity.

    YourIotHubName. Replace this placeholder below with the name you chose for your IoT hub.

    myDevice. You can use this name directly for the simulated device ID in the rest of this article. Optionally, use a different name.

    az iot hub device-identity create --device-id myDevice --hub-name {YourIoTHubName} 
    
  2. Run the az iot hub device-identity connection-string show command.

    az iot hub device-identity connection-string show --device-id myDevice --hub-name {YourIoTHubName}
    

    The connection string output is in the following format:

    HostName=<your IoT Hub name>.azure-devices.net;DeviceId=<your device id>;SharedAccessKey=<some value>
    
  3. Save the connection string in a secure location.

Note

Keep your CLI app open. You'll use it in later steps.

Run a simulated device

In this section, you use the Python SDK to send messages from your simulated device to your IoT hub.

  1. Open a new console window. You will use this console to install the Python SDK and work with Python sample code. You should now have two console windows open: the one you just opened, and the Cloud Shell or CLI console that you used previously to enter CLI commands.

  2. In your Python console, clone the Azure IoT Python SDK device samples to your local machine:

    git clone https://github.com/Azure/azure-iot-sdk-python
    
  3. Navigate to the samples directory:

    cd azure-iot-sdk-python/azure-iot-device/samples/pnp
    
  4. Install the Azure IoT Python SDK:

    pip3 install azure-iot-device
    
  5. Set the following environment variables, to enable your simulated device to connect to Azure IoT.

    • Set an environment variable called IOTHUB_DEVICE_CONNECTION_STRING. For the variable value, use the device connection string that you saved in the previous section.
    • Set an environment variable called IOTHUB_DEVICE_SECURITY_TYPE. For the variable, use the literal string value connectionString.

    Windows (cmd)

    set IOTHUB_DEVICE_CONNECTION_STRING=<your connection string here>
    set IOTHUB_DEVICE_SECURITY_TYPE=connectionString
    

    Note

    For Windows CMD there are no quotation marks surrounding the string values for each variable.

    PowerShell

    $env:IOTHUB_DEVICE_CONNECTION_STRING='<your connection string here>'
    $env:IOTHUB_DEVICE_SECURITY_TYPE='connectionString'
    

    Bash (Linux or Windows)

    export IOTHUB_DEVICE_CONNECTION_STRING="<your connection string here>"
    export IOTHUB_DEVICE_SECURITY_TYPE="connectionString"
    
  6. In your CLI app, run the az iot hub monitor-events command to begin monitoring for events on your simulated IoT device. Event messages print in the terminal as they arrive.

    az iot hub monitor-events --output table --hub-name {YourIoTHubName}
    
  7. In your Python console, run the code for the following sample file. The sample creates a simulated temperature controller with thermostat sensors.

    python temp_controller_with_thermostats.py
    

    Note

    This code sample uses Azure IoT Plug and Play, which lets you integrate smart devices into your solutions without any manual configuration. By default, most samples in this documentation use IoT Plug and Play. To learn more about the advantages of IoT PnP, and cases for using or not using it, see What is IoT Plug and Play?.

As the Python code sends a message from your device to the IoT hub, the message appears in your CLI app that is monitoring events:

Starting event monitor, use ctrl-c to stop...
event:
  component: thermostat1
  interface: dtmi:com:example:TemperatureController;2
  module: ''
  origin: myDevice
  payload:
    temperature: 29

event:
  component: thermostat2
  interface: dtmi:com:example:TemperatureController;2
  module: ''
  origin: myDevice
  payload:
    temperature: 48

Your device is now securely connected and sending telemetry to Azure IoT Hub.

View telemetry

After the simulated device connects to IoT Hub, it begins sending telemetry. You can view the telemetry metrics and other details about your Iot hub and devices in the Azure portal.

  1. Sign in to the Azure portal.

  2. Click your IoT hub to open it. You can find your IoT hub under Recent resources or in the left navigation, you can find it in All resources.

  3. On the Overview page scroll to view the overview metrics for your hub. IoT Hub device metrics overview

  4. Optionally, to review more metrics and build custom views, on the left navigation in Monitoring, select Metrics.

Clean up resources

If you no longer need the Azure resources created in this quickstart, you can use the Azure CLI to delete them.

Important

Deleting a resource group is irreversible. The resource group and all the resources contained in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources.

To delete a resource group by name:

  1. Run the az group delete command. This command removes the resource group, the IoT Hub, and the device registration you created.

    az group delete --name MyResourceGroup
    
  2. Run the az group list command to confirm the resource group is deleted.

    az group list
    

Next steps

In this quickstart, you learned a basic Azure IoT application workflow for securely connecting a device to the cloud and sending device-to-cloud telemetry. You used Azure CLI to create an Azure IoT hub and a device instance. Then you used an Azure IoT device SDK to create a simulated device, connect it to the hub, and send telemetry. You also used Azure portal to monitor telemetry.

As a next step, explore the following articles to learn more about building device solutions with Azure IoT.