快速入门:将遥测数据从设备发送到 IoT 中心并使用后端应用程序读取该数据 (C#)Quickstart: Send telemetry from a device to an IoT hub and read it with a back-end application (C#)

IoT 中心是一项 Azure 服务,用于将大量遥测数据从 IoT 设备引入云中进行存储或处理。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. 本快速入门会将模拟设备应用程序的遥测数据通过 IoT 中心发送到后端应用程序进行处理。In this quickstart, you send telemetry from a simulated device application, through IoT Hub, to a back-end application for processing.

本快速入门使用两个预先编写的 C# 应用程序,一个用于发送遥测数据,一个用于从中心读取遥测数据。The quickstart uses two pre-written C# applications, one to send the telemetry and one to read the telemetry from the hub. 运行这两个应用程序前,请先创建 IoT 中心并在中心注册设备。Before you run these two applications, you create an IoT hub and register a device with the hub.

如果没有 Azure 订阅,请在开始前创建一个试用帐户If you don’t have an Azure subscription, create a trial account before you begin.

先决条件Prerequisites

本快速入门中运行的两个示例应用程序是使用 C# 编写的。The two sample applications you run in this quickstart are written using C#. 开发计算机上需要有 .NET Core SDK 2.1.0 或更高版本。You need the .NET Core SDK 2.1.0 or greater on your development machine.

可以从 .NET 为多个平台下载 .NET Core SDK。You can download the .NET Core SDK for multiple platforms from .NET.

可以使用以下命令验证开发计算机上 C# 的当前版本:You can verify the current version of C# on your development machine using the following command:

dotnet --version

https://github.com/Azure-Samples/azure-iot-samples-csharp/archive/master.zip 下载示例 C# 项目并提取 ZIP 存档。Download the sample C# project from https://github.com/Azure-Samples/azure-iot-samples-csharp/archive/master.zip and extract the ZIP archive.

创建 IoT 中心Create an IoT hub

此部分介绍如何使用 Azure 门户创建 IoT 中心。This section describes how to create an IoT hub using the Azure portal.

  1. 登录到 Azure 门户Log in to the Azure portal.

  2. 选择“+创建资源”,然后搜索市场,寻找 IoT 中心Choose +Create a resource, then Search the Marketplace for the IoT Hub.

  3. 选择“IoT 中心”,然后单击“创建”按钮 。Select IoT Hub and click the Create button. 随即显示 IoT 中心创建过程的第一个屏幕。You see the first screen for creating an IoT hub.

    在 Azure 门户中创建中心

    填充字段。Fill in the fields.

    订阅:请选择要用于 IoT 中心的订阅。Subscription: Select the subscription to use for your IoT hub.

    资源组:可创建新的资源组或使用现有资源组。Resource Group: You can create a new resource group or use an existing one. 若要新建一个,请单击“新建” ,并填写要使用的名称。To create a new one, click Create new and fill in the name you want to use. 若要使用现有资源组,请单击“使用现有资源组” 并从下拉列表中选择该组。To use an existing resource group, click Use existing and select the resource group from the dropdown list. 有关详细信息,请参阅管理 Azure 资源管理器资源组For more information, see Manage Azure Resource Manager resource groups.

    区域:这是要在其中设置中心的区域。Region: This is the region in which you want your hub to be located. 从下拉列表中选择最靠近自己的位置。Select the location closest to you from the dropdown list.

    IoT 中心名称:输入 IoT 中心的名称。IoT Hub Name: Put in the name for your IoT Hub. 此名称必须全局唯一。This name must be globally unique. 如果输入的名称可用,会显示一个绿色复选标记。If the name you enter is available, a green check mark appears.

    Important

    IoT 中心将公开为 DNS 终结点,因此,命名时请务必避免包含任何敏感信息。The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming it.

  4. 单击“下一步: 大小和规模”,以便继续创建 IoT 中心。Click Next: Size and scale to continue creating your IoT hub.

    使用 Azure 门户为新的 IoT 中心设置大小和缩放级别

    在此屏幕上,可以采用默认值,只需在底部单击“查看+创建”即可 。On this screen, you can take the defaults and just click Review + create at the bottom.

    定价和缩放层:可以根据你需要的功能数以及每天通过解决方案发送的消息数从多个层中进行选择。Pricing and scale tier: You can choose from several tiers depending on how many features you want and how many messages you send through your solution per day. 免费层适用于测试和评估。The free tier is intended for testing and evaluation. 它允许 500 台设备连接到 IoT 中心,并且每天最多传输 8,000 条信息。It allows 500 devices to be connected to the IoT hub and up to 8,000 messages per day. 每个 Azure 订阅可以在免费层中创建一个 IoT 中心。Each Azure subscription can create one IoT Hub in the free tier.

    IoT 中心单元:每个单位每日允许的消息数取决于中心的定价层。IoT Hub units: The number of messages allowed per unit per day depends on your hub's pricing tier. 例如,如果希望 IoT 中心支持 700,000 条消息输入,则选择两个 S1 层单位。For example, if you want the IoT hub to support ingress of 700,000 messages, you choose two S1 tier units.

    有关其他层选项的详细信息,请参阅选择合适的 IoT 中心层For details about the other tier options, see Choosing the right IoT Hub tier.

    高级 / 设备到云分区:此属性将设备到云消息与这些消息的同步读取器数目相关联。Advanced / Device-to-cloud partitions: This property relates the device-to-cloud messages to the number of simultaneous readers of the messages. 大多数 IoT 中心只需要 4 个分区。Most IoT hubs only need four partitions.

  5. 单击“查看+创建” 可查看选择。Click Review + create to review your choices. 会显示类似于以下的屏幕。You see something similar to this screen.

    查看用于创建新 IoT 中心的信息

  6. 单击“创建”以创建新的 IoT 中心 。Click Create to create your new IoT hub. 创建中心需要几分钟时间。Creating the hub takes a few minutes.

注册设备Register a device

必须先将设备注册到 IoT 中心,然后该设备才能进行连接。A device must be registered with your IoT hub before it can connect. 在本快速入门中,请使用 Azure CLI 来注册模拟设备。In this quickstart, you use the Azure CLI to register a simulated device.

  1. 在终端窗口 Shell 中运行以下命令,以添加 IoT 中心 CLI 扩展并创建设备标识。Run the following commands in terminal window Shell to add the IoT Hub CLI extension and to create the device identity.

    YourIoTHubName:将下面的占位符替换为你为 IoT 中心选择的名称。YourIoTHubName : Replace this placeholder below with the name you choose for your IoT hub.

    MyDotnetDevice:所注册的设备的名称。MyDotnetDevice: The name of the device you're registering. 请按显示的方法使用 MyDotnetDevice。Use MyDotnetDevice as shown. 如果为设备选择其他名称,则需要在本文中从头至尾使用该名称,并在运行示例应用程序之前在其中更新设备名称。If you choose a different name for your device, you need to use that name throughout this article, and update the device name in the sample applications before you run them.

    az extension add --name azure-cli-iot-ext
    az iot hub device-identity create --hub-name YourIoTHubName --device-id MyDotnetDevice
    
  2. 运行以下命令,获取刚注册设备的 设备连接字符串Run the following commands to get the device connection string for the device you just registered:

    YourIoTHubName:将下面的占位符替换为你为 IoT 中心选择的名称。YourIoTHubName : Replace this placeholder below with the name you choose for your IoT hub.

    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={YourIoTHubName}.azure-devices.cn;DeviceId=MyNodeDevice;SharedAccessKey={YourSharedAccessKey}

    稍后会在快速入门中用到此值。You use this value later in the quickstart.

  3. 还需要来自 IoT 中心的与事件中心兼容的终结点、与事件中心兼容的路径和 iothubowner 主键,确保后端应用程序能连接到 IoT 中心并检索消息。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. 以下命令可检索 IoT 中心的这些值:The following commands retrieve these values for your IoT hub:

    YourIoTHubName:将下面的占位符替换为你为 IoT 中心选择的名称。YourIoTHubName : Replace this placeholder below with the name you choose 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 YourIoTHubName
    

    记下这三个值,稍后会在快速入门中用到这些值。Make a note of these three values, which you use later in the quickstart.

发送模拟遥测数据Send simulated telemetry

模拟设备应用程序会连接到 IoT 中心上特定于设备的终结点,并发送模拟的温度和湿度遥测数据。The simulated device application connects to a device-specific endpoint on your IoT hub and sends simulated temperature and humidity telemetry.

  1. 在本地终端窗口中,导航到示例 C# 项目的根文件夹。In a local terminal window, navigate to the root folder of the sample C# project. 然后导航到 iot-hub\Quickstarts\simulated-device 文件夹。Then navigate to the iot-hub\Quickstarts\simulated-device folder.

  2. 在所选文本编辑器中打开 SimulatedDevice.cs 文件。Open the SimulatedDevice.cs file in a text editor of your choice.

    s_connectionString 变量的值替换为之前记下的设备连接字符串。Replace the value of the s_connectionString variable with the device connection string you made a note of previously. 然后将更改保存到 SimulatedDevice.cs 文件。Then save your changes to SimulatedDevice.cs file.

  3. 在本地终端窗口中,运行以下命令以安装模拟设备应用程序所需的包:In the local terminal window, run the following commands to install the required packages for simulated device application:

    dotnet restore
    
  4. 在本地终端窗口中,运行以下命令,生成并运行模拟设备应用程序:In the local terminal window, run the following command to build and run the simulated device application:

    dotnet run
    

    以下屏幕截图显示了模拟设备应用程序将遥测数据发送到 IoT 中心后的输出:The following screenshot shows the output as the simulated device application sends telemetry to your IoT hub:

    运行模拟设备

从中心读取遥测数据Read the telemetry from your hub

后端应用程序会连接到 IoT 中心上的服务端“事件”终结点。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. IoT 中心后端应用程序通常在云中运行,接收和处理设备到云的消息。An IoT Hub back-end application typically runs in the cloud to receive and process device-to-cloud messages.

  1. 在另一本地终端窗口中,导航到示例 C# 项目的根文件夹。In another local terminal window, navigate to the root folder of the sample C# project. 然后导航到 iot-hub\Quickstarts\read-d2c-messages 文件夹。Then navigate to the iot-hub\Quickstarts\read-d2c-messages folder.

  2. 在所选文本编辑器中打开 ReadDeviceToCloudMessages.cs 文件。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 将变量的值替换为之前记下的 iothubowner 主键。Replace the value of the variable with the iothubowner primary key you made a note of previously.
  3. 在本地终端窗口中,运行以下命令,安装后端应用程序所需的库:In the local terminal window, run the following commands to install the required libraries for the back-end application:

    dotnet restore
    
  4. 在本地终端窗口中,运行以下命令,生成并运行后端应用程序:In the local terminal window, run the following commands to build and run the back-end application:

    dotnet run
    

    以下屏幕截图显示了后端应用程序接收模拟设备发送到 IoT 中心的遥测数据后的输出:The following screenshot shows the output as the back-end application receives telemetry sent by the simulated device to the hub:

    运行后端应用程序

清理资源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.

否则,可删除本文中创建的 Azure 资源,避免收费。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. 如果在现有的包含要保留资源的资源组中创建了 IoT 中心,则只删除 IoT 中心资源本身,而不要删除资源组。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. 登录到 Azure 门户,然后单击“资源组”。Sign in to the Azure portal and click Resource groups.

  2. 在“按名称筛选...”文本框中键入包含 IoT 中心的资源组的名称。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

本快速入门设置了 IoT 中心、注册了设备、使用 C# 应用程序发送了模拟遥测数据到中心,并使用简单的后端应用程序读取中心的遥测数据。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.