您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:将模拟设备添加到 IoT Central 应用程序Quickstart: Add a simulated device to your IoT Central application

设备模板定义连接到 IoT Central 应用程序的设备的功能。A device template defines the capabilities of a device that connects to your IoT Central application. 功能包括设备发送的遥测数据、设备属性以及设备响应的命令。Capabilities include telemetry the device sends, device properties, and the commands a device responds to. 使用设备模板,可以将实际设备和模拟设备添加到应用程序。Using a device template you can add both real and simulated devices to an application. 在连接实际设备之前,模拟设备对于测试 IoT Central 应用程序的行为非常有用。Simulated devices are useful for testing the behavior of your IoT Central application before you connect real devices.

在本快速入门中,你将添加 ESP32-Azure IoT 工具包开发板的设备模板并创建模拟设备。In this quickstart, you add a device template for an ESP32-Azure IoT Kit development board and create a simulated device. 无需准备真实的设备即可完成本快速入门,你将使用一个模拟的设备。To complete this quickstart you don't need a real device, you work with a simulation of the device. ESP32 设备:An ESP32 device:

  • 发送温度等遥测数据。Sends telemetry such as temperature.
  • 报告特定于设备的属性,例如自设备重新启动以来的最高温度。Reports device-specific properties such as the maximum temperature since the device rebooted.
  • 响应诸如重新启动等命令。Responds to commands such as reboot.
  • 报告常规设备属性,例如固件版本和序列号。Reports generic device properties such as firmware version and serial number.

先决条件Prerequisites

完成创建 Azure IoT Central 应用程序快速入门,使用“自定义应用”>“自定义应用程序”模板创建 IoT Central 应用程序。Complete the Create an Azure IoT Central application quickstart to create an IoT Central application using the Custom app > Custom application template.

创建设备模板Create a device template

若要将新设备模板添加到应用程序,请在左侧窗格中选择“设备模板”选项卡。To add a new device template to your application, select the Device Templates tab in the left pane.

显示设备模板空白列表的屏幕截图

设备模板包括定义以下内容的设备模型:A device template includes a device model that defines:

  • 设备发送的遥测。The telemetry the device sends.
  • 设备属性 。Device properties.
  • 设备响应的命令。The commands the device responds to.

添加设备模板Add a device template

可使用多个选项将设备模型添加到 IoT Central 应用程序。There are several options for adding a device model to your IoT Central application. 可以从头开始创建模型、从文件导入模型,或者从设备目录中选择设备。You can create a model from scratch, import a model from a file, or select a device from the device catalog. IoT Central 还支持“设备优先”方法,即当实际设备首次连接时,应用程序自动从存储库中导入模型。IoT Central also supports a device-first approach where the application automatically imports a model from a repository when a real device connects for the first time.

本快速入门将从设备目录中选择一个设备,以创建设备模板。In this quickstart, you choose a device from the device catalog to create a device template.

以下步骤说明如何使用设备目录导入 ESP32 设备的模型。The following steps show you how to use the device catalog to import the model for an ESP32 device. 这些设备将遥测数据(例如温度)发送到应用程序:These devices send telemetry, such as temperature, to your application:

  1. 若要添加新的设备模板,请在“设备模板”页上选择“+ 新建” 。To add a new device template, select + New on the Device templates page.

  2. 在“选择类型”页上,向下滚动直到在“使用预配置的设备模板”部分中找到“ESP32-Azure IoT 套件”磁贴。 On the Select type page, scroll down until you find the ESP32-Azure IoT Kit tile in the Use a pre-configured device template section.

  3. 选择“ESP32-Azure IoT 工具包”磁贴,然后选择“下一页: 查看”。Select the ESP32-Azure IoT Kit tile, and then select Next: Review.

  4. 在“查看”页上,选择“创建” 。On the Review page, select Create.

  5. 几秒钟后,即可看到新的设备模板:After a few seconds, you can see your new device template:

    显示 ESP32 设备的设备模板的屏幕截图

    模板的名称为“传感器控制器”。The name of the template is Sensor Controller. 该模型包含“传感器控制器”、“SensorTemp”和“设备信息接口”等组件 。The model includes components such as Sensor Controller, SensorTemp, and Device Information interface. 组件定义 ESP32 设备的功能。Components define the capabilities of an ESP32 device. 功能包括遥测、属性和命令。Capabilities include the telemetry, properties, and commands.

添加云属性Add cloud properties

设备模板可以包含云属性。A device template can include cloud properties. 云属性仅存在于 IoT Central 应用程序中,永远不会与设备相互发送和接收信息。Cloud properties only exist in the IoT Central application and are never sent to, or received from, a device. 若要添加两个云属性:To add two cloud properties:

  1. 依次选择“云属性”、“+ 添加云属性”。Select Cloud Properties and then + Add cloud property. 使用下表中的信息将两个云属性添加到设备模板:Use the information in the following table to add two cloud properties to your device template:

    显示名称Display Name 语义类型Semantic Type 架构Schema
    最后维修日期Last Service Date None 日期Date
    客户名称Customer Name None 字符串String
  2. 选择“保存”以保存更改:Select Save to save your changes:

    显示两个云属性的屏幕截图

视图Views

可自定义应用程序以显示设备相关信息。You can customize the application to display relevant information about the device. 自定义让其他人可以管理连接到应用程序的设备。Customizations enable other to manage the devices connected to the application. 可创建两种类型的视图来与设备交互:You can create two types of views to interact with devices:

  • 用于查看和编辑设备与云属性的窗体。Forms to view and edit device and cloud properties.
  • 用于可视化设备(包括其发送的遥测数据)的仪表板。Dashboards to visualize devices including the telemetry they send.

默认视图Default views

默认视图是开始可视化重要设备信息的快捷方法。Default views are a quick way to get started with visualizing your important device information. 最多可为设备模板生成三个默认视图:You can have up to three default views generated for your device template:

  • 通过“命令”视图可将命令发送到设备。The Commands view lets you dispatch commands to your device.
  • “概述”视图使用图表和指标来显示设备遥测数据。The Overview view uses charts and metrics to display device telemetry.
  • “关于”视图显示设备属性。The About view displays device properties.

在设备模板中选择“视图”节点。Select the Views node in the device template. 添加模板时,可以看到 IoT Central 生成了“概述”、“关于”和“原始数据”视图。 You can see that IoT Central generated the Overview, About, and Raw Data views for you when you added the template.

添加新窗体以管理设备:To add a new form to manage the device:

  1. 选择“视图”节点,然后选择“编辑设备和云数据”磁贴来添加新视图。Select the Views node, and then select the Editing device and cloud data tile to add a new view.

  2. 将窗体名称更改为“管理设备”。Change the form name to Manage device.

  3. 依次选择“客户名称”、“上次服务日期”云属性和“目标温度”属性 。Select the Customer Name and Last Service Date cloud properties, and the Target Temperature property. 然后选择“添加部分”:Then select Add section:

    显示添加到设备模板中的新窗体的屏幕截图

  4. 选择“保存”以保存新窗体。Select Save to save your new form.

发布设备模板Publish device template

需要先发布设备模板,然后才能创建模拟设备或连接真实设备。Before you can create a simulated device, or connect a real device, you need to publish your device template. 尽管首次创建模板时 IoT Central 已发布了该模板,但仍必须发布更新的版本。Although IoT Central published the template when you first created it, you must publish the updated version.

若要发布设备模板:To publish a device template:

  1. 从“设备模板”页导航到你的“传感器控制器”设备模板 。Navigate to your Sensor Controller device template from the Device templates page.

  2. 从页面顶部的命令栏中选择“发布”。Select Publish from the command bar at the top of the page.

  3. 在出现的对话框中,选择“发布”。On the dialog that appears, select Publish.

发布设备模板后,它将显示在“设备”页上。After you publish a device template, it's visible on the Devices page. 在发布的设备模板中,除非创建新的版本,否则无法编辑设备模型。In a published device template, you can't edit a device model without creating a new version. 但是,在不创建新版本的情况下,可以在发布的设备模板中修改云属性、自定义项和视图。However, you can modify cloud properties, customizations, and views in a published device template without versioning. 进行任何更改后,选择“发布”以推送这些更改供真实和模拟设备使用。After making any changes, select Publish to push those changes for real and simulated devices to use.

添加模拟设备Add a simulated device

若要将模拟设备添加到应用程序,请使用创建的 ESP32 设备模板。To add a simulated device to your application, use the ESP32 device template you created.

  1. 若要添加新设备,请在左侧窗格中选择“设备”。To add a new device choose Devices in the left pane. “设备”选项卡显示“所有设备”和 ESP32 设备的“传感器控制器”设备模板 。The Devices tab shows All devices and the Sensor Controller device template for the ESP32 device. 选择“传感器控制器”。Select Sensor Controller.

  2. 若要添加模拟的 DevKit 设备,请选择“+ 新建”。To add a simulated DevKit device, select + New. 使用建议的“设备 ID”或输入自己的设备 ID。Use the suggested Device ID or enter your own. 设备 ID 中可以包含字母、数字和 - 字符。A device ID can contain letters, numbers, and the - character. 也可输入新设备的名称。You can also enter a name for your new device. 请确保“模拟此设备”设置为“是”,然后选择“创建” 。Make sure the Simulate this device is set to Yes and then select Create.

    显示模拟的传感器控制器设备的屏幕截图

现在,可使用模拟数据与之前创建的视图交互:Now you can interact with the views that you created earlier using simulated data:

  1. 在“设备”页上选择你的模拟设备Select your simulated device on the Devices page

    • “概述”视图将显示模拟遥测数据的绘图:The Overview view shows a plot of the simulated telemetry:

      显示模拟设备的概述页的屏幕截图

    • “关于”视图显示属性值。The About view shows property values.

    • 在“命令”视图中可以运行命令,例如,针对设备运行 reboot 。The Commands view lets you run commands, such as reboot on the device.

    • “管理设备”视图是你创建的用于管理设备的窗体。The Manage devices view is the form you created to manage the device.

    • 借助“原始数据”视图,可以查看设备发送的原始遥测和属性值。The Raw data view lets you view the raw telemetry and property values sent by the device. 此视图对于调试设备非常有用。This view is useful for debugging devices.

后续步骤Next steps

本快速入门介绍了如何为 ESP32 设备创建传感器控制器设备模板,以及如何将模拟设备添加到应用程序。In this quickstart, you learned how to create a Sensor Controller device template for an ESP32 device and add a simulated device to your application.

若要了解有关连接到应用程序的监视设备的详细信息,请继续阅读快速入门:To learn more about monitoring devices connected to your application, continue to the quickstart: