将 Raspberry Pi 联机模拟器连接到 Azure IoT 中心 (Node.js)Connect Raspberry Pi online simulator to Azure IoT Hub (Node.js)

本教程会先介绍使用 Raspberry Pi 联机模拟器的基础知识。In this tutorial, you begin by learning the basics of working with Raspberry Pi online simulator. 然后介绍如何使用 Azure IoT 中心将 Pi 模拟器无缝连接到云。You then learn how to seamlessly connect the Pi simulator to the cloud by using Azure IoT Hub.

如果有物理设备,请访问将 Raspberry Pi 连接到 Azure IoT 中心以开始进行操作。If you have physical devices, visit Connect Raspberry Pi to Azure IoT Hub to get started.

准备工作What you do

  • 了解 Raspberry Pi 联机模拟器的基础知识。Learn the basics of Raspberry Pi online simulator.
  • 创建 IoT 中心。Create an IoT hub.
  • 在 IoT 中心内为 Pi 注册设备。Register a device for Pi in your IoT hub.
  • 在 Pi 上运行示例应用程序,将模拟传感器数据发送到 IoT 中心。Run a sample application on Pi to send simulated sensor data to your IoT hub.

将模拟 Raspberry Pi 连接到所创建的 IoT 中心。Connect simulated Raspberry Pi to an IoT hub that you create. 然后使用模拟器运行示例应用程序,生成传感器数据。Then you run a sample application with the simulator to generate sensor data. 最后,将传感器数据发送到 IoT 中心。Finally, you send the sensor data to your IoT hub.

学习内容What you learn

Raspberry Pi Web 模拟器概述Overview of Raspberry Pi web simulator

单击用于启动 Raspberry Pi 联机模拟器的按钮。Click the button to launch Raspberry Pi online simulator.

Web 模拟器中有三个区域。There are three areas in the web simulator.

  1. 程序集区域 - 默认电路是 Pi 连接到 BME280 传感器和 LED。Assembly area - The default circuit is that a Pi connects with a BME280 sensor and an LED. 该区域在预览版本中为锁定状态,因此目前无法执行自定义操作。The area is locked in preview version so currently you cannot do customization.
  2. 编码区域 - 一个联机代码编辑器,可在其中使用 Raspberry Pi 进行编码。Coding area - An online code editor for you to code with Raspberry Pi. 默认示例应用程序可帮助从 BME280 传感器收集传感器数据,并将其发送到 Azure IoT 中心。The default sample application helps to collect sensor data from BME280 sensor and sends to your Azure IoT Hub. 该应用程序与真实 Pi 设备完全兼容。The application is fully compatible with real Pi devices.
  3. 集成控制台窗口 - 显示代码输出。Integrated console window - It shows the output of your code. 在此窗口的顶部,有三个按钮。At the top of this window, there are three buttons.
    • 运行 - 在编码区域运行应用程序 。Run - Run the application in the coding area.
    • 重置 - 将编码区域重置为默认示例应用程序 。Reset - Reset the coding area to the default sample application.
    • 折叠/展开 - 可使用右侧的按钮折叠/展开控制台窗口 。Fold/Expand - On the right side there is a button for you to fold/expand the console window.

备注

目前 Raspberry Pi Web 模拟器在预览版中可用。The Raspberry Pi web simulator is now available in preview version. 请通过 Gitter Chatroom 向我们提供意见和建议。We'd like to hear your voice in the Gitter Chatroom. 源代码在 Github 上是公开的。The source code is public on Github.

Pi 联机模拟器概述

创建 IoT 中心Create an IoT hub

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

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

  2. 从 Azure 主页中选择“+ 创建资源”按钮,然后在“搜索市场”字段中输入“IoT 中心”。 From the Azure homepage, select the + Create a resource button, and then enter IoT Hub in the Search the Marketplace field.

  3. 在搜索结果中选择“IoT 中心”,然后选择“创建” 。Select IoT Hub from the search results, and then select Create.

  4. 在“基本信息”选项卡上,按如下所示填写字段: On the Basics tab, complete the fields as follows:

    • 订阅:选择要用于中心的订阅。Subscription: Select the subscription to use for your hub.

    • 资源组:选择一个资源组或新建一个资源组。Resource Group: Select a resource group or create a new one. 若要新建资源组,请选择“新建”并填写要使用的名称。 To create a new one, select Create new and fill in the name you want to use. 若要使用现有的资源组,请选择它。To use an existing resource group, select that resource group. 有关详细信息,请参阅管理 Azure 资源管理器资源组For more information, see Manage Azure Resource Manager resource groups.

    • 区域:选择中心所在的区域。Region: Select the region in which you want your hub to be located. 选择最靠近你的位置。Select the location closest to you.

    • IoT 中心名称:输入中心的名称。IoT Hub Name: Enter a name for your hub. 此名称必须全局唯一。This name must be globally unique. 如果输入的名称可用,会显示一个绿色复选标记。If the name you enter is available, a green check mark appears.

    重要

    IoT 中心将公开为 DNS 终结点,因此,命名时请务必避免包含任何敏感信息。The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming it.

    在 Azure 门户中创建中心

  5. 在完成时选择“下一步: 大小和规模”,以继续创建中心。Select Next: Size and scale to continue creating your hub.

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

    在此屏幕中可以设置以下值:This screen allows you to set the following values:

    • 定价和缩放层:选择的层。Pricing and scale tier: Your selected tier. 可以根据你需要的功能数以及每天通过解决方案发送的消息数从多个层级中进行选择。You can choose from several tiers, depending on how many features you want and how many messages you send through your solution per day. 免费层适用于测试和评估。The free tier is intended for testing and evaluation. 允许 500 台设备连接到中心,每天最多可传输 8,000 条消息。It allows 500 devices to be connected to the hub and up to 8,000 messages per day. 每个 Azure 订阅可以在免费层中创建一个 IoT 中心。Each Azure subscription can create one IoT Hub in the free tier.

    • IoT 中心单元:每个单位每日允许的消息数取决于中心的定价层。IoT Hub units: The number of messages allowed per unit per day depends on your hub's pricing tier. 例如,如果希望中心支持 700,000 条消息引入,请选择两个 S1 层单位。For example, if you want the hub to support ingress of 700,000 messages, you choose two S1 tier units. 有关其他层选项的详细信息,请参阅选择合适的 IoT 中心层For details about the other tier options, see Choosing the right IoT Hub tier.

    • 高级设置 > 设备到云的分区:此属性将设备到云消息与这些消息的同步读取器数目相关联。Advanced Settings > Device-to-cloud partitions: This property relates the device-to-cloud messages to the number of simultaneous readers of the messages. 大多数中心只需要 4 个分区。Most hubs need only four partitions.

  6. 对于本文,请接受默认选项,然后选择“查看 + 创建”以查看所做的选择。 For this article, accept the default choices, and then select Review + create to review your choices. 会显示类似于以下的屏幕。You see something similar to this screen.

    查看用于创建新中心的信息

  7. 选择“创建”以创建新的中心 。Select Create to create your new hub. 创建中心需要几分钟时间。Creating the hub takes a few minutes.

在 IoT 中心内注册新设备Register a new device in the IoT hub

本部分在 IoT 中心的标识注册表中创建设备标识。In this section, you create a device identity in the identity registry in your IoT hub. 除非设备在标识注册表中具有条目,否则设备无法连接到中心。A device cannot connect to a hub unless it has an entry in the identity registry. 有关详细信息,请参阅 IoT 中心开发人员指南For more information, see the IoT Hub developer guide.

  1. 在 IoT 中心导航菜单中,打开“IoT 设备” ,然后选择“新建” 以在 IoT 中心中添加设备。In your IoT hub navigation menu, open IoT Devices, then select New to add a device in your IoT hub.

    在门户中创建设备标识

  2. 在“创建设备” 中,为新设备提供名称(例如 myDeviceId),然后选择“保存” 。In Create a device, provide a name for your new device, such as myDeviceId, and select Save. 此操作会为 IoT 中心创建设备标识。This action creates a device identity for your IoT hub.

    添加新设备

    重要

    收集的日志中可能会显示设备 ID 用于客户支持和故障排除,因此,在为日志命名时,请务必避免包含任何敏感信息。The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid any sensitive information while naming it.

  3. 创建设备后,在“IoT 设备”窗格的列表中打开该设备 。After the device is created, open the device from the list in the IoT devices pane. 复制主连接字符串以便稍后使用。Copy the Primary Connection String to use later.

    设备连接字符串

备注

IoT 中心标识注册表仅存储用于实现 IoT 中心安全访问的设备标识。The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. 它存储设备 ID 和密钥作为安全凭据,以及启用/禁用标志让你禁用对单个设备的访问。It stores device IDs and keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. 如果应用程序需要存储其他特定于设备的元数据,则应使用特定于应用程序的存储。If your application needs to store other device-specific metadata, it should use an application-specific store. 有关详细信息,请参阅 IoT 中心开发人员指南For more information, see IoT Hub developer guide.

在 Pi Web 模拟器上运行示例应用程序Run a sample application on Pi web simulator

  1. 在编码区域,请确保使用的是默认示例应用程序。In coding area, make sure you are working on the default sample application. 将第 15 行的占位符替换为 Azure IoT 中心设备连接字符串。Replace the placeholder in Line 15 with the Azure IoT hub device connection string.

    替换设备连接字符串

  2. 选择“运行”,或键入 ,即可运行应用程序npm startSelect Run or type npm start to run the application.

应看到以下输出,该输出显示传感器数据和发送到 IoT 中心的消息输出 - 从 Raspberry Pi 发送到 IoT 中心的传感器数据You should see the following output that shows the sensor data and the messages that are sent to your IoT hub Output - sensor data sent from Raspberry Pi to your IoT hub

读取 IoT 中心收到的消息Read the messages received by your hub

若要监视 IoT 中心从模拟设备收到的消息,一种方法是使用适用于 Visual Studio Code 的 Azure IoT Tools。One way to monitor messages received by your IoT hub from the simulated device is to use the Azure IoT Tools for Visual Studio Code. 若要了解详细信息,请参阅使用适用于 Visual Studio Code 的 Azure IoT Tools 在设备和 IoT 中心之间发送和接收消息To learn more, see Use Azure IoT Tools for Visual Studio Code to send and receive messages between your device and IoT Hub.

若要了解如何通过更多方式来处理设备发送的数据,请转到下一部分。For more ways to process data sent by your device, continue on to the next section.

后续步骤Next steps

此时已运行示例应用程序,收集传感器数据并将其发送到 IoT 中心。You’ve run a sample application to collect sensor data and send it to your IoT hub.

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