快速入门:将遥测数据从设备发送到 IoT 中心并使用 Azure CLI 监视该数据Quickstart: Send telemetry from a device to an IoT hub and monitor it with the Azure CLI

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. 在本快速入门中,你将使用 Azure CLI 创建 IoT 中心和模拟设备、将设备遥测数据发送到中心,以及发送云到设备的消息。In this quickstart, you use the Azure CLI to create an IoT Hub and a simulated device, send device telemetry to the hub, and send a cloud-to-device message. 还将使用 Azure 门户可视化设备指标。You also use the Azure portal to visualize device metrics. 这是一个基本工作流,适用于使用 CLI 与 IoT 中心应用程序交互的开发人员。This is a basic workflow for developers who use the CLI to interact with an IoT Hub application.

先决条件Prerequisites

  • 如果没有 Azure 订阅,可以在开始前创建一个试用帐户If you don't have an Azure subscription, create one for trial before you begin.
  • Azure CLI。Azure CLI. 可以使用在浏览器中运行的交互式 CLI shell 运行本快速入门中的所有命令。You can run all commands in this quickstart using an interactive CLI shell that runs in your browser. 本快速入门需要 Azure CLI 2.0.76 或更高版本。This quickstart requires Azure CLI version 2.0.76 or later. 若要查找版本,请运行 az --version。Run az --version to find the version. 若要安装或升级,请参阅安装 Azure CLITo install or upgrade, see Install Azure CLI.

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

通过 https://portal.azure.cn 登录到 Azure 门户。Sign in to the Azure portal at https://portal.azure.cn.

准备两个 CLI 会话Prepare two CLI sessions

在本部分中,准备两个 Azure CLI 会话。In this section, you prepare two Azure CLI sessions. 如果使用本地 CLI 客户端,将运行两个单独的 CLI 实例。If using a local CLI client, you will run two separate CLI instances. 使用第一个会话作为模拟设备,并使用第二个会话来监视和发送消息。You'll use the first session as a simulated device, and the second session to monitor and send messages. 若要运行命令,请选择“复制”以复制本快速入门中的代码块,将其粘贴到 shell 会话中,然后运行它 。To run a command, select Copy to copy a block of code in this quickstart, paste it into your shell session, and run it.

Azure CLI 要求你登录到 Azure 帐户。Azure CLI requires you to be logged into your Azure account. Azure CLI Shell 会话与 IoT 中心之间的所有通信都经过身份验证和加密。All communication between your Azure CLI shell session and your IoT hub is authenticated and encrypted. 因此,本快速入门不需要将用于实际设备的额外身份验证(如连接字符串)。As a result, this quickstart does not need additional authentication that you'd use with a real device, such as a connection string.

  1. 运行 az extension add 命令将用于 Azure CLI 的 Microsoft Azure IoT 扩展添加。Run the az extension add command to add the Microsoft Azure IoT Extension for Azure CLI to your CLI shell. 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-iot
    

    安装 Azure IOT 扩展后,无需在任何会话中再次安装。After you install the Azure IOT extension, you don't need to install it again in any session.

    备注

    本文使用最新版本的 Azure IoT 扩展(称为 azure-iot)。This article uses the newest version of the Azure IoT extension, called azure-iot. 旧版本称为 azure-iot-cli-ext。只应同时安装有一个版本。The legacy version is called azure-iot-cli-ext.You should only have one version installed at a time. 可以使用命令 az extension list 来验证当前安装的扩展。You can use the command az extension list to validate the currently installed extensions.

    使用 az extension remove --name azure-cli-iot-ext 可删除扩展的旧版本。Use az extension remove --name azure-cli-iot-ext to remove the legacy version of the extension.

    使用 az extension add --name azure-iot 可添加扩展的新版本。Use az extension add --name azure-iot to add the new version of the extension.

    若要查看已安装了哪些扩展,请使用 az extension listTo see what extensions you have installed, use az extension list.

  2. 打开第二个 CLI 会话。Open a second CLI session. 如果在本地使用 CLI,请打开第二个实例。If you're using the CLI locally, open a second instance.

创建 IoT 中心Create an IoT Hub

在本部分中,使用 Azure CLI 创建资源组和 IoT 中心。In this section, you use the Azure CLI to create a resource group and an IoT Hub. Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed. IoT 中心充当中央消息中心,用于 IoT 应用程序与设备之间的双向通信。An IoT Hub acts as a central message hub for bi-directional communication between your IoT application and the devices.

提示

(可选)可以通过使用 Azure 门户Visual Studio Code 或其他编程方法来创建 Azure 资源组、IoT 中心和其他资源。Optionally, you can create an Azure resource group, an IoT Hub, and other resources by using the Azure portal, Visual Studio Code, or other programmatic methods.

  1. 运行 az group create 命令创建资源组。Run the az group create command to create a resource group. 以下命令在“chinaeast” 位置创建名为“MyResourceGroup” 的资源组。The following command creates a resource group named MyResourceGroup in the chinaeast location.

    az group create --name MyResourceGroup --location chinaeast
    
  2. 运行 az iot hub create 命令创建 IoT 中心。Run the az iot hub create command to create an IoT hub. 创建 IoT 中心可能需要数分钟的时间。It might take a few minutes to create an IoT hub.

    YourIotHubNameYourIotHubName. 将下面的占位符替换为你为 IoT 中心选择的名称。Replace this placeholder below with the name you chose for your IoT hub. IoT 中心名称必须在 Azure 中全局唯一。An IoT hub name must be globally unique in Azure. 此占位符在本快速入门的其余部分中用于表示 IoT 中心名称。This placeholder is used in the rest of this quickstart to represent your IoT hub name.

    az iot hub create --resource-group MyResourceGroup --name {YourIoTHubName}
    

创建和监视设备Create and monitor a device

在本部分中,将在第一个 CLI 会话中创建模拟设备。In this section, you create a simulated device in the first CLI session. 模拟设备将设备遥测数据发送到 IoT 中心。The simulated device sends device telemetry to your IoT hub. 在第二个 CLI 会话中,将监视事件和遥测数据,并将云到设备的消息发送到模拟设备。In the second CLI session, you monitor events and telemetry, and send a cloud-to-device message to the simulated device.

若要创建和启动模拟设备,请执行以下操作:To create and start a simulated device:

  1. 在第一个 CLI 会话中运行 az iot hub device-identity create 命令。Run the az iot hub device-identity create command in the first CLI session. 这会创建模拟设备标识。This creates the simulated device identity.

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

    simDevice:simDevice. 在本快速入门的其余部分中,可以直接将此名称用于模拟设备。You can use this name directly for the simulated device in the rest of this quickstart. 或者,也可使用其他名称。Optionally, use a different name.

    az iot hub device-identity create --device-id simDevice --hub-name {YourIoTHubName} 
    
  2. 在第一个 CLI 会话中运行 az iot device simulate 命令。Run the az iot device simulate command in the first CLI session. 这会启动模拟设备。This starts the simulated device. 设备将遥测数据发送到 IoT 中心,并从其中接收消息。The device sends telemetry to your IoT hub and receives messages from it.

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

    az iot device simulate -d simDevice -n {YourIoTHubName}
    

若要监视设备,请执行以下操作:To monitor a device:

  1. 在第二个 CLI 会话中,运行 az iot hub monitor-events 命令。In the second CLI session, run the az iot hub monitor-events command. 这会开始监视模拟设备。This starts monitoring the simulated device. 输出显示模拟设备发送到 IoT 中心的遥测数据。The output shows telemetry that the simulated device sends to the IoT hub.

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

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

    Cloud Shell 监视事件

  2. 在第二个 CLI 会话中监视模拟设备后,按 Ctrl+C 以停止监视。After you monitor the simulated device in the second CLI session, press Ctrl+C to stop monitoring.

使用 CLI 发送消息Use the CLI to send a message

在本部分中,将使用第二个 CLI 会话将消息发送到模拟设备。In this section, you use the second CLI session to send a message to the simulated device.

  1. 在第一个 CLI 会话中,确认模拟设备是否正在运行。In the first CLI session, confirm that the simulated device is running. 如果设备已停止,请运行以下命令来启动它:If the device has stopped, run the following command to start it:

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

    az iot device simulate -d simDevice -n {YourIoTHubName}
    
  2. 在第二个 CLI 会话中,运行 az iot device c2d-message send 命令。In the second CLI session, run the az iot device c2d-message send command. 这会将云到设备的消息从 IoT 中心发送到模拟设备。This sends a cloud-to-device message from your IoT hub to the simulated device. 该消息包含一个字符串和两个键值对。The message includes a string and two key-value pairs.

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

    az iot device c2d-message send -d simDevice --data "Hello World" --props "key0=value0;key1=value1" -n {YourIoTHubName}
    

    (可选)可以使用 Azure 门户发送云到设备的消息。Optionally, you can send cloud-to-device messages by using the Azure portal. 为此,请浏览到 IoT 中心的概述页,依次选择“IoT 设备”、模拟设备和“发送到设备的消息” 。To do this, browse to the overview page for your IoT Hub, select IoT Devices, select the simulated device, and select Message to Device.

  3. 在第一个 CLI 会话中,确认模拟设备是否已收到消息。In the first CLI session, confirm that the simulated device received the message.

    Cloud Shell 云到设备的消息

  4. 查看消息后,关闭第二个 CLI 会话。After you view the message, close the second CLI session. 使第一个 CLI 会话保持打开状态。Keep the first CLI session open. 在稍后的步骤中会使用它来清理资源。You use it to clean up resources in a later step.

在门户中查看消息传递指标View messaging metrics in the portal

使用 Azure 门户可以管理 IoT 中心和设备的所有方面。The Azure portal enables you to manage all aspects of your IoT Hub and devices. 在引入来自设备的遥测数据的典型 IoT 中心应用程序中,你可能想要监视设备或查看设备遥测数据中的指标。In a typical IoT Hub application that ingests telemetry from devices, you might want to monitor devices or view metrics on device telemetry.

若要在 Azure 门户中可视化消息传递指标,请执行以下操作:To visualize messaging metrics in the Azure portal:

  1. 在门户的左侧导航菜单中选择“所有资源” 。In the left navigation menu on the portal, select All Resources. 这会列出订阅中的所有资源,包括你创建的 IoT 中心。This lists all resources in your subscription, including the IoT hub you created.

  2. 选择你创建的 IoT 中心上的链接。Select the link on the IoT hub you created. 门户将显示中心的概述页。The portal displays the overview page for the hub.

  3. 在 IoT 中心的左窗格中选择“指标” 。Select Metrics in the left pane of your IoT Hub.

    IoT 中心消息传递指标

  4. 在“范围”中输入 IoT 中心名称 。Enter your IoT hub name in Scope.

  5. 在“指标命名空间”中选择“IoT 中心标准指标” 。Select Iot Hub Standard Metrics in Metric Namespace.

  6. 在“指标”中选择“已使用的消息总数”。 Select Total number of messages used in Metric.

  7. 将鼠标指针悬停在设备发送消息的时间线区域上。Hover your mouse pointer over the area of the timeline in which your device sent messages. 某个时间点的消息总数显示在时间线的左下角。The total number of messages at a point in time appears in the lower left corner of the timeline.

    查看 Azure IoT 中心指标

  8. (可选)使用“指标”下拉列表显示模拟设备上的其他指标 。Optionally, use the Metric dropdown to display other metrics on your simulated device. 例如,“C2D 消息传递已完成”或“设备总数(预览)” 。For example, C2d message deliveries completed or Total devices (preview).

清理资源Clean up resources

如果不再需要本快速入门中创建的 Azure 资源,可以使用 Azure CLI 将其删除。If you no longer need the Azure resources created in this quickstart, you can use the Azure CLI to delete them.

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

重要

删除资源组的操作不可逆。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.

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

  1. 运行 az group delete 命令。Run the az group delete command. 这会删除你创建的资源组、IoT 中心和设备注册。This removes the resource group, the IoT Hub, and the device registration you created.

    az group delete --name MyResourceGroup
    
  2. 运行 az group list 命令,确认资源组是否已删除。Run the az group list command to confirm the resource group is deleted.

    az group list
    

后续步骤Next steps

在本快速入门中,你使用 Azure CLI 创建了 IoT 中心和模拟设备、发送了遥测数据、监视了遥测数据、发送了云到设备的消息,并清理了资源。In this quickstart, you used the Azure CLI to create an IoT hub, create a simulated device, send telemetry, monitor telemetry, send a cloud-to-device message, and clean up resources. 使用 Azure 门户可视化了设备上的消息传递指标。You used the Azure portal to visualize messaging metrics on your device.

如果你是设备开发人员,建议执行的下一步骤是查看使用适用于 C 的 Azure IoT 设备 SDK 的遥测快速入门。(可选)使用首选语言或 SDK 查看可用的 Azure IoT 中心遥测快速入门文章之一。If you are a device developer, the suggested next step is to see the telemetry quickstart that uses the Azure IoT Device SDK for C. Optionally, see one of the available Azure IoT Hub telemetry quickstart articles in your preferred language or SDK.