A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.microsoft.com/azure/iot-hub/iot-hub-csharp-csharp-getstarted below:

Send device telemetry to Azure IoT Hub tutorial - Azure IoT

In this quickstart, you learn a basic Azure IoT application development workflow. You use the Azure CLI and IoT Explorer to create an Azure IoT hub and a device. Then you use an Azure IoT device SDK sample to run a temperature controller, connect it securely to the hub, and send telemetry. The temperature controller sample application runs on your local machine and generates simulated sensor data to send to IoT Hub.

Prerequisites

This quickstart runs on Windows, Linux, and Raspberry Pi. It's been tested on the following OS and device versions:

Install the following prerequisites on your development machine except where noted for Raspberry Pi:

Install the remaining prerequisites for your operating system.

Linux or Raspberry Pi OS

To complete this quickstart on Linux or Raspberry Pi OS, install the following software:

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 3.13 or greater, and the version of GCC is 4.4.7 or greater.

cmake --version
gcc --version
Windows

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

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

    Note

    For existing Visual Studio 2022 users, select Windows Start, type Visual Studio Installer, run the installer, and then select Modify.

  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 devices.

To create an IoT hub and a resource group:

  1. Launch Azure CLI:

    To run the CLI commands in the rest of this quickstart: copy the command syntax, paste it into your Cloud Shell window or CLI console, edit variable values, and press Enter.

  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. 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, you'll use Azure IoT Explorer to interact with your IoT hub in the rest of this quickstart. 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. Optionally, you can continue to use CLI commands.

Configure IoT Explorer

In the rest of this quickstart, you'll use IoT Explorer to register a device to your IoT hub and to view the device telemetry. In this section, you configure IoT Explorer to connect to the IoT hub you just created and to read plug and play models from the public model repository.

To add a connection to your IoT hub:

  1. Run the az iot hub connection-string show command to get the connection string for your IoT hub.

    az iot hub connection-string  show --hub-name {YourIoTHubName}
    
  2. Copy the connection string without the surrounding quotation characters.

  3. In Azure IoT Explorer, select IoT hubs on the left menu, then select + Add connection.

  4. Paste the connection string into the Connection string box.

  5. Select Save.

  6. If the connection succeeds, IoT Explorer switches to the Devices view.

To add the public model repository:

  1. In IoT Explorer, select Home to return to the home view.

  2. On the left menu, select IoT Plug and Play Settings, then select +Add and select Public repository from the drop-down menu.

  3. An entry appears for the public model repository at https://devicemodels.azure.com.

  4. Select Save.

Register a device

In this section, you create a new device instance and register it with the IoT hub you created. You'll use the connection information for the newly registered device to securely connect your device in a later section.

To register a device:

  1. From the home view in IoT Explorer, select IoT hubs.

  2. The connection you previously added should appear. Select View devices in this hub below the connection properties.

  3. Select + New and enter a device ID for your device; for example, mydevice. Leave all other properties the same.

  4. Select Create.

  5. Use the copy buttons to copy and note down the Primary connection string field. You'll need this connection string later.

Run the device sample

In this section, you use the C SDK to send messages from a device to your IoT hub. You run a sample that implements a temperature controller with two thermostat sensors.

Build the sample
  1. Open a new 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 and Raspberry Pi OS, open a terminal for Bash commands.

    Note

    If you're using a local installation of Azure CLI, you might now have two console windows open. Be sure to enter the commands in this section in the console you just opened, not the one that you've been using for the CLI.

  2. Navigate to a local folder where you want to clone the sample repo.

  3. Clone the Azure IoT C device SDK to your local machine:

    git clone https://github.com/Azure/azure-iot-sdk-c.git
    
  4. 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.

  5. 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
    
  6. Set the following environment variables, to enable your device to connect to Azure IoT.

    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. 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/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?.

The sample securely connects to your IoT hub as the device you registered and begins sending telemetry messages. The sample output appears in your console.

View telemetry

You can view the device telemetry with IoT Explorer. Optionally, you can view telemetry using Azure CLI.

To view telemetry in Azure IoT Explorer:

  1. From your Iot hub in IoT Explorer, select View devices in this hub, then select your device from the list.

  2. On the left menu for your device, select Telemetry.

  3. Confirm that Use built-in event hub is set to Yes and then select Start.

  4. View the telemetry as the device sends messages to the cloud.

  5. Select Stop to end receiving events.

To read telemetry sent by individual device components, you can use the plug and play features in IoT Explorer. For example, the temperature controller in this quickstart has two thermostats: thermostat1 and thermostat2. To see the temperature reported by thermostat1:

  1. On your device in IoT Explorer, select IoT Plug and Play components from the left menu. Then select thermostat1 from the list of components.

  2. On the thermostat1 component pane, select Telemetry from the top menu.

  3. On the Telemetry pane, follow the same steps that you did previously. Make sure that Use built-in event hub is set to Yes and then select Start.

To view device telemetry with Azure CLI:

  1. Run the az iot hub monitor-events command to monitor events sent from the device to your IoT hub. Use the names that you created previously in Azure IoT for your device and IoT hub.

    az iot hub monitor-events --output table --device-id mydevice --hub-name {YourIoTHubName}
    
  2. View the connection details and telemetry output in the console.

    Starting event monitor, filtering on device: mydevice, 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}'
    

In this quickstart, you learn a basic Azure IoT application development workflow. You use the Azure CLI and IoT Explorer to create an Azure IoT hub and a device. Then you use an Azure IoT device SDK sample to run a temperature controller, connect it securely to the hub, and send telemetry. The temperature controller sample application runs on your local machine and generates simulated sensor data to send to IoT Hub.

Prerequisites

This quickstart runs on Windows, Linux, and Raspberry Pi. It's been tested on the following OS and device versions:

Install the following prerequisites on your development machine except where noted for Raspberry Pi:

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 devices.

To create an IoT hub and a resource group:

  1. Launch Azure CLI:

    To run the CLI commands in the rest of this quickstart: copy the command syntax, paste it into your Cloud Shell window or CLI console, edit variable values, and press Enter.

  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. 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, you'll use Azure IoT Explorer to interact with your IoT hub in the rest of this quickstart. 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. Optionally, you can continue to use CLI commands.

Configure IoT Explorer

In the rest of this quickstart, you'll use IoT Explorer to register a device to your IoT hub and to view the device telemetry. In this section, you configure IoT Explorer to connect to the IoT hub you just created and to read plug and play models from the public model repository.

To add a connection to your IoT hub:

  1. Run the az iot hub connection-string show command to get the connection string for your IoT hub.

    az iot hub connection-string  show --hub-name {YourIoTHubName}
    
  2. Copy the connection string without the surrounding quotation characters.

  3. In Azure IoT Explorer, select IoT hubs on the left menu, then select + Add connection.

  4. Paste the connection string into the Connection string box.

  5. Select Save.

  6. If the connection succeeds, IoT Explorer switches to the Devices view.

To add the public model repository:

  1. In IoT Explorer, select Home to return to the home view.

  2. On the left menu, select IoT Plug and Play Settings, then select +Add and select Public repository from the drop-down menu.

  3. An entry appears for the public model repository at https://devicemodels.azure.com.

  4. Select Save.

Register a device

In this section, you create a new device instance and register it with the IoT hub you created. You'll use the connection information for the newly registered device to securely connect your device in a later section.

To register a device:

  1. From the home view in IoT Explorer, select IoT hubs.

  2. The connection you previously added should appear. Select View devices in this hub below the connection properties.

  3. Select + New and enter a device ID for your device; for example, mydevice. Leave all other properties the same.

  4. Select Create.

  5. Use the copy buttons to copy and note down the Primary connection string field. You'll need this connection string later.

Run the device sample

In this section, you'll use the C# SDK to send messages from a device to your IoT hub. You'll run a sample that implements a temperature controller with two thermostat sensors.

  1. Open a new console such as Windows CMD, PowerShell, or Bash. In the following steps, you'll use this console to install the Node.js SDK and work with Node.js sample code.

    Note

    If you're using a local installation of Azure CLI, you might now have two console windows open. Be sure to enter the commands in this section in the console you just opened, not the one that you've been using for the CLI.

  2. Clone the Microsoft Azure IoT SDK for C# (.NET) to your local machine:

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

    Windows

    cd azure-iot-sdk-csharp\iothub\device\samples\solutions\PnpDeviceSamples\TemperatureController
    

    Linux or Raspberry Pi OS

    cd azure-iot-sdk-csharp/iothub/device/samples/solutions/PnpDeviceSamples/TemperatureController
    
  4. Install the Azure IoT C# SDK and necessary dependencies:

    dotnet restore
    

    This command installs the proper dependencies as specified in the TemperatureController.csproj file.

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

    CMD (Windows)

    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

    export IOTHUB_DEVICE_CONNECTION_STRING="<your connection string here>"
    export IOTHUB_DEVICE_SECURITY_TYPE="connectionString"
    
  6. Run the code sample:

    dotnet run
    

    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?.

The sample securely connects to your IoT hub as the device you registered and begins sending telemetry messages. The sample output appears in your console.

View telemetry

You can view the device telemetry with IoT Explorer. Optionally, you can view telemetry using Azure CLI.

To view telemetry in Azure IoT Explorer:

  1. From your Iot hub in IoT Explorer, select View devices in this hub, then select your device from the list.

  2. On the left menu for your device, select Telemetry.

  3. Confirm that Use built-in event hub is set to Yes and then select Start.

  4. View the telemetry as the device sends messages to the cloud.

  5. Select Stop to end receiving events.

To read telemetry sent by individual device components, you can use the plug and play features in IoT Explorer. For example, the temperature controller in this quickstart has two thermostats: thermostat1 and thermostat2. To see the temperature reported by thermostat1:

  1. On your device in IoT Explorer, select IoT Plug and Play components from the left menu. Then select thermostat1 from the list of components.

  2. On the thermostat1 component pane, select Telemetry from the top menu.

  3. On the Telemetry pane, follow the same steps that you did previously. Make sure that Use built-in event hub is set to Yes and then select Start.

To view device telemetry with Azure CLI:

  1. Run the az iot hub monitor-events command to monitor events sent from the device to your IoT hub. Use the names that you created previously in Azure IoT for your device and IoT hub.

    az iot hub monitor-events --output table --device-id mydevice --hub-name {YourIoTHubName}
    
  2. View the connection details and telemetry output in the console.

    Starting event monitor, filtering on device: mydevice, 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
    
  3. Select CTRL+C to end monitoring.

In this quickstart, you learn a basic Azure IoT application development workflow. You use the Azure CLI and IoT Explorer to create an Azure IoT hub and a device. Then you use an Azure IoT device SDK sample to run a temperature controller, connect it securely to the hub, and send telemetry. The temperature controller sample application runs on your local machine and generates simulated sensor data to send to IoT Hub.

Prerequisites

This quickstart runs on Windows, Linux, and Raspberry Pi. It's been tested on the following OS and device versions:

Install the following prerequisites on your development machine except where noted for Raspberry Pi:

Install the remaining prerequisites for your operating system.

Windows

To complete this quickstart on Windows, install the following software:

Linux or Raspberry Pi OS

To complete this quickstart on Linux or Raspberry Pi OS, install the following software:

Note

Steps in this section are based on Linux Ubuntu/Debian distributions. (Raspberry Pi OS is based on Debian.) If you're using a different Linux distribution, you'll need to modify the steps accordingly.

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 devices.

To create an IoT hub and a resource group:

  1. Launch Azure CLI:

    To run the CLI commands in the rest of this quickstart: copy the command syntax, paste it into your Cloud Shell window or CLI console, edit variable values, and press Enter.

  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. 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, you'll use Azure IoT Explorer to interact with your IoT hub in the rest of this quickstart. 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. Optionally, you can continue to use CLI commands.

Configure IoT Explorer

In the rest of this quickstart, you'll use IoT Explorer to register a device to your IoT hub and to view the device telemetry. In this section, you configure IoT Explorer to connect to the IoT hub you just created and to read plug and play models from the public model repository.

To add a connection to your IoT hub:

  1. Run the az iot hub connection-string show command to get the connection string for your IoT hub.

    az iot hub connection-string  show --hub-name {YourIoTHubName}
    
  2. Copy the connection string without the surrounding quotation characters.

  3. In Azure IoT Explorer, select IoT hubs on the left menu, then select + Add connection.

  4. Paste the connection string into the Connection string box.

  5. Select Save.

  6. If the connection succeeds, IoT Explorer switches to the Devices view.

To add the public model repository:

  1. In IoT Explorer, select Home to return to the home view.

  2. On the left menu, select IoT Plug and Play Settings, then select +Add and select Public repository from the drop-down menu.

  3. An entry appears for the public model repository at https://devicemodels.azure.com.

  4. Select Save.

Register a device

In this section, you create a new device instance and register it with the IoT hub you created. You'll use the connection information for the newly registered device to securely connect your device in a later section.

To register a device:

  1. From the home view in IoT Explorer, select IoT hubs.

  2. The connection you previously added should appear. Select View devices in this hub below the connection properties.

  3. Select + New and enter a device ID for your device; for example, mydevice. Leave all other properties the same.

  4. Select Create.

  5. Use the copy buttons to copy and note down the Primary connection string field. You'll need this connection string later.

Run the device sample

In this section, you use the Java SDK to send messages from a device to your IoT hub. You'll run a sample that implements a temperature controller with two thermostat sensors.

  1. Open a console to install the Azure IoT Java device SDK, build, and run the code sample. You'll use this console in the following steps.

    Note

    If you're using a local installation of Azure CLI, you might now have two console windows open. Be sure to enter the commands in this section in the console you just opened, not the one that you've been using for the CLI.

    Linux and Raspberry Pi OS

    Confirm that the JAVA_HOME (echo $JAVA_HOME) environment variable is set. For information about setting JAVA_HOME, see Linux/Raspberry Pi Prerequisites.

  2. Clone the Azure IoT Java device SDK to your local machine:

    git clone https://github.com/Azure/azure-iot-sdk-java.git
    
  3. 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.

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

    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"
    
  5. Navigate to the sample directory.

    CMD

    cd device\iot-device-samples\pnp-device-sample\temperature-controller-device-sample
    

    Bash

    cd device/iot-device-samples/pnp-device-sample/temperature-controller-device-sample
    
  6. Run the code sample.

    java -jar -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?.

The sample securely connects to your IoT hub as the device you registered and begins sending telemetry messages. The sample output appears in your console.

View telemetry

You can view the device telemetry with IoT Explorer. Optionally, you can view telemetry using Azure CLI.

To view telemetry in Azure IoT Explorer:

  1. From your Iot hub in IoT Explorer, select View devices in this hub, then select your device from the list.

  2. On the left menu for your device, select Telemetry.

  3. Confirm that Use built-in event hub is set to Yes and then select Start.

  4. View the telemetry as the device sends messages to the cloud.

  5. Select Stop to end receiving events.

To read telemetry sent by individual device components, you can use the plug and play features in IoT Explorer. For example, the temperature controller in this quickstart has two thermostats: thermostat1 and thermostat2. To see the temperature reported by thermostat1:

  1. On your device in IoT Explorer, select IoT Plug and Play components from the left menu. Then select thermostat1 from the list of components.

  2. On the thermostat1 component pane, select Telemetry from the top menu.

  3. On the Telemetry pane, follow the same steps that you did previously. Make sure that Use built-in event hub is set to Yes and then select Start.

To view device telemetry with Azure CLI:

  1. Run the az iot hub monitor-events command to monitor events sent from the device to your IoT hub. Use the names that you created previously in Azure IoT for your device and IoT hub.

    az iot hub monitor-events --output table --device-id mydevice --hub-name {YourIoTHubName}
    
  2. View the connection details and telemetry output in the console.

    Starting event monitor, filtering on device: mydevice, use ctrl-c to stop...
    event:
      component: thermostat1
      interface: dtmi:com:example:TemperatureController;2
      module: ''
      origin: mydevice
      payload:
        temperature: 24.1
    
    event:
      component: thermostat2
      interface: dtmi:com:example:TemperatureController;2
      module: ''
      origin: mydevice
      payload:
        temperature: 33.3
    

In this quickstart, you learn a basic Azure IoT application development workflow. You use the Azure CLI and IoT Explorer to create an Azure IoT hub and a device. Then you use an Azure IoT device SDK sample to run a temperature controller, connect it securely to the hub, and send telemetry. The temperature controller sample application runs on your local machine and generates simulated sensor data to send to IoT Hub.

Prerequisites

This quickstart runs on Windows, Linux, and Raspberry Pi. It's been tested on the following OS and device versions:

Install the following prerequisites on your development machine except where noted for Raspberry Pi:

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 devices.

To create an IoT hub and a resource group:

  1. Launch Azure CLI:

    To run the CLI commands in the rest of this quickstart: copy the command syntax, paste it into your Cloud Shell window or CLI console, edit variable values, and press Enter.

  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. 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, you'll use Azure IoT Explorer to interact with your IoT hub in the rest of this quickstart. 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. Optionally, you can continue to use CLI commands.

Configure IoT Explorer

In the rest of this quickstart, you'll use IoT Explorer to register a device to your IoT hub and to view the device telemetry. In this section, you configure IoT Explorer to connect to the IoT hub you just created and to read plug and play models from the public model repository.

To add a connection to your IoT hub:

  1. Run the az iot hub connection-string show command to get the connection string for your IoT hub.

    az iot hub connection-string  show --hub-name {YourIoTHubName}
    
  2. Copy the connection string without the surrounding quotation characters.

  3. In Azure IoT Explorer, select IoT hubs on the left menu, then select + Add connection.

  4. Paste the connection string into the Connection string box.

  5. Select Save.

  6. If the connection succeeds, IoT Explorer switches to the Devices view.

To add the public model repository:

  1. In IoT Explorer, select Home to return to the home view.

  2. On the left menu, select IoT Plug and Play Settings, then select +Add and select Public repository from the drop-down menu.

  3. An entry appears for the public model repository at https://devicemodels.azure.com.

  4. Select Save.

Register a device

In this section, you create a new device instance and register it with the IoT hub you created. You'll use the connection information for the newly registered device to securely connect your device in a later section.

To register a device:

  1. From the home view in IoT Explorer, select IoT hubs.

  2. The connection you previously added should appear. Select View devices in this hub below the connection properties.

  3. Select + New and enter a device ID for your device; for example, mydevice. Leave all other properties the same.

  4. Select Create.

  5. Use the copy buttons to copy and note down the Primary connection string field. You'll need this connection string later.

Run the device sample

In this section, you'll use the Node.js SDK to send messages from a device to your IoT hub. You'll run a sample that implements a temperature controller with two thermostat sensors.

  1. Open a new console such as Windows CMD, PowerShell, or Bash. In the following steps, you'll use this console to install the Node.js SDK and work with Node.js sample code.

    Note

    If you're using a local installation of Azure CLI, you might now have two console windows open. Be sure to enter the commands in this section in the console you just opened, not the one that you've been using for the CLI.

  2. 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 sample directory:

    Windows

    cd azure-iot-sdk-node\device\samples\javascript
    

    Linux or Raspberry Pi OS

    cd azure-iot-sdk-node/device/samples/javascript
    
  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 device to connect to Azure IoT.

    CMD (Windows)

    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

    export IOTHUB_DEVICE_CONNECTION_STRING="<your connection string here>"
    export IOTHUB_DEVICE_SECURITY_TYPE="connectionString"
    
  6. Run the following code sample:

    node pnp_temperature_controller.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?.

The sample securely connects to your IoT hub as the device you registered and begins sending telemetry messages. The sample output appears in your console.

View telemetry

You can view the device telemetry with IoT Explorer. Optionally, you can view telemetry using Azure CLI.

To view telemetry in Azure IoT Explorer:

  1. From your Iot hub in IoT Explorer, select View devices in this hub, then select your device from the list.

  2. On the left menu for your device, select Telemetry.

  3. Confirm that Use built-in event hub is set to Yes and then select Start.

  4. View the telemetry as the device sends messages to the cloud.

  5. Select Stop to end receiving events.

To read telemetry sent by individual device components, you can use the plug and play features in IoT Explorer. For example, the temperature controller in this quickstart has two thermostats: thermostat1 and thermostat2. To see the temperature reported by thermostat1:

  1. On your device in IoT Explorer, select IoT Plug and Play components from the left menu. Then select thermostat1 from the list of components.

  2. On the thermostat1 component pane, select Telemetry from the top menu.

  3. On the Telemetry pane, follow the same steps that you did previously. Make sure that Use built-in event hub is set to Yes and then select Start.

To view device telemetry with Azure CLI:

  1. Run the az iot hub monitor-events command to monitor events sent from the device to your IoT hub. Use the names that you created previously in Azure IoT for your device and IoT hub.

    az iot hub monitor-events --output table --device-id mydevice --hub-name {YourIoTHubName}
    
  2. View the connection details and telemetry output in the console.

    Starting event monitor, filtering on device: mydevice, 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
    

In this quickstart, you learn a basic Azure IoT application development workflow. You use the Azure CLI and IoT Explorer to create an Azure IoT hub and a device. Then you use an Azure IoT device SDK sample to run a temperature controller, connect it securely to the hub, and send telemetry. The temperature controller sample application runs on your local machine and generates simulated sensor data to send to IoT Hub.

Prerequisites

This quickstart runs on Windows, Linux, and Raspberry Pi. It's been tested on the following OS and device versions:

Install the following prerequisites on your development machine except where noted for Raspberry Pi:

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 devices.

To create an IoT hub and a resource group:

  1. Launch Azure CLI:

    To run the CLI commands in the rest of this quickstart: copy the command syntax, paste it into your Cloud Shell window or CLI console, edit variable values, and press Enter.

  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. 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, you'll use Azure IoT Explorer to interact with your IoT hub in the rest of this quickstart. 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. Optionally, you can continue to use CLI commands.

Configure IoT Explorer

In the rest of this quickstart, you'll use IoT Explorer to register a device to your IoT hub and to view the device telemetry. In this section, you configure IoT Explorer to connect to the IoT hub you just created and to read plug and play models from the public model repository.

To add a connection to your IoT hub:

  1. Run the az iot hub connection-string show command to get the connection string for your IoT hub.

    az iot hub connection-string  show --hub-name {YourIoTHubName}
    
  2. Copy the connection string without the surrounding quotation characters.

  3. In Azure IoT Explorer, select IoT hubs on the left menu, then select + Add connection.

  4. Paste the connection string into the Connection string box.

  5. Select Save.

  6. If the connection succeeds, IoT Explorer switches to the Devices view.

To add the public model repository:

  1. In IoT Explorer, select Home to return to the home view.

  2. On the left menu, select IoT Plug and Play Settings, then select +Add and select Public repository from the drop-down menu.

  3. An entry appears for the public model repository at https://devicemodels.azure.com.

  4. Select Save.

Register a device

In this section, you create a new device instance and register it with the IoT hub you created. You'll use the connection information for the newly registered device to securely connect your device in a later section.

To register a device:

  1. From the home view in IoT Explorer, select IoT hubs.

  2. The connection you previously added should appear. Select View devices in this hub below the connection properties.

  3. Select + New and enter a device ID for your device; for example, mydevice. Leave all other properties the same.

  4. Select Create.

  5. Use the copy buttons to copy and note down the Primary connection string field. You'll need this connection string later.

Run the device sample

In this section, you use the Python SDK to send messages from a device to your IoT hub. You'll run a sample that implements a temperature controller with two thermostat sensors.

  1. Open a new console such as Windows CMD, PowerShell, or Bash. In the following steps, you'll use this console to install the Python SDK and work with the Python sample code.

    Note

    If you're using a local installation of Azure CLI, you might now have two console windows open. Be sure to enter the commands in this section in the console you just opened, not the one that you've been using for the CLI.

  2. Clone the Azure IoT Python SDK device samples to your local machine:

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

    Windows

    cd azure-iot-sdk-python\samples\pnp
    

    Linux or Raspberry Pi OS

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

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

    CMD (Windows)

    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

    export IOTHUB_DEVICE_CONNECTION_STRING="<your connection string here>"
    export IOTHUB_DEVICE_SECURITY_TYPE="connectionString"
    
  6. Run the code for the following sample file.

    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 Plug and Play, and cases for using or not using it, see What is IoT Plug and Play?.

The sample securely connects to your IoT hub as the device you registered and begins sending telemetry messages. The sample output appears in your console.

View telemetry

You can view the device telemetry with IoT Explorer. Optionally, you can view telemetry using Azure CLI.

To view telemetry in Azure IoT Explorer:

  1. From your Iot hub in IoT Explorer, select View devices in this hub, then select your device from the list.

  2. On the left menu for your device, select Telemetry.

  3. Confirm that Use built-in event hub is set to Yes and then select Start.

  4. View the telemetry as the device sends messages to the cloud.

  5. Select Stop to end receiving events.

To read telemetry sent by individual device components, you can use the plug and play features in IoT Explorer. For example, the temperature controller in this quickstart has two thermostats: thermostat1 and thermostat2. To see the temperature reported by thermostat1:

  1. On your device in IoT Explorer, select IoT Plug and Play components from the left menu. Then select thermostat1 from the list of components.

  2. On the thermostat1 component pane, select Telemetry from the top menu.

  3. On the Telemetry pane, follow the same steps that you did previously. Make sure that Use built-in event hub is set to Yes and then select Start.

To view device telemetry with Azure CLI:

  1. Run the az iot hub monitor-events command to monitor events sent from the device to your IoT hub. Use the names that you created previously in Azure IoT for your device and IoT hub.

    az iot hub monitor-events --output table --device-id mydevice --hub-name {YourIoTHubName}
    
  2. View the connection details and telemetry output in the console.

    Starting event monitor, filtering on device: mydevice, use ctrl-c to stop...
    event:
      component: thermostat1
      interface: dtmi:com:example:TemperatureController;2
      module: ''
      origin: mydevice
      payload:
        temperature: 28
    
    event:
      component: thermostat2
      interface: dtmi:com:example:TemperatureController;2
      module: ''
      origin: mydevice
      payload:
        temperature: 10
    
Clean up resources

If you no longer need the Azure resources created in this tutorial, 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 tutorial, 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 temperature controller, connect it to the hub, and send telemetry. You also used Azure CLI to monitor telemetry.

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

Control a device connected to an IoT hub


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4