使用适用于 Visual Studio Code 的 Azure IoT 工具进行 Azure IoT 中心设备管理Use Azure IoT Tools for Visual Studio Code for Azure IoT Hub device management

端到端关系图

Azure IoT 工具是一个有用的 Visual Studio Code 扩展,可以使 IoT 中心管理和 IoT 应用程序开发更加容易。Azure IoT Tools is a useful Visual Studio Code extension that makes IoT Hub management and IoT application development easier. 它附带了可用于执行各种任务的管理选项。It comes with management options that you can use to perform various tasks.

备注

本文介绍的功能仅在 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.
读取设备孪生Read device twin 获取报告的设备状态。Get the reported state of a device. 例如,设备报告 LED 现在正在闪烁。For example, the device reports the LED is blinking now.
更新设备孪生Update device twin 让设备进入特定状态,例如将 LED 设置为绿色,或将遥测发送间隔设置为 30 分钟。Put a device into certain states, such as setting an LED to green or setting the telemetry send interval to 30 minutes.
云到设备的消息Cloud-to-device messages 向设备发送通知。Send notifications to a device. 例如,“今天很可能会下雨。For example, "It is very likely to rain today. 不要忘记带雨伞。”Don't forget to bring an umbrella."

有关这些选项的差异和使用指导的更详细说明,请参阅设备到云通信指南云到设备通信指南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.

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

学习内容What you learn

了解在开发计算机上通过各种管理选项使用适用于 Visual Studio Code 的 Azure IoT 工具。You learn using Azure IoT Tools for Visual Studio Code with various management options on your development machine.

准备工作What you do

使用各种管理选项运行适用于 Visual Studio Code 的 Azure IoT 工具。Run Azure IoT Tools for Visual Studio Code with various management options.

需要什么What you need

登录以访问 IoT 中心Sign in to access your IoT hub

  1. 在 VS Code 的“资源管理器” 视图中,展开左下角的“Azure IoT 中心设备” 部分。In Explorer view of VS Code, expand Azure IoT Hub Devices section in the bottom left corner.

  2. 在上下文菜单中单击“选择 IoT 中心” 。Click Select IoT Hub in context menu.

  3. 弹出窗口将显示在右下角,以便让你首次登录 Azure。A pop-up will show in the bottom right corner to let you sign in to Azure for the first time.

  4. 登录后,将显示 Azure 订阅列表,然后选择“Azure 订阅和 IoT 中心”。After you sign in, your Azure Subscription list will be shown, then select Azure Subscription and IoT Hub.

  5. 几秒钟后,设备列表将显示在“Azure IoT 中心设备” 选项卡中。The device list will be shown in Azure IoT Hub Devices tab in a few seconds.

    备注

    也可以选择“设置 IoT 中心连接字符串”来完成设置。 You can also complete the set up by choosing Set IoT Hub Connection String. 在弹出窗口中输入 IoT 设备连接到的 IoT 中心的 iothubowner 策略连接字符串。Enter the iothubowner policy connection string for the IoT hub that your IoT device connects to in the pop-up window.

直接方法Direct methods

  1. 右键单击设备并选择“调用直接方法” 。Right-click your device and select Invoke Direct Method.
  2. 在输入框中输入方法名称和有效负载。Enter the method name and payload in input box.
  3. 结果将显示在“输出” > “Azure IoT 中心” 视图中。Results will be shown in OUTPUT > Azure IoT Hub view.

读取设备孪生Read device twin

  1. 右键单击设备并选择“编辑设备孪生” 。Right-click your device and select Edit Device Twin.
  2. 将打开一个 azure-iot-device-twin.json 文件,其中包含设备孪生的内容 。An azure-iot-device-twin.json file will be opened with the content of device twin.

更新设备孪生Update device twin

  1. 编辑“标记”或 properties.desired 字段 。Make some edits of tags or properties.desired field.
  2. 右键单击 azure-iot-device-twin.json 文件 。Right-click on the azure-iot-device-twin.json file.
  3. 选择“更新设备孪生”以更新设备孪生 。Select Update Device Twin to update the device twin.

发送“云到设备”消息Send cloud-to-device messages

要将消息从 IoT 中心发送到设备,请执行以下步骤:To send a message from your IoT hub to your device, follow these steps:

  1. 右键单击设备,然后选择“将 C2D 消息发送到设备” 。Right-click your device and select Send C2D Message to Device.
  2. 在输入框中输入消息。Enter the message in input box.
  3. 结果将显示在“输出” > “Azure IoT 中心” 视图中。Results will be shown in OUTPUT > Azure IoT Hub view.

后续步骤Next steps

你已学习了如何通过各种管理选项使用适用于 Visual Studio Code 的 Azure IoT 工具扩展。You've learned how to use Azure IoT Tools extension for Visual Studio Code with various management options.

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