您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:通过 IoT 中心设备流在 C 中与设备应用程序通信(预览)Quickstart: Communicate to a device application in C via IoT Hub device streams (preview)

Azure IoT 中心目前支持设备流作为预览版功能Azure IoT Hub currently supports device streams as a preview feature.

服务和设备应用程序可以使用 IoT 中心设备流以安全且防火墙友好的方式进行通信。IoT Hub device streams allow service and device applications to communicate in a secure and firewall-friendly manner. 在公共预览期,C SDK 仅支持设备端的设备流。During public preview, the C SDK supports device streams on the device side only. 因此,本快速入门只介绍如何运行设备端应用程序。As a result, this quickstart covers instructions to run only the device-side application. 若要运行随附的服务端应用程序,请参阅:To run an accompanying service-side application, see:

本快速入门中的设备端 C 应用程序具有以下功能:The device-side C application in this quickstart has the following functionality:

  • 建立发往 IoT 设备的设备流。Establish a device stream to an IoT device.
  • 接收从服务端应用程序发送的数据并将其回显。Receive data that's sent from the service-side application and echo it back.

代码将演示设备流的发起过程,以及如何用其来发送和接收数据。The code demonstrates the initiation process of a device stream, as well as how to use it to send and receive data.

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. 通过 Cloud Shell 可以将 bashPowerShell 与 Azure 服务配合使用。Cloud Shell lets you use either bash or PowerShell to work with Azure services. 可以使用 Azure Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.

若要启动 Azure Cloud Shell,请执行以下操作:To launch Azure Cloud Shell:

选项Option 示例/链接Example/Link
选择代码块右上角的“试用”。 Select Try It in the upper-right corner of a code block. 选择“试用” 不会自动将代码复制到 Cloud Shell。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的“试用”示例
转到 https://shell.azure.com 或选择“启动 Cloud Shell” 按钮可在浏览器中打开 Cloud Shell。Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新窗口中启动 Cloud ShellLaunch Cloud Shell in a new window
选择 Azure 门户右上方菜单栏中的“Cloud Shell” 按钮。Select the Cloud Shell button on the top-right menu bar in the Azure portal. Azure 门户中的“Cloud Shell”按钮

若要在 Azure Cloud Shell 中运行本文中的代码,请执行以下操作:To run the code in this article in Azure Cloud Shell:

  1. 打开 Cloud Shell。Open Cloud Shell.
  2. 选择代码块上的“复制”按钮 以复制代码。Select the Copy button on a code block to copy the code.
  3. 在 Windows 和 Linux 上使用 Ctrl+Shift+V 将代码粘贴到 Cloud Shell 会话中,或在 macOS 上使用 Cmd+Shift+V 将代码粘贴到 Cloud Shell 会话中。Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.
  4. Enter 运行此代码。Press Enter to run the code.

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don’t have an Azure subscription, create a free account before you begin.


  • 目前仅以下区域中创建的 IoT 中心支持设备流预览:The preview of device streams is currently supported only for IoT hubs that are created in the following regions:

    • 美国中部Central US
    • 美国中部 EUAPCentral US EUAP
  • 安装 Visual Studio 2017 并启用使用 C++ 的桌面开发工作负荷。Install Visual Studio 2017 with the Desktop development with C++ workload enabled.

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

  • 运行以下命令将用于 Azure CLI 的 Azure IoT 扩展添加到 Cloud Shell 实例。Run the following command to add the Azure IoT Extension for Azure CLI to your Cloud Shell instance. 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 the Azure CLI.

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

准备开发环境Prepare the development environment

在本快速入门中,我们将使用适用于 C 的 Azure IoT 设备 SDK。准备一个用于从 GitHub 克隆和生成 Azure IoT C SDK 的开发环境。For this quickstart, you use the Azure IoT device SDK for C. You 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 that's used in this quickstart.

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

    在开始安装 CMake 之前,必须在计算机上安装 Visual Studio 必备组件(Visual Studio 和“使用 C++ 的桌面开发”工作负荷)。 Before you start the CMake installation, it's important that the Visual Studio prerequisites (Visual Studio and the Desktop development with C++ workload) are installed on your machine. 安装必备组件并验证下载内容后,可以安装 CMake 生成系统。After the prerequisites are in place and you've verified the download, you can install the CMake build system.

  2. 打开命令提示符或 Git Bash shell。Open a command prompt or Git Bash shell. 执行以下命令克隆 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 -b public-preview

    此操作需要花费几分钟时间。This operation should take a few minutes.

  3. 按以下命令中所示在 Git 存储库的根目录中创建 cmake 子目录,并转到该文件夹。Create a cmake subdirectory in the root directory of the Git repository, as shown in the following command, and then go to that folder.

    cd azure-iot-sdk-c
    mkdir cmake
    cd cmake
  4. cmake 目录运行以下命令,以生成特定于开发客户端平台的 SDK 版本。Run the following commands from the cmake directory to build a version of the SDK that's specific to your development client platform.

    • 在 Linux 中:In Linux:

      cmake ..
      make -j
    • 在 Windows 中,请在 Visual Studio 2015 或 2017 的开发人员命令提示下运行以下命令。In Windows, run the following commands in Developer Command Prompt for Visual Studio 2015 or 2017. 将在 cmake 目录中生成模拟设备的 Visual Studio 解决方案。A Visual Studio solution for the simulated device will be generated in the cmake directory.

      rem For VS2015
      cmake .. -G "Visual Studio 14 2015"
      rem Or for VS2017
      cmake .. -G "Visual Studio 15 2017"
      rem Then build the project
      cmake --build . -- /m /p:Configuration=Release

创建 IoT 中心Create an IoT hub

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

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

  2. 选择“创建资源”,然后选择“物联网”。 Select Create a resource, and then select Internet of Things.

  3. 在右侧的列表中选择“IoT 中心”。 In the list at the right, select Iot Hub. 此时会打开用于创建 IoT 中心的第一个页面。The first page for creating an IoT hub opens.

    在 Azure 门户中创建 IoT 中心

    填写字段:Fill in the fields:

    a.a. 在“订阅”下拉列表中,选择要用于 IoT 中心的订阅。 In the Subscription drop-down list, select the subscription to use for your IoT hub.

    b.b. 对于“资源组”,请执行以下操作之一: For Resource Group, do either of the following:

    • 若要创建新的资源组,请选择“新建”,然后输入所需的名称 。To create a new resource group, select Create new and enter the name you want to use.

    • 若要使用现有资源组,请选择“使用现有项”,然后在下拉列表中选择该资源组。 To use an existing resource group, select Use existing and then, in the drop-down list, select the resource group.

      有关详细信息,请参阅管理 Azure 资源管理器资源组For more information, see Manage Azure Resource Manager resource groups.

    c.c. 在“区域”下拉列表中,选择要在其中创建中心的区域。 In the Region drop-down list, select the region in which you want your hub to be located. 选择支持 IoT 中心设备流预览版的区域:“美国中部”或“美国中部 EUAP”。 Select a region that supports the IoT Hub device streams preview, either Central US or Central US EUAP.

    d.d. 在“IoT 中心名称”框中,输入 IoT 中心的名称。 In the IoT Hub Name box, enter the name for your IoT hub. 该名称必须全局唯一。The name must be globally unique. 如果输入的名称可用,会显示一个绿色复选标记。If the name you enter is available, a green check mark appears.


    由于 IoT 中心将作为 DNS 终结点公开可发现,因此请务必避免在命名它时输入任何敏感信息或个人身份信息。Because the IoT hub will be publicly discoverable as a DNS endpoint, be sure to avoid entering any sensitive or personally identifiable information when you name it.

  4. 若要继续创建 IoT 中心,请选择“下一步: 大小和规模”。To continue creating your IoT hub, select Next: Size and scale.

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

    在此窗格中,可以接受默认设置并选择底部的“查看 + 创建”。 In this pane, you can accept the default settings and select Review + create at the bottom. 请考虑以下选项:Consider the following options:

    • 在“定价和规模层”下拉列表中,选择一个标准层(“S1”、“S2”或“S3”),或选择“F1: 免费层”。In the Pricing and scale tier drop-down list, select one of the standard tiers (S1, S2, or S3) or F1: Free tier. 也可根据队列大小以及预期在中心会出现的非流式处理工作负荷(例如遥测消息)完成该选择。This choice can also be guided by the size of your fleet and the non-streaming workloads that you expect in your hub (for example, telemetry messages). 例如,免费层适用于测试和评估。For example, 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 中心单元数”: 此项选择取决于中心内预期会出现的非流式处理工作负荷。For Number of IoT Hub units: This choice depends on non-streaming workload you expect in your hub. 暂时可以选择“1”。You can select 1 for now.

    有关层选项的详细信息,请参阅选择适当的 IoT 中心层For more information about tier options, see Choose the right IoT hub tier.

  5. 若要检查所做的选择,请选择“查看+创建”选项卡。 此时会打开如下所示的窗格:To review your choices, select the Review + create tab. The pane that opens is similar to the following:

    用于创建新 IoT 中心的信息

  6. 若要创建新的 IoT 中心,请选择“创建” 。To create your new IoT hub, select Create. 该过程需要花费几分钟时间。The process takes a few minutes.

注册设备Register a device

必须先将设备注册到 IoT 中心,然后该设备才能进行连接。You must register a device with your IoT hub before it can connect. 在本部分中,我们将使用具有 IoT 扩展的 Azure Cloud Shell 注册模拟设备。In this section, you use Azure Cloud Shell with the IoT Extension to register a simulated device.

  1. 若要创建设备标识,请在 Cloud Shell 中运行以下命令:To create the device identity, run the following command in Cloud Shell:


    • 请将 YourIoTHubName 占位符替换为你为 IoT 中心选择的名称。Replace the YourIoTHubName placeholder with the name you choose for your IoT hub.
    • 如示例中所示使用 MyDeviceUse MyDevice, as shown. 它是为注册的设备提供的名称。It's the name given for the registered device. 如果为设备选择其他名称,请在本文中从头至尾使用该名称,并在运行示例应用程序之前在其中更新设备名称。If you choose a different name for your device, 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 MyDevice
  2. 若要获取刚刚注册的设备的设备连接字符串,请在 Cloud Shell 中运行以下命令:To get the device connection string for the device that you just registered, run the following commands in Cloud Shell:


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

    az iot hub device-identity show-connection-string --hub-name YourIoTHubName --device-id MyDevice --output table

    请记下设备连接字符串,稍后需要在本快速入门中用到它。Note the device connection string for later use in this quickstart. 如以下示例所示:It looks like the following example:


通过设备流在设备与服务之间通信Communicate between the device and the service via device streams

在本部分中,你将运行设备端应用程序和服务器端应用程序并在两者之间进行通信。In this section, you run both the device-side application and the service-side application and communicate between the two.

运行设备端应用程序Run the device-side application

若要运行设备端应用程序,请执行以下操作:To run the device-side application, do the following:

  1. 编辑 iothub_client/samples/iothub_client_c2d_streaming_sample 文件夹中的 iothub_client_c2d_streaming_sample.c 源文件并提供设备连接字符串,以提供设备凭据。Provide your device credentials by editing the iothub_client_c2d_streaming_sample.c source file in the iothub_client/samples/iothub_client_c2d_streaming_sample folder and then providing your device connection string.

    /* Paste in your iothub connection string  */
    static const char* connectionString = "[device connection string]";
  2. 按如下所示编译代码:Compile the code as follows:

    # In Linux
    # Go to the sample's folder cmake/iothub_client/samples/iothub_client_c2d_streaming_sample
    make -j
    rem In Windows
    rem Go to the cmake folder at the root of repo
    cmake --build . -- /m /p:Configuration=Release
  3. 运行编译的程序:Run the compiled program:

    # In Linux
    # Go to the sample's folder cmake/iothub_client/samples/iothub_client_c2d_streaming_sample
    rem In Windows
    rem Go to the sample's release folder cmake\iothub_client\samples\iothub_client_c2d_streaming_sample\Release

运行服务端应用程序Run the service-side application

如前所述,IoT 中心 C SDK 仅支持设备端的设备流。As mentioned previously, the IoT Hub C SDK supports device streams on the device side only. 若要生成并运行服务端应用程序,请遵照下列其中一篇快速入门中的说明:To build and run the service-side application, follow the instructions in one of the following quickstarts:

清理资源Clean up resources

如果你打算继续学习下一篇建议的文章,可以保留并重复使用已创建的资源。If you plan to continue to the next recommended article, you can keep and reuse the resources you've already created.

否则,可以删除本文中创建的 Azure 资源,以避免产生费用。Otherwise, to avoid charges, you can delete the Azure resources that you created in this article.


删除资源组的操作不可逆。Deleting a resource group is irreversible. 资源组以及包含在其中的所有资源将被永久删除。The resource group and all the resources contained in it are permanently deleted. 请确保不要意外删除错误的资源组或资源。Make sure that you don't accidentally delete the wrong resource group or resources. 如果在现有的包含要保留资源的资源组中创建了 IoT 中心,请只删除 IoT 中心资源本身,而不要删除资源组。If you created the IoT hub inside an existing resource group that contains resources that you want to keep, delete only the IoT hub resource itself, not the resource group.

若要按名称删除资源组,请执行以下操作:To delete a resource group by name:

  1. 登录到 Azure 门户,然后选择“资源组”。 Sign in to the Azure portal, and then select Resource groups.

  2. 在“按名称筛选”框中,输入包含你的 IoT 中心的资源组的名称。 In the Filter by name box, enter the name of the resource group that contains your IoT hub.

  3. 在结果列表中你的资源组的右侧,选择 ( ... ),然后选择“删除资源组” 。In the result list, to the right of your resource group, select the ellipsis (...), and then select Delete resource group.


  4. 若要确认删除该资源组,请重新输入资源组名称,然后选择“删除”。 To confirm the deletion of the resource group, reenter the resource group name, and then select Delete. 片刻之后,将会删除该资源组及其包含的所有资源。After a few moments, the resource group and all its contained resources are deleted.

后续步骤Next steps

在本快速入门中,我们设置了 IoT 中心、注册了设备、在设备上的 C# 应用程序和服务端上的其他应用之间建立了设备流,并已使用该流在应用程序之间来回发送数据。In this quickstart, you've set up an IoT hub, registered a device, established a device stream between a C application on the device and another application on the service side, and used the stream to send data back and forth between the applications.

若要详细了解设备流,请参阅:To learn more about device streams, see: