Quickstart: Send telemetry from a device to an IoT hub and read the telemetry from the hub with a back-end application (C#)

IoT Hub is an Azure service that enables you to ingest high volumes of telemetry from your IoT devices into the cloud for storage or processing. In this quickstart, you send telemetry from a simulated device application, through IoT Hub, to a back-end application for processing.

The quickstart uses two pre-written C# applications, one to send the telemetry and one to read the telemetry from the hub. Before you run these two applications, you create an IoT hub and register a device with the hub.

Open Azure Cloud Shell

Azure Cloud Shell is a free, interactive shell that you can use to run the steps in this article. Common Azure tools are preinstalled and configured in Cloud Shell for you to use with your account. Just select the Copy button to copy the code, paste it in Cloud Shell, and then press Enter to run it. There are a few ways to open Cloud Shell:

Select Try It in the upper-right corner of a code block. Cloud Shell in this article
Open Cloud Shell in your browser. https://shell.azure.com/bash
Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. Cloud Shell in the portal

If you don’t have an Azure subscription, create a free account before you begin.

Prerequisites

The two sample applications you run in this quickstart are written using C#. You need the .NET Core SDK 2.1.0 or greater on your development machine.

You can download the .NET Core SDK for multiple platforms from .NET.

You can verify the current version of C# on your development machine using the following command:

dotnet --version

Download the sample C# project from https://github.com/Azure-Samples/azure-iot-samples-csharp/archive/master.zip and extract the ZIP archive.

Create an IoT hub

The first step is to use the Azure portal to create an IoT hub in your subscription. The IoT hub enables you to ingest high volumes of telemetry into the cloud from many devices. The hub then enables one or more back-end services running in the cloud to read and process that telemetry.

  1. Sign in to the Azure portal.

  2. Select Create a resource > Internet of Things > IoT Hub.

    Select to install IoT Hub

  3. In the IoT hub pane, enter the following information for your IoT hub:

    • Subscription: Choose the subscription that you want to use to create this IoT hub.
    • Resource group: Create a resource group to contain the IoT hub or use an existing one. By putting all related resources in a group together, such as TestResources, you can manage them all together. For example, deleting the resource group deletes all resources contained in that group. For more information, see Use resource groups to manage your Azure resources.
    • Region: Select the closest location to your devices.
    • Name: Create a unique name for your IoT hub. If the name you enter is available, a green check mark appears.

    Important

    The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming it.

    IoT Hub basics window

  4. Select Next: Size and scale to continue creating your IoT hub.

  5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your subscription. For more information, see the Pricing and scale tier.

    IoT Hub size and scale window

  6. Select Review + create.

  7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You can monitor the progress in the Notifications pane.

Register a device

A device must be registered with your IoT hub before it can connect. In this quickstart, you use the Azure CLI to register a simulated device.

  1. Add the IoT Hub CLI extension and create the device identity. Replace {YourIoTHubName} with the name you chose for your IoT hub:

    az extension add --name azure-cli-iot-ext
    az iot hub device-identity create --hub-name {YourIoTHubName} --device-id MyDotnetDevice
    

    If you choose a different name for your device, update the device name in the sample applications before you run them.

  2. Run the following command to get the device connection string for the device you just registered:

    az iot hub device-identity show-connection-string --hub-name {YourIoTHubName} --device-id MyDotnetDevice --output table
    

    Make a note of the device connection string, which looks like Hostname=...=. You use this value later in the quickstart.

  3. You also need the Event Hubs-compatible endpoint, Event Hubs-compatible path, and iothubowner primary key from your IoT hub to enable the back-end application to connect to your IoT hub and retrieve the messages. The following commands retrieve these values for your IoT hub:

    az iot hub show --query properties.eventHubEndpoints.events.endpoint --name {YourIoTHubName}
    
    az iot hub show --query properties.eventHubEndpoints.events.path --name {YourIoTHubName}
    
    az iot hub policy show --name iothubowner --query primaryKey --hub-name {your IoT Hub name}
    

    Make a note of these three values, which you use later in the quickstart.

Send simulated telemetry

The simulated device application connects to a device-specific endpoint on your IoT hub and sends simulated temperature and humidity telemetry.

  1. In a terminal window, navigate to the root folder of the sample C# project. Then navigate to the iot-hub\Quickstarts\simulated-device folder.

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

    Replace the value of the s_connectionString variable with the device connection string you made a note of previously. Then save your changes to SimulatedDevice.cs file.

  3. In the terminal window, run the following commands to install the required packages for simulated device application:

    dotnet restore
    
  4. In the terminal window, run the following command to build and run the simulated device application:

    dotnet run
    

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

    Run the simulated device

Read the telemetry from your hub

The back-end application connects to the service-side Events endpoint on your IoT Hub. The application receives the device-to-cloud messages sent from your simulated device. An IoT Hub back-end application typically runs in the cloud to receive and process device-to-cloud messages.

  1. In another terminal window, navigate to the root folder of the sample C# project. Then navigate to the iot-hub\Quickstarts\read-d2c-messages folder.

  2. Open the ReadDeviceToCloudMessages.cs file in a text editor of your choice. Update the following variables and save your changes to the file.

    Variable Value
    s_eventHubsCompatibleEndpoint Replace the value of the variable with the Event Hubs-compatible endpoint you made a note of previously.
    s_eventHubsCompatiblePath Replace the value of the variable with the Event Hubs-compatible path you made a note of previously.
    s_iotHubSasKey Replace the value of the variable with the iothubowner primary key you made a note of previously.
  3. In the terminal window, run the following commands to install the required libraries for the back-end application:

    dotnet restore
    
  4. In the terminal window, run the following commands to build and run the back-end application:

    dotnet run
    

    The following screenshot shows the output as the back-end application receives telemetry sent by the simulated device to the hub:

    Run the back-end application

Clean up resources

If you will be continuing to the next recommended article, you can keep the resources you've already created and reuse them.

Otherwise, you can delete the Azure resources created in this article to avoid charges.

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. If you created the IoT Hub inside an existing resource group that contains resources you want to keep, only delete the IoT Hub resource itself instead of deleting the resource group.

To delete a resource group by name:

  1. Sign in to the Azure portal and click Resource groups.

  2. In the Filter by name... textbox, type the name of the resource group containing your IoT Hub.

  3. To the right of your resource group in the result list, click ... then Delete resource group.

    Delete

  4. You will be asked to confirm the deletion of the resource group. Type the name of your resource group again to confirm, and then click Delete. After a few moments, the resource group and all of its contained resources are deleted.

Next steps

In this quickstart, you've setup an IoT hub, registered a device, sent simulated telemetry to the hub using a C# application, and read the telemetry from the hub using a simple back-end application.

To learn how to control your simulated device from a back-end application, continue to the next quickstart.