针对 Azure IoT 中心设备管理,使用适用于 Azure CLI 的 IoT 扩展Use the IoT extension for Azure CLI for Azure IoT Hub device management

端到端关系图

Note

开始本教程前,请设置设备Before you start this tutorial, set up your device. 在本文中,将设置 Azure IoT 设备和 IoT 中心,并部署一个在设备上运行的示例应用程序。In the article, you set up your Azure IoT device and IoT hub, and you deploy a sample application to run on your device. 该应用程序会将收集的传感器数据发送到 IoT 中心。The application sends collected sensor data to your IoT hub.

适用于 Azure CLI 的 IoT 扩展是一个开放源代码 IoT 扩展,它增强了 Azure CLI 的功能。The IoT extension for Azure CLI is an open-source IoT extension that adds to the capabilities of the Azure CLI. Azure CLI 包含用于与 Azure 资源管理器和管理终结点进行交互的命令。The Azure CLI includes commands for interacting with Azure resource manager and management endpoints. 例如,可使用 Azure CLI 创建 Azure VM 或 IoT 中心。For example, you can use Azure CLI to create an Azure VM or an IoT hub. CLI 扩展使 Azure 服务能够扩展 Azure CLI,从而可访问其他特定于服务的功能。A CLI extension enables an Azure service to augment the Azure CLI giving you access to additional service-specific capabilities. IoT 扩展为 IoT 开发人员提供了面向所有 IoT 中心、IoT Edge 和 IoT 中心设备预配服务功能的命令行访问。The IoT extension gives IoT developers command line access to all IoT Hub, IoT Edge, and IoT Hub Device Provisioning Service capabilities.

Note

本文使用最新版本的 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.

Note

本文介绍的功能仅在 IoT 中心的标准层中可用。The features described in this article are available only in the standard tier of IoT Hub. 有关基本和标准/免费 IoT 中心层的详细信息,请参阅选择合适的 IoT 中心层For more information about the basic and standard/free IoT Hub tiers, see Choose the right IoT Hub tier.

管理选项Management option 任务Task
直接方法Direct methods 让设备执行操作,如开始或停止发送消息或重新启动设备。Make a device act such as starting or stopping sending messages or rebooting the device.
孪生所需属性Twin desired properties 让设备进入特定状态,例如将 LED 设置为绿色,或将遥测发送间隔设置为 30 分钟。Put a device into certain states, such as setting an LED to green or setting the telemetry send interval to 30 minutes.
孪生报告属性Twin reported properties 获取报告的设备状态。Get the reported state of a device. 例如,设备报告 LED 现在正在闪烁。For example, the device reports the LED is blinking now.
孪生标记Twin tags 将设备特定的元数据存储在云中。Store device-specific metadata in the cloud. 例如,存储在自动售货机的部署位置。For example, the deployment location of a vending machine.
设备孪生查询Device twin queries 查询所有设备孪生以检索符合任意条件的设备孪生,例如确定可供使用的设备。Query all device twins to retrieve those twins with arbitrary conditions, such as identifying the devices that are available for use.

有关这些选项的差异和使用指导的更详细说明,请参阅设备到云通信指南云到设备通信指南For more detailed explanation on the differences and guidance on using these options, see Device-to-cloud communication guidance and Cloud-to-device communication guidance.

设备孪生是存储设备状态信息(元数据、配置和条件)的 JSON 文档。Device twins are JSON documents that store device state information (metadata, configurations, and conditions). IoT 中心为连接到它的每台设备保留一个设备孪生。IoT Hub persists a device twin for each device that connects to it. 有关设备孪生的详细信息,请参阅设备孪生入门For more information about device twins, see Get started with device twins.

学习内容What you learn

了解如何在开发计算机上通过各种管理选项使用适用于 Azure CLI 的 IoT 扩展。You learn to use the IoT extension for Azure CLI with various management options on your development machine.

准备工作What you do

使用各种管理选项运行 Azure CLI 和适用于 Azure CLI 的 IoT 扩展。Run Azure CLI and the IoT extension for Azure CLI with various management options.

所需条件What you need

  • 完成 Raspberry Pi 联机模拟器教程或其中一个设备教程;例如将 Raspberry Pi 与 Node.js 配合使用Complete the Raspberry Pi online simulator tutorial or one of the device tutorials; for example, Raspberry Pi with node.js. 这些项包括以下要求:These items cover the following requirements:

    • 一个有效的 Azure 订阅。An active Azure subscription.
    • 已在订阅中创建一个 Azure IoT 中心。An Azure IoT hub under your subscription.
    • 一个可向 Azure IoT 中心发送消息的客户端应用程序。A client application that sends messages to your Azure IoT hub.
  • 在学习本教程期间,确保设备与客户端应用程序均处于运行状态。Make sure your device is running with the client application during this tutorial.

  • Python 2.7x 或 Python 3.xPython 2.7x or Python 3.x

  • Azure CLI。The Azure CLI. 如需进行安装,请参阅安装 Azure CLIIf you need to install it, see Install the Azure CLI. Azure CLI 版本必须至少是 2.0.24 或更高版本。At a minimum, your Azure CLI version must be 2.0.24 or above. 请使用 az –version 验证版本。Use az –version to validate.

Note

本文使用最新版本的 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.

  • 安装 IoT 扩展。Install the IoT extension. 最简单的方法是运行 az extension add --name azure-iotThe simplest way is to run az extension add --name azure-iot. IoT 扩展自述文件介绍了该扩展的多种安装方法。The IoT extension readme describes several ways to install the extension.

登录到 Azure 帐户Sign in to your Azure account

通过运行以下命令登录到 Azure 帐户:Sign in to your Azure account by running the following command:

az login

直接方法Direct methods

az iot hub invoke-device-method --device-id <your device id> --hub-name <your hub name> --method-name <the method name> --method-payload <the method payload>

设备孪生所需属性Device twin desired properties

通过运行以下命令将所需属性间隔设置为 3000:Set a desired property interval = 3000 by running the following command:

az iot hub device-twin update -n <your hub name> -d <your device id> --set properties.desired.interval = 3000

可从设备读取此属性。This property can be read from your device.

设备孪生报告属性Device twin reported properties

通过运行以下命令获取报告的设备属性:Get the reported properties of the device by running the following command:

az iot hub device-twin show -n <your hub name> -d <your device id>

其中一个孪生报告属性为 $metadata.$lastUpdated,它显示设备应用上次更新其报告属性集的时间。One of the twin reported properties is $metadata.$lastUpdated which shows the last time the device app updated its reported property set.

设备孪生标记Device twin tags

通过运行以下命令显示设备的标记和属性:Display the tags and properties of the device by running the following command:

az iot hub device-twin show --hub-name <your hub name> --device-id <your device id>

通过运行以下命令向设备添加字段角色 = 温度和湿度:Add a field role = temperature&humidity to the device by running the following command:

az iot hub device-twin update --hub-name <your hub name> --device-id <your device id> --set tags = '{"role":"temperature&humidity"}}'

设备孪生查询Device twin queries

通过运行以下命令查询角色标记 =“温度和湿度”的设备:Query devices with a tag of role = 'temperature&humidity' by running the following command:

az iot hub query --hub-name <your hub name> --query-command "SELECT * FROM devices WHERE tags.role = 'temperature&humidity'"

通过运行以下命令查询除角色标记 =“温度和湿度”的设备以外的所有设备:Query all devices except those with a tag of role = 'temperature&humidity' by running the following command:

az iot hub query --hub-name <your hub name> --query-command "SELECT * FROM devices WHERE tags.role != 'temperature&humidity'"

后续步骤Next steps

现在,已了解如何监视设备到云的消息,以及在 IoT 设备与 Azure IoT 中心之间发送云到设备的消息。You’ve learned how to monitor device-to-cloud messages and send cloud-to-device messages between your IoT device and Azure IoT Hub.

若要继续了解 Azure IoT 中心入门知识并浏览所有扩展的 IoT 方案,请参阅:To continue to get started with Azure IoT Hub and to explore all extended IoT scenarios, see the following: