Quickstart: Send telemetry from a device to Azure IoT Central

Applies to: General device developers

Browse code

In this quickstart, you learn a basic Azure IoT application development workflow. First you create an Azure IoT Central application for hosting devices. Then you use an Azure IoT device SDK sample to create a temperature controller, connect it securely to IoT Central, and send telemetry. The temperature controller sample application runs on your local machine and generates simulated sensor data to send to IoT Central.

Tip

As a developer, you have some options for how to connect devices to Azure IoT. To learn about connection options, see What is Azure IoT device and application development?.

Prerequisites

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

  • Windows 10
  • Ubuntu 20.04 LTS
  • Raspberry Pi OS (Raspbian) version 10, running on a Raspberry Pi 3 Model B+
  • An active Azure subscription. If you don't have an Azure subscription, create a free account before you begin.

Install the remaining prerequisites for your operating system.

Linux or Raspberry Pi OS

To complete this quickstart on Linux and 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 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. In the installer Individual components tab, select Git for Windows.
  4. Run the installation.

Create an application

There are several ways to connect devices to Azure IoT. In this section, you learn how to connect a device by using Azure IoT Central. IoT Central is an IoT application platform that reduces the cost and complexity of managing devices in an IoT solution.

To create a new application:

  1. Browse to Azure IoT Central and sign in with a Microsoft personal, work, or school account.

  2. Navigate to Build and select Custom apps. IoT Central start page

  3. In Application name, enter a unique name or use the generated name.

  4. In URL, enter a memorable application URL prefix or use the generated URL prefix.

  5. Leave Application template set to Custom application.

  6. Select a Pricing plan option.

    • Select one of the standard pricing plans. Select your Directory, Azure subscription, and Location. To learn about pricing, see Azure IoT Central pricing.
      • Directory is the Microsoft Entra ID in which you create your application. A Microsoft Entra ID contains user identities, credentials, and other organizational information. If you don't have a Microsoft Entra ID, one is created when you create an Azure subscription.
      • An Azure subscription enables you to create instances of Azure services. IoT Central provisions resources in your subscription. If you don't have a subscription, you can create one for free. If you have a subscription, you can select it in the dropdown.
      • Location is the Azure geography in which you create an application. Select a location that's physically closest to your devices to get optimal performance. After you choose a location, you can't move the application to a different location.

    IoT Central new application dialog

  7. Select Create.

    After IoT Central creates the application, it redirects you to the application dashboard. IoT Central new application dashboard

Add a device

In this section, you add a new device to your IoT Central application. The device is an instance of a device template that represents a device that you'll connect to the application.

To create a new device:

  1. In the left pane select Devices, then select +New.

  2. Leave Device template set to Unassigned and Simulate this device? set to No.

  3. Set a friendly Device name and Device ID. Optionally, use the generated values. IoT Central new device dialog

  4. Select Create.

    The created device appears in the All devices list. IoT Central all devices list

To get connection details for the new device:

  1. In the All devices list, click the linked device name to display details.

  2. In the top menu, select Connect.

    The Device connection dialog displays the connection details: IoT Central device connection details

  3. Copy the following values from the Device connection dialog to a safe location. You'll use these values to connect your device to IoT Central.

    • ID scope
    • Device ID
    • Primary key

Run the device sample

In this section, you configure your local environment, install the Azure IoT C device SDK, and run a sample that creates a temperature controller.

Configure your environment

  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 and Raspberry Pi OS, open a terminal for Bash commands.

  2. Set the following environment variables, using the appropriate commands for your console. The device uses these values to connect to IoT Central. For IOTHUB_DEVICE_DPS_ID_SCOPE, IOTHUB_DEVICE_DPS_DEVICE_KEY, and IOTHUB_DEVICE_DPS_DEVICE_ID, use the device connection values that you saved previously.

    CMD

    set IOTHUB_DEVICE_SECURITY_TYPE=DPS
    set IOTHUB_DEVICE_DPS_ID_SCOPE=<application ID scope>
    set IOTHUB_DEVICE_DPS_DEVICE_KEY=<device primary key>
    set IOTHUB_DEVICE_DPS_DEVICE_ID=<your device ID>
    set IOTHUB_DEVICE_DPS_ENDPOINT=global.azure-devices-provisioning.net
    

    Note

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

    Bash

    export IOTHUB_DEVICE_SECURITY_TYPE='DPS'
    export IOTHUB_DEVICE_DPS_ID_SCOPE='<application ID scope>'
    export IOTHUB_DEVICE_DPS_DEVICE_KEY='<device primary key>'
    export IOTHUB_DEVICE_DPS_DEVICE_ID='<your device ID>'
    export IOTHUB_DEVICE_DPS_ENDPOINT='global.azure-devices-provisioning.net' 
    

Install the SDK and samples

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

  2. Copy 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
    

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
    

    After your 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:

    Info: Initiating DPS client to retrieve IoT Hub connection information
    -> 17:03:08 CONNECT | VER: 4 | KEEPALIVE: 0 | FLAGS: 194 | USERNAME: xxxxxxxxxxxxxxx/registrations/my-sdk-device/api-version=2019-03-31&ClientVersion=1.6.0 | PWD: XXXX | CLEAN: 1
    <- 17:03:09 CONNACK | SESSION_PRESENT: false | RETURN_CODE: 0x0
    -> 17:03:10 SUBSCRIBE | PACKET_ID: 1 | TOPIC_NAME: $dps/registrations/res/# | QOS: 1
    <- 17:03:11 SUBACK | PACKET_ID: 1 | RETURN_CODE: 1
    Info: Provisioning callback indicates success.  iothubUri=iotc-xxxxxxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx.azure-devices.net, deviceId=my-sdk-device
    -> 17:03:27 DISCONNECT
    Info: DPS successfully registered.  Continuing on to creation of IoTHub device client handle.
    Info: Successfully created device client.  Hit Control-C to exit program
    
    Info: Sending serialNumber property to IoTHub
    Info: Sending device information property to IoTHub.  propertyName=swVersion, propertyValue="1.0.0.0"
    Info: Sending device information property to IoTHub.  propertyName=manufacturer, propertyValue="Sample-Manufacturer"
    Info: Sending device information property to IoTHub.  propertyName=model, propertyValue="sample-Model-123"
    Info: Sending device information property to IoTHub.  propertyName=osName, propertyValue="sample-OperatingSystem-name"
    Info: Sending device information property to IoTHub.  propertyName=processorArchitecture, propertyValue="Contoso-Arch-64bit"
    Info: Sending device information property to IoTHub.  propertyName=processorManufacturer, propertyValue="Processor Manufacturer(TM)"
    Info: Sending device information property to IoTHub.  propertyName=totalStorage, propertyValue=10000
    Info: Sending device information property to IoTHub.  propertyName=totalMemory, propertyValue=200
    Info: Sending maximumTemperatureSinceLastReboot property to IoTHub for component=thermostat1
    Info: Sending maximumTemperatureSinceLastReboot property to IoTHub for component=thermostat2
    

Browse code

In this quickstart, you learn a basic Azure IoT application development workflow. First you create an Azure IoT Central application for hosting devices. Then you use an Azure IoT device SDK sample to create a temperature controller, connect it securely to IoT Central, and send telemetry. The temperature controller sample application runs on your local machine and generates simulated sensor data to send to IoT Central.

Tip

As a developer, you have some options for how to connect devices to Azure IoT. To learn about connection options, see What is Azure IoT device and application development?.

Prerequisites

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

  • Windows 10
  • Ubuntu 20.04 LTS
  • Raspberry Pi OS (Raspbian) version 10, running on a Raspberry Pi 3 Model B+
  • An active Azure subscription. If you don't have an Azure subscription, create a free account before you begin.

Install the following prerequisites on your development machine:

  • If you don't have an Azure subscription, create one for free before you begin.

  • Git.

  • .NET Core SDK 3.1. Be sure to install the .NET SDK, not just the runtime. To check the version of the .NET SDK and runtime installed on your machine, run dotnet --info.

    • For Windows and Linux (except Raspberry Pi), follow the instructions to install the .NET Core SDK 3.1 on your platform.
    • For Raspberry Pi, you'll need to follow the instructions to manually install the SDK. This is because on Debian, package manager installs of the .NET SDK are only supported for the x64 architecture.

Create an application

There are several ways to connect devices to Azure IoT. In this section, you learn how to connect a device by using Azure IoT Central. IoT Central is an IoT application platform that reduces the cost and complexity of managing devices in an IoT solution.

To create a new application:

  1. Browse to Azure IoT Central and sign in with a Microsoft personal, work, or school account.

  2. Navigate to Build and select Custom apps. IoT Central start page

  3. In Application name, enter a unique name or use the generated name.

  4. In URL, enter a memorable application URL prefix or use the generated URL prefix.

  5. Leave Application template set to Custom application.

  6. Select a Pricing plan option.

    • Select one of the standard pricing plans. Select your Directory, Azure subscription, and Location. To learn about pricing, see Azure IoT Central pricing.
      • Directory is the Microsoft Entra ID in which you create your application. A Microsoft Entra ID contains user identities, credentials, and other organizational information. If you don't have a Microsoft Entra ID, one is created when you create an Azure subscription.
      • An Azure subscription enables you to create instances of Azure services. IoT Central provisions resources in your subscription. If you don't have a subscription, you can create one for free. If you have a subscription, you can select it in the dropdown.
      • Location is the Azure geography in which you create an application. Select a location that's physically closest to your devices to get optimal performance. After you choose a location, you can't move the application to a different location.

    IoT Central new application dialog

  7. Select Create.

    After IoT Central creates the application, it redirects you to the application dashboard. IoT Central new application dashboard

Add a device

In this section, you add a new device to your IoT Central application. The device is an instance of a device template that represents a device that you'll connect to the application.

To create a new device:

  1. In the left pane select Devices, then select +New.

  2. Leave Device template set to Unassigned and Simulate this device? set to No.

  3. Set a friendly Device name and Device ID. Optionally, use the generated values. IoT Central new device dialog

  4. Select Create.

    The created device appears in the All devices list. IoT Central all devices list

To get connection details for the new device:

  1. In the All devices list, click the linked device name to display details.

  2. In the top menu, select Connect.

    The Device connection dialog displays the connection details: IoT Central device connection details

  3. Copy the following values from the Device connection dialog to a safe location. You'll use these values to connect your device to IoT Central.

    • ID scope
    • Device ID
    • Primary key

Run the device sample

In this section, you configure your local environment, install the Azure IoT C# SDK, and run a sample that creates a temperature controller.

Configure your environment

  1. Open a console such as Windows CMD, PowerShell, or Bash.

  2. Set the following environment variables, using the appropriate commands for your console. The device uses these values to connect to IoT Central. For IOTHUB_DEVICE_DPS_ID_SCOPE, IOTHUB_DEVICE_DPS_DEVICE_KEY, and IOTHUB_DEVICE_DPS_DEVICE_ID, use the device connection values that you saved previously.

    CMD (Windows)

    set IOTHUB_DEVICE_SECURITY_TYPE=DPS
    set IOTHUB_DEVICE_DPS_ID_SCOPE=<application ID scope>
    set IOTHUB_DEVICE_DPS_DEVICE_KEY=<device primary key>
    set IOTHUB_DEVICE_DPS_DEVICE_ID=<your device ID>
    set IOTHUB_DEVICE_DPS_ENDPOINT=global.azure-devices-provisioning.net
    

    Note

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

    PowerShell

    $env:IOTHUB_DEVICE_SECURITY_TYPE='DPS'
    $env:IOTHUB_DEVICE_DPS_ID_SCOPE='<application ID scope>'
    $env:IOTHUB_DEVICE_DPS_DEVICE_KEY='<device primary key>'
    $env:IOTHUB_DEVICE_DPS_DEVICE_ID='<your device ID>'
    $env:IOTHUB_DEVICE_DPS_ENDPOINT='global.azure-devices-provisioning.net'
    

    Bash

    export IOTHUB_DEVICE_SECURITY_TYPE='DPS'
    export IOTHUB_DEVICE_DPS_ID_SCOPE='<application ID scope>'
    export IOTHUB_DEVICE_DPS_DEVICE_KEY='<device primary key>'
    export IOTHUB_DEVICE_DPS_DEVICE_ID='<your device ID>'
    export IOTHUB_DEVICE_DPS_ENDPOINT='global.azure-devices-provisioning.net' 
    

Install the SDK and samples

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

    git clone https://github.com/Azure/azure-iot-sdk-csharp.git
    
  2. 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
    
  3. Install the Azure IoT C# SDK and necessary dependencies:

    dotnet restore
    

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

Run the code

  1. In your console, run the code sample. The sample creates a temperature controller with thermostat sensors.

    dotnet run
    

    After your 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:

    [10/09/2021 00:29:18]info: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0]
          Press Control+C to quit the sample.
    [10/09/2021 00:29:18]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0]
          Set up the device client.
    [10/09/2021 00:29:18]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0]
          Initializing via DPS
    [10/09/2021 00:29:38]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0]
          Set handler for 'reboot' command.
    [10/09/2021 00:29:39]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0]
          Connection status change registered - status=Connected, reason=Connection_Ok.
    [10/09/2021 00:29:39]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0]
          Set handler for "getMaxMinReport" command.
    [10/09/2021 00:29:39]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0]
          Set handler to receive 'targetTemperature' updates.
    [10/09/2021 00:29:39]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0]
          Property: Update - component = 'deviceInformation', properties update is complete.
    [10/09/2021 00:29:39]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0]
          Property: Update - { "serialNumber": "SR-123456" } is complete.
    [10/09/2021 00:29:40]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0]
          Telemetry: Sent - component="thermostat1", { "temperature": 23.7 } in °C.
    [10/09/2021 00:29:40]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0]
          Property: Update - component="thermostat1", { "maxTempSinceLastReboot": 23.7 } in °C is complete.
    [10/09/2021 00:29:40]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0]
          Telemetry: Sent - component="thermostat2", { "temperature": 25.8 } in °C.
    

Browse code

In this quickstart, you learn a basic Azure IoT application development workflow. First you create an Azure IoT Central application for hosting devices. Then you use an Azure IoT device SDK sample to create a temperature controller, connect it securely to IoT Central, and send telemetry. The temperature controller sample application runs on your local machine and generates simulated sensor data to send to IoT Central.

Tip

As a developer, you have some options for how to connect devices to Azure IoT. To learn about connection options, see What is Azure IoT device and application development?.

Prerequisites

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

  • Windows 10
  • Ubuntu 20.04 LTS
  • Raspberry Pi OS (Raspbian) version 10, running on a Raspberry Pi 3 Model B+
  • An active Azure subscription. If you don't have an Azure subscription, create a free account before you begin.

Install the following prerequisites on your development machine:

Install the remaining prerequisites for your operating system.

Windows

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

  • 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 environment variable.

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.

  • OpenJDK (Open Java Development Kit) 8 or later. You can use the java -version command to verify the version of Java installed on your system. Make sure that the JDK is installed, not just the Java runtime (JRE).

    1. To install the OpenJDK for your system, enter the following commands:

      To install the default version of OpenJDK for your system (OpenJDK 11 for Ubuntu 20.04 and Raspberry Pi OS 10 at the time of writing):

      sudo apt update
      sudo apt install default-jdk
      

      Alternatively, you can specify a version of the JDK to install. For example:

      sudo apt update
      sudo apt install openjdk-8-jdk
      
    2. If your system has multiple versions of Java installed, you can use the following commands to configure the default (auto) versions of Java and the Java compiler.

      update-java-alternatives --list          #list the Java versions installed
      sudo update-alternatives --config java   #set the default Java version
      sudo  update-alternatives --config javac #set the default Java compiler version
      
    3. Set the JAVA_HOME environment variable to the path of your JDK installation. (This is generally a versioned subdirectory in the /usr/lib/jvm directory.)

      export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
      

      Important

      This command sets the JAVA_HOME variable in your current shell environment. We recommend adding the command to your ~/.bashrc or /etc/profile file to make it available whenever you open a new shell.

    4. Verify the version of the Java JDK (and JRE) installed, that your Java compiler version matches the JDK version, and that the JAVA_HOME environment variable is properly set.

      java -version
      javac -version
      echo $JAVA_HOME
      
  • Apache Maven 3. You can use the mvn --version command to verify the version of Maven installed on your system.

    1. To install Maven, enter the following commands:

      sudo apt-get update
      sudo apt-get install maven
      
    2. Enter the following command to verify your installation.

      mvn --version
      

Create an application

There are several ways to connect devices to Azure IoT. In this section, you learn how to connect a device by using Azure IoT Central. IoT Central is an IoT application platform that reduces the cost and complexity of managing devices in an IoT solution.

To create a new application:

  1. Browse to Azure IoT Central and sign in with a Microsoft personal, work, or school account.

  2. Navigate to Build and select Custom apps. IoT Central start page

  3. In Application name, enter a unique name or use the generated name.

  4. In URL, enter a memorable application URL prefix or use the generated URL prefix.

  5. Leave Application template set to Custom application.

  6. Select a Pricing plan option.

    • Select one of the standard pricing plans. Select your Directory, Azure subscription, and Location. To learn about pricing, see Azure IoT Central pricing.
      • Directory is the Microsoft Entra ID in which you create your application. A Microsoft Entra ID contains user identities, credentials, and other organizational information. If you don't have a Microsoft Entra ID, one is created when you create an Azure subscription.
      • An Azure subscription enables you to create instances of Azure services. IoT Central provisions resources in your subscription. If you don't have a subscription, you can create one for free. If you have a subscription, you can select it in the dropdown.
      • Location is the Azure geography in which you create an application. Select a location that's physically closest to your devices to get optimal performance. After you choose a location, you can't move the application to a different location.

    IoT Central new application dialog

  7. Select Create.

    After IoT Central creates the application, it redirects you to the application dashboard. IoT Central new application dashboard

Add a device

In this section, you add a new device to your IoT Central application. The device is an instance of a device template that represents a device that you'll connect to the application.

To create a new device:

  1. In the left pane select Devices, then select +New.

  2. Leave Device template set to Unassigned and Simulate this device? set to No.

  3. Set a friendly Device name and Device ID. Optionally, use the generated values. IoT Central new device dialog

  4. Select Create.

    The created device appears in the All devices list. IoT Central all devices list

To get connection details for the new device:

  1. In the All devices list, click the linked device name to display details.

  2. In the top menu, select Connect.

    The Device connection dialog displays the connection details: IoT Central device connection details

  3. Copy the following values from the Device connection dialog to a safe location. You'll use these values to connect your device to IoT Central.

    • ID scope
    • Device ID
    • Primary key

Run the device sample

In this section, you configure your local environment, install the Azure IoT Java device SDK, and run a sample that creates a temperature controller.

Configure your environment

  1. Open a console such as Windows CMD or Bash.

    Linux and Raspberry Pi OS

    Confirm that the JAVA_HOME (echo $JAVA_HOME) environment variable is set. This environment variable must be set to successfully build the SDK and samples. For information about setting JAVA_HOME, see Linux/Raspberry Pi Prerequisites.

  2. Set the following environment variables, using the appropriate commands for your console. The device uses these values to connect to IoT Central. For IOTHUB_DEVICE_DPS_ID_SCOPE, IOTHUB_DEVICE_DPS_DEVICE_KEY, and IOTHUB_DEVICE_DPS_DEVICE_ID, use the device connection values that you saved previously.

    Windows CMD

    set IOTHUB_DEVICE_SECURITY_TYPE=DPS
    set IOTHUB_DEVICE_DPS_ID_SCOPE=<application ID scope>
    set IOTHUB_DEVICE_DPS_DEVICE_KEY=<device primary key>
    set IOTHUB_DEVICE_DPS_DEVICE_ID=<your device ID>
    set IOTHUB_DEVICE_DPS_ENDPOINT=global.azure-devices-provisioning.net
    

    Note

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

    Bash

    export IOTHUB_DEVICE_SECURITY_TYPE='DPS'
    export IOTHUB_DEVICE_DPS_ID_SCOPE='<application ID scope>'
    export IOTHUB_DEVICE_DPS_DEVICE_KEY='<device primary key>'
    export IOTHUB_DEVICE_DPS_DEVICE_ID='<your device ID>'
    export IOTHUB_DEVICE_DPS_ENDPOINT='global.azure-devices-provisioning.net' 
    

Build and run the code

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

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

  3. Navigate to the sample directory.

    Windows

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

    Linux or Raspberry Pi OS

    cd device/iot-device-samples/pnp-device-sample/temperature-controller-device-sample
    
  4. Run the following code sample from the SDK. The sample creates a temperature controller with thermostat sensors.

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

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

    2021-05-13 15:39:26.411 DEBUG Mqtt:253 - Sending MQTT SUBSCRIBE packet for topic $iothub/twin/res/#
    2021-05-13 15:39:26.428 INFO  IotHubTransport:540 - Message was queued to be sent later ( Message details: Correlation Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] Message Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] Request Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] Device Operation Type [DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST] )
    2021-05-13 15:39:26.432 DEBUG TemperatureController:427 - Property: Update - component = "deviceInformation" is COMPLETED.
    2021-05-13 15:39:26.436 INFO  IotHubTransport:540 - Message was queued to be sent later ( Message details: Correlation Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] Message Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] )
    2021-05-13 15:39:26.438 DEBUG TemperatureController:438 - Telemetry: Sent - {"workingSet": 1024.0KiB }
    2021-05-13 15:39:26.439 INFO  IotHubTransport:540 - Message was queued to be sent later ( Message details: Correlation Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] Message Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] Request Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] Device Operation Type [DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST] )
    2021-05-13 15:39:26.439 DEBUG TemperatureController:446 - Property: Update - {"serialNumber": SR-123456} is COMPLETED
    2021-05-13 15:39:26.447 INFO  IotHubTransport:540 - Message was queued to be sent later ( Message details: Correlation Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] Message Id [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] )
    2021-05-13 15:39:26.447 DEBUG TemperatureController:465 - Telemetry: Sent - {"temperature": 44.4░C} with message Id xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
    

Browse code

In this quickstart, you learn a basic Azure IoT application development workflow. First you create an Azure IoT Central application for hosting devices. Then you use an Azure IoT device SDK sample to create a temperature controller, connect it securely to IoT Central, and send telemetry. The temperature controller sample application runs on your local machine and generates simulated sensor data to send to IoT Central.

Tip

As a developer, you have some options for how to connect devices to Azure IoT. To learn about connection options, see What is Azure IoT device and application development?.

Prerequisites

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

  • Windows 10
  • Ubuntu 20.04 LTS
  • Raspberry Pi OS (Raspbian) version 10, running on a Raspberry Pi 3 Model B+
  • An active Azure subscription. If you don't have an Azure subscription, create a free account before you begin.

Install the following prerequisites on your development machine:

  • Node.js version 6 or later. To check your version, run node --version in your console app.
  • Git.

Create an application

There are several ways to connect devices to Azure IoT. In this section, you learn how to connect a device by using Azure IoT Central. IoT Central is an IoT application platform that reduces the cost and complexity of managing devices in an IoT solution.

To create a new application:

  1. Browse to Azure IoT Central and sign in with a Microsoft personal, work, or school account.

  2. Navigate to Build and select Custom apps. IoT Central start page

  3. In Application name, enter a unique name or use the generated name.

  4. In URL, enter a memorable application URL prefix or use the generated URL prefix.

  5. Leave Application template set to Custom application.

  6. Select a Pricing plan option.

    • Select one of the standard pricing plans. Select your Directory, Azure subscription, and Location. To learn about pricing, see Azure IoT Central pricing.
      • Directory is the Microsoft Entra ID in which you create your application. A Microsoft Entra ID contains user identities, credentials, and other organizational information. If you don't have a Microsoft Entra ID, one is created when you create an Azure subscription.
      • An Azure subscription enables you to create instances of Azure services. IoT Central provisions resources in your subscription. If you don't have a subscription, you can create one for free. If you have a subscription, you can select it in the dropdown.
      • Location is the Azure geography in which you create an application. Select a location that's physically closest to your devices to get optimal performance. After you choose a location, you can't move the application to a different location.

    IoT Central new application dialog

  7. Select Create.

    After IoT Central creates the application, it redirects you to the application dashboard. IoT Central new application dashboard

Add a device

In this section, you add a new device to your IoT Central application. The device is an instance of a device template that represents a device that you'll connect to the application.

To create a new device:

  1. In the left pane select Devices, then select +New.

  2. Leave Device template set to Unassigned and Simulate this device? set to No.

  3. Set a friendly Device name and Device ID. Optionally, use the generated values. IoT Central new device dialog

  4. Select Create.

    The created device appears in the All devices list. IoT Central all devices list

To get connection details for the new device:

  1. In the All devices list, click the linked device name to display details.

  2. In the top menu, select Connect.

    The Device connection dialog displays the connection details: IoT Central device connection details

  3. Copy the following values from the Device connection dialog to a safe location. You'll use these values to connect your device to IoT Central.

    • ID scope
    • Device ID
    • Primary key

Run the device sample

In this section, you configure your local environment, install the Azure IoT Node.js device SDK, and run a sample that creates a temperature controller.

Configure your environment

  1. Open a console such as Windows CMD, PowerShell, or Bash.

  2. Set the following environment variables, using the appropriate commands for your console. The device uses these values to connect to IoT Central. For IOTHUB_DEVICE_DPS_ID_SCOPE, IOTHUB_DEVICE_DPS_DEVICE_KEY, and IOTHUB_DEVICE_DPS_DEVICE_ID, use the device connection values that you saved previously.

    CMD (Windows)

    set IOTHUB_DEVICE_SECURITY_TYPE=DPS
    set IOTHUB_DEVICE_DPS_ID_SCOPE=<application ID scope>
    set IOTHUB_DEVICE_DPS_DEVICE_KEY=<device primary key>
    set IOTHUB_DEVICE_DPS_DEVICE_ID=<your device ID>
    set IOTHUB_DEVICE_DPS_ENDPOINT=global.azure-devices-provisioning.net
    

    Note

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

    PowerShell

    $env:IOTHUB_DEVICE_SECURITY_TYPE='DPS'
    $env:IOTHUB_DEVICE_DPS_ID_SCOPE='<application ID scope>'
    $env:IOTHUB_DEVICE_DPS_DEVICE_KEY='<device primary key>'
    $env:IOTHUB_DEVICE_DPS_DEVICE_ID='<your device ID>'
    $env:IOTHUB_DEVICE_DPS_ENDPOINT='global.azure-devices-provisioning.net'
    

    Bash

    export IOTHUB_DEVICE_SECURITY_TYPE='DPS'
    export IOTHUB_DEVICE_DPS_ID_SCOPE='<application ID scope>'
    export IOTHUB_DEVICE_DPS_DEVICE_KEY='<device primary key>'
    export IOTHUB_DEVICE_DPS_DEVICE_ID='<your device ID>'
    export IOTHUB_DEVICE_DPS_ENDPOINT='global.azure-devices-provisioning.net' 
    

Install the SDK and samples

  1. Copy the Azure IoT Node.js device SDK to your local machine.

    git clone https://github.com/Azure/azure-iot-sdk-node
    
  2. 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
    
  3. Install the Azure IoT Node.js SDK and necessary dependencies:

    npm install
    

Run the code

  1. In your console, run the following code sample from the SDK. The sample creates a temperature controller with thermostat sensors.

    node pnp_temperature_controller.js
    

    After your 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:

    registration succeeded
    assigned hub=iotc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx.azure-devices.net
    deviceId=my-sdk-device
    payload=undefined
    Connecting using connection string: HostName=iotc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx.azure-devices.net;DeviceId=my-sdk-device;SharedAccessKey=xxxxxxxxxxxxxxxxxxxxxxxxxxx
    Enabling the commands on the client
    Please enter q or Q to exit sample.
    The following properties will be updated for root interface.
    { serialNumber: 'alwinexlepaho8329' }
    The following properties will be updated for component: thermostat1
    {
      thermostat1: { maxTempSinceLastReboot: 40.53506261527863, __t: 'c' }
    }
    The following properties will be updated for component: thermostat2
    {
      thermostat2: { maxTempSinceLastReboot: 89.55136974144273, __t: 'c' }
    }
    The following properties will be updated for component: deviceInformation
    {
      deviceInformation: {
        manufacturer: 'Contoso Device Corporation',
        model: 'Contoso 47-turbo',
        swVersion: '10.89',
        osName: 'Contoso_OS',
        processorArchitecture: 'Contoso_x86',
        processorManufacturer: 'Contoso Industries',
        totalStorage: 65000,
        totalMemory: 640,
        __t: 'c'
      }
    }
    executed sample
    Received an update for device with value: {"$version":1}
    Properties have been reported for root interface.
    Properties have been reported for component: thermostat1
    Properties have been reported for component: thermostat2
    Properties have been reported for component: deviceInformation
    Sending telemetry message 0 from component: thermostat1
    Sending telemetry message 0 from component: thermostat2
    

Browse code

In this quickstart, you learn a basic Azure IoT application development workflow. First you create an Azure IoT Central application for hosting devices. Then you use an Azure IoT device SDK sample to create a temperature controller, connect it securely to IoT Central, and send telemetry. The temperature controller sample application runs on your local machine and generates simulated sensor data to send to IoT Central.

Tip

As a developer, you have some options for how to connect devices to Azure IoT. To learn about connection options, see What is Azure IoT device and application development?.

Prerequisites

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

  • Windows 10 or Windows 11
  • Ubuntu 20.04 LTS
  • Raspberry Pi OS (Raspbian) version 10, running on a Raspberry Pi 3 Model B+
  • An active Azure subscription. If you don't have an Azure subscription, create a free account before you begin.

Install the following prerequisites on your development machine:

  • Python version 3.7 or later. To check your Python version, run python3 --version.
  • Git.

Create an application

There are several ways to connect devices to Azure IoT. In this section, you learn how to connect a device by using Azure IoT Central. IoT Central is an IoT application platform that reduces the cost and complexity of managing devices in an IoT solution.

To create a new application:

  1. Browse to Azure IoT Central and sign in with a Microsoft personal, work, or school account.

  2. Navigate to Build and select Custom apps. IoT Central start page

  3. In Application name, enter a unique name or use the generated name.

  4. In URL, enter a memorable application URL prefix or use the generated URL prefix.

  5. Leave Application template set to Custom application.

  6. Select a Pricing plan option.

    • Select one of the standard pricing plans. Select your Directory, Azure subscription, and Location. To learn about pricing, see Azure IoT Central pricing.
      • Directory is the Microsoft Entra ID in which you create your application. A Microsoft Entra ID contains user identities, credentials, and other organizational information. If you don't have a Microsoft Entra ID, one is created when you create an Azure subscription.
      • An Azure subscription enables you to create instances of Azure services. IoT Central provisions resources in your subscription. If you don't have a subscription, you can create one for free. If you have a subscription, you can select it in the dropdown.
      • Location is the Azure geography in which you create an application. Select a location that's physically closest to your devices to get optimal performance. After you choose a location, you can't move the application to a different location.

    IoT Central new application dialog

  7. Select Create.

    After IoT Central creates the application, it redirects you to the application dashboard. IoT Central new application dashboard

Add a device

In this section, you add a new device to your IoT Central application. The device is an instance of a device template that represents a device that you'll connect to the application.

To create a new device:

  1. In the left pane select Devices, then select +New.

  2. Leave Device template set to Unassigned and Simulate this device? set to No.

  3. Set a friendly Device name and Device ID. Optionally, use the generated values. IoT Central new device dialog

  4. Select Create.

    The created device appears in the All devices list. IoT Central all devices list

To get connection details for the new device:

  1. In the All devices list, click the linked device name to display details.

  2. In the top menu, select Connect.

    The Device connection dialog displays the connection details: IoT Central device connection details

  3. Copy the following values from the Device connection dialog to a safe location. You'll use these values to connect your device to IoT Central.

    • ID scope
    • Device ID
    • Primary key

Run the device sample

In this section, you configure your local environment, install the Azure IoT Python device SDK, and run a sample that creates a temperature controller.

Configure your environment

  1. Open a console such as Windows CMD, PowerShell, or Bash.

  2. Set the following environment variables, using the appropriate commands for your console. The device uses these values to connect to IoT Central. For IOTHUB_DEVICE_DPS_ID_SCOPE, IOTHUB_DEVICE_DPS_DEVICE_KEY, and IOTHUB_DEVICE_DPS_DEVICE_ID, use the device connection values that you saved previously.

    CMD (Windows)

    set IOTHUB_DEVICE_SECURITY_TYPE=DPS
    set IOTHUB_DEVICE_DPS_ID_SCOPE=<application ID scope>
    set IOTHUB_DEVICE_DPS_DEVICE_KEY=<device primary key>
    set IOTHUB_DEVICE_DPS_DEVICE_ID=<your device ID>
    set IOTHUB_DEVICE_DPS_ENDPOINT=global.azure-devices-provisioning.net
    

    Note

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

    PowerShell

    $env:IOTHUB_DEVICE_SECURITY_TYPE='DPS'
    $env:IOTHUB_DEVICE_DPS_ID_SCOPE='<application ID scope>'
    $env:IOTHUB_DEVICE_DPS_DEVICE_KEY='<device primary key>'
    $env:IOTHUB_DEVICE_DPS_DEVICE_ID='<your device ID>'
    $env:IOTHUB_DEVICE_DPS_ENDPOINT='global.azure-devices-provisioning.net'
    

    Bash

    export IOTHUB_DEVICE_SECURITY_TYPE='DPS'
    export IOTHUB_DEVICE_DPS_ID_SCOPE='<application ID scope>'
    export IOTHUB_DEVICE_DPS_DEVICE_KEY='<device primary key>'
    export IOTHUB_DEVICE_DPS_DEVICE_ID='<your device ID>'
    export IOTHUB_DEVICE_DPS_ENDPOINT='global.azure-devices-provisioning.net' 
    

Install the SDK and samples

  1. Copy the Azure IoT Python device SDK to your local machine.

    git clone --branch v2 https://github.com/Azure/azure-iot-sdk-python
    
  2. 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
    
  3. Install the Azure IoT Python SDK.

    pip3 install azure-iot-device
    

Run the code

  1. In your console, run the following code sample from the SDK. The sample creates a temperature controller with thermostat sensors.

    python temp_controller_with_thermostats.py
    

    After your 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:

    Device was assigned
    iotc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.azure-devices.net
    my-sdk-device
    Updating pnp properties for root interface
    {'serialNumber': 'alohomora'}
    Updating pnp properties for thermostat1
    {'thermostat1': {'maxTempSinceLastReboot': 98.34, '__t': 'c'}}
    Updating pnp properties for thermostat2
    {'thermostat2': {'maxTempSinceLastReboot': 48.92, '__t': 'c'}}
    Updating pnp properties for deviceInformation
    {'deviceInformation': {'swVersion': '5.5', 'manufacturer': 'Contoso Device Corporation', 'model': 'Contoso 4762B-turbo', 'osName': 'Mac Os', 'processorArchitecture': 'x86-64', 'processorManufacturer': 'Intel', 'totalStorage': 1024, 'totalMemory': 32, '__t': 'c'}}
    Listening for command requests and property updates
    Press Q to quit
    Sending telemetry from various components
    Sent message
    {"temperature": 33}
    

View telemetry

After the device connects to IoT Central, it begins sending telemetry. You can view the telemetry and other details about connected devices in IoT Central.

In IoT Central, select Devices, select your device name, then select the Overview tab. This view displays a graph of the temperatures from the two thermostat devices.

IoT Central device telemetry overview

Select the Raw data tab. This view displays the telemetry each time a thermostat reading is sent.

IoT Central device telemetry raw output

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

Clean up resources

If you no longer need the IoT Central resources created in this quickstart, you can delete them. Optionally, if you plan to continue following the documentation in this guide, you can keep the application you created and reuse it for other samples.

To remove the Azure IoT Central sample application and all its devices and resources:

  1. Select Administration > Your application.
  2. Select Delete.

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 IoT Central to create an application and a device instance. Then you used an Azure IoT device SDK to create a temperature controller, connect to IoT Central, and send telemetry. You also used IoT Central to monitor the telemetry.

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