快速入门:将遥测数据从设备发送到 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 的 Azure IoT 设备 SDK 中的 C 示例应用程序向 IoT 中心发送遥测数据。The quickstart uses a C sample application from the Azure IoT device SDK for C to send telemetry to an IoT hub. Azure IoT 设备 SDK 以 ANSI C (C99) 编写,具有可移植性和广泛的平台兼容性。The Azure IoT device SDKs are written in ANSI C (C99) for portability and broad platform compatibility. 在运行示例代码之前,需要创建一个 IoT 中心并将模拟设备注册到该中心。Before running the sample code, you will create an IoT hub and register the simulated device with that hub.

本文针对 Windows 编写,但也可以在 Linux 上完成本快速入门。This article written for Windows but you can complete this quickstart on Linux as well.

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


  • 安装 Visual Studio 2019 并启用“使用 C++ 的桌面开发”工作负荷。Install Visual Studio 2019 with the 'Desktop development with C++' workload enabled.

  • 安装最新版本的 GitInstall the latest version of Git.

  • 运行以下命令将用于 Azure CLI 的 Azure IoT 扩展添加。Run the following command to add the Azure IoT Extension for Azure CLI. IOT 扩展会将 IoT 中心、IoT Edge 和 IoT 设备预配服务 (DPS) 特定的命令添加到 Azure CLI。The IOT Extension adds IoT Hub, IoT Edge, and IoT Device Provisioning Service (DPS) specific commands to Azure CLI.

    az extension add --name azure-cli-iot-ext

准备开发环境Prepare the development environment

在本快速入门中,我们将使用适用于 C 的 Azure IoT 设备 SDKFor this quickstart, you'll be using the Azure IoT device SDK for C.

可以通过安装适用于以下环境的包和库来使用该 SDK:You can use the SDK by installing the packages and libraries for the following environments:

  • Linux:apt-get 包适用于使用以下 CPU 体系结构的 Ubuntu 16.04 和 18.04:amd64、arm64、armhf 和 i386。Linux: apt-get packages are available for Ubuntu 16.04 and 18.04 using the following CPU architectures: amd64, arm64, armhf and i386. 有关详细信息,请参阅在 Ubuntu 上使用 apt-get 创建 C 设备客户端项目For more information, see Using apt-get to create a C device client project on Ubuntu.

  • mbed:对于在 mbed 平台上创建设备应用程序的开发人员,我们发布了可帮助在几分钟内学会使用 Azure IoT 中心的库和示例。mbed: For developers creating device applications on the mbed platform, we've published a library and samples that will get you started in minutes witH Azure IoT Hub. 有关详细信息,请参阅使用 mbed 库For more information, see Use the mbed library.

  • Arduino:如果在 Arduino 上进行开发,则可以利用 Arduino IDE 库管理器提供的 Azure IoT 库。Arduino: If you're developing on Arduino, you can leverage the Azure IoT library available in the Arduino IDE library manager. 有关详细信息,请参阅适用于 Arduino 的 Azure IoT 中心库For more information, see The Azure IoT Hub library for Arduino.

  • iOS:IoT 中心设备 SDK 能够以 CocoaPods 的形式提供,用于 Mac 和 iOS 设备开发。iOS: The IoT Hub Device SDK is available as CocoaPods for Mac and iOS device development. 有关详细信息,请参阅适用于 Azure IoT 的 iOS 示例For more information, see iOS Samples for Azure IoT.

但在本快速入门中,你将准备一个用于从 GitHub 克隆和生成 Azure IoT C SDK 的开发环境。However, in this quickstart, you'll prepare a development environment used to clone and build the Azure IoT C SDK from GitHub. GitHub 上的 SDK 包含本快速入门中使用的示例代码。The SDK on GitHub includes the sample code used in this quickstart.

  1. 下载 CMake 生成系统Download the CMake build system.

    在进行 CMake 安装之前,必须在计算机上安装 Visual Studio 必备组件(Visual Studio 和“使用 C++ 的桌面开发”工作负载) 。It's important that the Visual Studio prerequisites (Visual Studio and the 'Desktop development with C++' workload) are installed on your machine, before starting the CMake installation. 满足先决条件并验证下载内容后,安装 CMake 生成系统。Once the prerequisites are in place, and the download is verified, install the CMake build system.

  2. 打开命令提示符或 Git Bash shell,并导航到要将 Azure IoT C SDK 克隆到的工作目录。Open a command prompt or Git Bash shell and navigate to a working directory that you want to clone the Azure IoT C SDK into. 执行以下命令克隆 Azure IoT C SDK GitHub 存储库:Execute the following command to clone the Azure IoT C SDK GitHub repository:

    git clone https://github.com/Azure/azure-iot-sdk-c.git --recursive

    应该预料到此操作需要几分钟才能完成。You should expect this operation to take several minutes to complete.

  3. 在 git 存储库的根目录中创建 cmake 子目录,并导航到该文件夹。Create a cmake subdirectory in the root directory of the git repository, and navigate to that folder. 在工作目录中输入以下命令:Enter the following commands from your working directory:

    cd azure-iot-sdk-c
    mkdir cmake
    cd cmake
  4. 运行以下命令,以生成特定于你的开发客户端平台的 SDK 版本。Run the following command to build a version of the SDK specific to your development client platform. 将在 cmake 目录中生成模拟设备的 Visual Studio 解决方案。A Visual Studio solution for the simulated device will be generated in the cmake directory.

    cmake ..

    如果 cmake 找不到 C++ 编译器,则可能会在运行以上命令时出现生成错误。If cmake doesn't find your C++ compiler, you might get build errors while running the above command. 如果出现这种情况,请尝试在 Visual Studio 命令提示符窗口中运行该命令。If that happens, try running this command in the Visual Studio command prompt.

    生成成功后,最后的几个输出行如下所示:Once the build succeeds, the last few output lines will look similar to the following output:

    $ cmake ..
    -- Building for: Visual Studio 15 2017
    -- Selecting Windows SDK version 10.0.16299.0 to target Windows 10.0.17134.
    -- The C compiler identification is MSVC 19.12.25835.0
    -- The CXX compiler identification is MSVC 19.12.25835.0
    -- Configuring done
    -- Generating done
    -- Build files have been written to: E:/IoT Testing/azure-iot-sdk-c/cmake

创建 IoT 中心Create an IoT hub

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

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

  2. 选择“创建资源”,然后在“搜索市场”字段中输入“IoT 中心”。 Choose Create a resource, and then enter IoT Hub in the Search the Marketplace field.

  3. 在搜索结果中选择“IoT 中心”,然后选择“创建” 。Select IoT Hub from the search results, and then select Create.

  4. 在“基本信息”选项卡上,按如下所示填写字段: On the Basics tab, complete the fields as follows:

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

    • 资源组:选择一个资源组或新建一个资源组。Resource Group: Select a resource group or create a new one. 若要新建资源组,请选择“新建”并填写要使用的名称。 To create a new one, select Create new and fill in the name you want to use. 若要使用现有的资源组,请选择它。To use an existing resource group, select that resource group. 有关详细信息,请参阅管理 Azure 资源管理器资源组For more information, see Manage Azure Resource Manager resource groups.

    • 区域:选择中心所在的区域。Region: Select the region in which you want your hub to be located. 选择最靠近你的位置。Select the location closest to you.

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


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

    在 Azure 门户中创建中心

  5. 在完成时选择“下一步: 大小和规模”,以继续创建中心。Select Next: Size and scale to continue creating your hub.

    使用 Azure 门户为新的中心设置大小和规模

    在此屏幕中可以设置以下值:This screen allows you to set the following values:

    • 定价和缩放层:选择的层。Pricing and scale tier: Your selected 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 台设备连接到中心,每天最多可传输 8,000 条消息。It allows 500 devices to be connected to the 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. 例如,如果希望中心支持 700,000 条消息引入,请选择两个 S1 层单位。For example, if you want the 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 Settings > Device-to-cloud partitions: This property relates the device-to-cloud messages to the number of simultaneous readers of the messages. 大多数中心只需要 4 个分区。Most hubs need only four partitions.

  6. 对于本文,请接受默认选项,然后选择“查看 + 创建”以查看所做的选择。 For this article, accept the default choices, and then select Review + create to review your choices. 会显示类似于以下的屏幕。You see something similar to this screen.


  7. 选择“创建”以创建新的中心 。Select Create to create your new 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. 在本部分,我们将结合 IoT 扩展使用 Azure CLI 来注册模拟设备。In this section, you will use the Azure CLI with the IoT extension to register a simulated device.

  1. 运行以下命令,以添加 IoT 中心 CLI 扩展并创建设备标识。Run the following commands 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.

    MyCDevice:这是为注册的设备提供的名称。MyCDevice: This is the name given for the registered device. 请按显示的方法使用 MyCDevice。Use MyCDevice as shown. 如果为设备选择不同名称,则可能还需要在本文中从头至尾使用该名称,并在运行示例应用程序之前在其中更新设备名称。If you choose a different name for your device, you'll also need to use that name throughout this article, and update the device name in the sample applications before you run them.

    az iot hub device-identity create --hub-name YourIoTHubName --device-id MyCDevice
  2. 运行以下命令,获取你注册的设备的_设备连接字符串_:Run the following commands to get the device connection string for the device you 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 MyCDevice --output table

    记下如下所示的设备连接字符串:Make a note of the device connection string, which looks like:


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

发送模拟遥测数据Send simulated telemetry

模拟设备应用程序将连接到 IoT 中心上特定于设备的终结点,并发送模拟遥测数据形式的字符串。The simulated device application connects to a device-specific endpoint on your IoT hub and sends a string as simulated telemetry.

  1. 使用文本编辑器打开 iothub_convenience_sample.c 源文件,并查看用于发送遥测数据的示例代码。Using a text editor, open the iothub_convenience_sample.c source file and review the sample code for sending telemetry. 该文件位于克隆 Azure IoT C SDK 的工作目录下的以下位置:The file is located in the following location under the working directory where you cloned the Azure IoT C SDK:

  2. 找到 connectionString 常量的声明:Find the declaration of the connectionString constant:

    /* Paste in your device connection string  */
    static const char* connectionString = "[device connection string]";

    connectionString 常量的值替换为之前记下的设备连接字符串。Replace the value of the connectionString constant with the device connection string you made a note of previously. 然后保存对 iothub_convenience_sample.c 所做的更改。Then save your changes to iothub_convenience_sample.c.

  3. 在本地终端窗口中,导航到在 Azure IoT C SDK 中创建的 CMake 目录中的 iothub_convenience_sample 项目目录 。In a local terminal window, navigate to the iothub_convenience_sample project directory in the CMake directory that you created in the Azure IoT C SDK. 在工作目录中输入以下命令:Enter the following command from your working directory:

    cd azure-iot-sdk-c/cmake/iothub_client/samples/iothub_convenience_sample
  4. 在本地终端窗口中运行 CMake 以使用更新的 connectionString 值生成示例:Run CMake in your local terminal window to build the sample with your updated connectionString value:

    cmake --build . --target iothub_convenience_sample --config Debug
  5. 在本地终端窗口中运行以下命令,以便运行模拟设备应用程序:In your local terminal window, run the following command to run the simulated device application:


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


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

在本部分,我们将结合 IoT 扩展使用 Azure CLI 来监视模拟设备发送的设备消息。In this section, you will use the Azure CLI with the IoT extension to monitor the device messages that are sent by the simulated device.

  1. 使用 Azure CLI 运行以下命令,以建立连接并读取 IoT 中心发送的消息:Using the Azure CLI, run the following command to connect and read messages from your IoT hub:

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

    az iot hub monitor-events --hub-name YourIoTHubName --output table

    使用 Azure CLI 读取设备消息

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


删除资源组的操作不可逆。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 select 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, select ... then Delete resource group.


  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 select Delete. 片刻之后,将会删除该资源组及其包含的所有资源。After a few moments, the resource group and all of its contained resources are deleted.

后续步骤Next steps

在本快速入门中,我们设置了 IoT 中心、注册了设备、使用 C 应用程序将模拟遥测数据发送到了中心,然后使用 Azure CLI 读取了中心发送的遥测数据。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 the Azure CLI.

若要详细了解如何使用 Azure IoT 中心 C SDK 进行开发,请继续学习以下操作方法指南:To learn more about developing with the Azure IoT Hub C SDK, continue to the following How-to guide: