使用适用于 Visual Studio Code 的 Azure IoT 工具在设备和 IoT 中心之间发送和接收消息Use Azure IoT Tools for Visual Studio Code to send and receive messages between your device and IoT Hub

端到端关系图

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. 本文重点介绍了如何使用适用于 Visual Studio Code 的 Azure IoT 工具在设备和 IoT 中心之间发送和接收消息。This article focuses on how to use Azure IoT Tools for Visual Studio Code to send and receive messages between your device and your IoT hub.

备注

本文中提到的某些功能(例如云到设备消息传递、设备孪生、设备管理)仅在 IoT 中心的标准层中提供。Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only available in the standard tier of IoT hub. 有关基本和标准 IoT 中心层的详细信息,请参阅如何选择合适的 IoT 中心层For more information about the basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.

要学习的知识What you will learn

了解如何使用适用于 Visual Studio Code 的 Azure IoT 工具来监视设备到云消息,并发送云到设备消息。You learn how to use Azure IoT Tools for Visual Studio Code to monitor device-to-cloud messages and to send cloud-to-device messages. 设备到云的消息可能是设备收集的,随后要发送到 IoT 中心的传感器数据。Device-to-cloud messages could be sensor data that your device collects and then sends to your IoT hub. 云到设备的消息可能是 IoT 中心发送到设备的,用于闪烁连接到设备的 LED 的命令。Cloud-to-device messages could be commands that your IoT hub sends to your device to blink an LED that is connected to your device.

执行的操作What you will do

  • 使用适用于 Visual Studio Code 的 Azure IoT 工具来监视设备到云消息。Use Azure IoT Tools for Visual Studio Code to monitor device-to-cloud messages.
  • 使用适用于 Visual Studio Code 的 Azure IoT 工具来发送云到设备消息。Use Azure IoT Tools for Visual Studio Code to send cloud-to-device messages.

需要什么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.

监视设备到云的消息Monitor device-to-cloud messages

若要监视设备发送到 IoT 中心的消息,请执行以下步骤:To monitor messages that are sent from your device to your IoT hub, follow these steps:

  1. 右键单击设备,然后选择“开始监视内置事件终结点” 。Right-click your device and select Start Monitoring Built-in Event Endpoint.

  2. 受监视的消息将显示在“输出” > “Azure IoT 中心” 视图中。The monitored messages will be shown in OUTPUT > Azure IoT Hub view.

  3. 若要停止监视,请右键单击“输出” 视图,然后选择“停止监视内置事件终结点” 。To stop monitoring, right-click the OUTPUT view and select Stop Monitoring Built-in Event Endpoint.

发送“云到设备”消息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

现在,已了解如何监视设备到云的消息,以及在 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: