将 IoT DevKit AZ3166 连接到 Azure IoT 中心Connect IoT DevKit AZ3166 to Azure IoT Hub

MXChip IoT DevKit 可用于开发物联网 (IoT) 解决方案和构建其原型,以利用 Azure 服务。You can use the MXChip IoT DevKit to develop and prototype Internet of Things (IoT) solutions that take advantage of Azure services. 它包括一个具有大量外围设备和传感器的 Arduino 兼容的板、一个开源板包和一个丰富的示例库It includes an Arduino-compatible board with rich peripherals and sensors, an open-source board package, and a rich sample gallery.

学习内容What you learn

  • 如何创建 IoT 中心以及注册 MXChip IoT DevKit 的设备。How to create an IoT hub and register a device for the MXChip IoT DevKit.
  • 如何将 IoT DevKit 连接到 Wi-Fi 并配置 IoT 中心连接字符串。How to connect the IoT DevKit to Wi-Fi and configure the IoT Hub connection string.
  • 如何将 DevKit 传感器遥测数据发送到 IoT 中心。How to send the DevKit sensor telemetry data to your IoT hub.
  • 如何准备开发环境并为 IoT DevKit 开发应用程序。How to prepare the development environment and develop application for the IoT DevKit.

还没有 DevKit?Don't have a DevKit yet? 请尝试 DevKit 模拟器购买 DevKitTry the DevKit simulator or purchase a DevKit.

可以在 IoTDevEnvExamples 存储库中找到所有 DevKit 教程的源代码。You can find the source code for all DevKit tutorials at the IoTDevEnvExamples repository.

需要什么What you need

准备硬件Prepare your hardware

将以下硬件挂接到计算机:Hook up the following hardware to your computer:

  • DevKit 板DevKit board
  • Micro-USB 电缆Micro-USB cable

所需硬件

请按照以下步骤将 DevKit 连接到计算机:To connect the DevKit to your computer, follow these steps:

  1. 将 USB 端连接到计算机。Connect the USB end to your computer.
  2. 将 Micro-USB 端连接到 DevKit。Connect the Micro-USB end to the DevKit.
  3. 电源的绿色 LED 灯确认连接。The green LED for power confirms the connection.

硬件连接

快速入门:将遥测数据从 DevKit 发送到 IoT 中心Quickstart: Send telemetry from DevKit to an IoT Hub

本快速入门将使用预编译的 DevKit 固件将遥测数据发送到 IoT 中心。The quickstart uses pre-compiled DevKit firmware to send the telemetry to the IoT Hub. 在运行此工具之前,请先创建一个 IoT 中心并在中心注册设备。Before you run it, you create an IoT hub and register a device with the hub.

创建 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.

    Important

    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.

注册设备Register a device

必须先将设备注册到 IoT 中心,然后该设备才能进行连接。A device must be registered with your IoT hub before it can connect. 在本快速入门中,将使用 Azure CLI 来注册模拟设备。In this quickstart, you use the Azure CLI to register a simulated device.

  1. 运行以下命令,以创建设备标识。Run the following command to create the device identity.

    YourIoTHubName:将下面的占位符替换为你为 IoT 中心选择的名称。YourIoTHubName: Replace this placeholder below with the name you choose for your IoT hub.

    MyNodeDevice:所注册的设备的名称。MyNodeDevice: The name of the device you're registering. 如下所示请使用 MyNodeDevice 。 Use MyNodeDevice as shown. 如果为设备选择其他名称,则需要在本文中从头至尾使用该名称,并在运行示例应用程序之前在其中更新设备名称。If you choose a different name for your device, you need to use that name throughout this article, and update the device name in the sample applications before you run them.

    az iot hub device-identity create --hub-name YourIoTHubName --device-id MyNodeDevice
    

    Note

    如果运行 device-identity 时出现错误,请安装适用于 Azure CLI 的 Azure IOT 扩展以了解更多详细信息。If you get an error running device-identity, install the Azure IOT Extension for Azure CLI for more details.

  2. 运行以下命令,获取刚注册设备的_设备连接字符串_:Run the following commands to get the device connection string for the device you just registered:

    YourIoTHubName:将下面的占位符替换为你为 IoT 中心选择的名称。YourIoTHubName: Replace this placeholder below with the name you choose for your IoT hub.

    az iot hub device-identity show-connection-string --hub-name YourIoTHubName --device-id MyNodeDevice --output table
    

    记下如下所示的设备连接字符串:Make a note of the device connection string, which looks like:

    HostName={YourIoTHubName}.azure-devices.cn;DeviceId=MyNodeDevice;SharedAccessKey={YourSharedAccessKey}

    稍后会在快速入门中用到此值。You use this value later in the quickstart.

发送 DevKit 遥测数据Send DevKit telemetry

DevKit 将连接到 IoT 中心内特定于设备的终结点,并发送温度和湿度遥测数据。The DevKit connects to a device-specific endpoint on your IoT hub and sends temperature and humidity telemetry.

  1. 下载适用于 IoT DevKit 的最新版 GetStarted 固件Download the latest version of GetStarted firmware for IoT DevKit.

  2. 确保 IoT DevKit 通过 USB 连接到计算机。Make sure IoT DevKit connect to your computer via USB. 打开文件资源管理器,在其中可以看到一个名为 AZ3166 的 USB 大容量存储设备。Open File Explorer there is a USB mass storage device called AZ3166. 打开 Windows 资源管理器Open Windows Explorer

  3. 将刚刚下载的固件拖放到该大容量存储设备,然后该设备会自动闪烁。Drag and drop the firmware just downloaded into the mass storage device and it will flash automatically. 复制固件Copy firmware

  4. 在 DevKit 上,按住按钮 B 不放,按下再松开“重置”按钮,然后松开按钮 B。 DevKit 将进入 AP 模式。On the DevKit, Hold down button B, push and release the Reset button, and then release button B. Your DevKit enters AP mode. 屏幕将显示 DevKit 的服务集标识符 (SSID) 和配置门户 IP 地址供你确认。To confirm, the screen displays the service set identifier (SSID) of the DevKit and the configuration portal IP address.

    “重置”按钮、按钮 B 和 SSID

    设置 AP 模式

  5. 在另一个启用了 Wi-Fi 的设备(计算机或手机)上使用 Web 浏览器连接到上一步骤中显示的 IoT DevKit SSID。Use a Web browser on a different Wi-Fi enabled device (computer or mobile phone) to connect to the IoT DevKit SSID displayed in the previous step. 如果出现密码提示,请将密码留空。If it asks for a password, leave it empty.

    连接 SSID

  6. 在浏览器中打开 192.168.0.1Open 192.168.0.1 in the browser. 选择 IoT DevKit 所要连接到的 Wi-Fi,键入 Wi-Fi 密码,然后粘贴前面记下的设备连接字符串。Select the Wi-Fi that you want the IoT DevKit connect to, type the Wi-Fi password, then paste the device connection string you made note of previously. 然后单击“保存”。Then click Save.

    配置 UI

    Note

    IoT DevKit 仅支持 2.4GHz 网络。The IoT DevKit only supports 2.4GHz network. 有关详细信息,请查阅 FAQCheck FAQ for more details.

  7. 出现结果页时,WiFi 信息和设备连接字符串将存储到 IoT DevKit 中。The WiFi information and device connection string will be stored into the IoT DevKit when you see the result page. 配置结果Configuration result

    Note

    配置 Wi-Fi 后,即使拔下设备插头,该连接的凭据也会保留在设备上。After Wi-Fi is configured, your credentials will persist on the device for that connection, even if the device is unplugged.

  8. IoT DevKit 在几秒钟后将重新启动。The IoT DevKit reboots in a few seconds. 在 DevKit 屏幕上,可以看到 DevKit 的 IP 地址,后接遥测数据,包括温度和湿度值,以及发送到 Azure IoT 中心的消息计数。On the DevKit screen, you see the IP address for the DevKit follows by the telemetry data including temperature and humidity value with message count send to Azure IoT Hub.

    WiFi IP

    发送数据

  9. 若要验证发送到 Azure 的遥测数据,请运行以下命令:To verify the telemetry data sent to Azure, run the following command:

    az iot hub monitor-events --hub-name YourIoTHubName --output table
    

准备开发环境Prepare the development environment

遵循以下步骤准备 DevKit 的开发环境:Follow these steps to prepare the development environment for the DevKit:

安装包含 Azure IoT Tools 扩展包的 Visual Studio CodeInstall Visual Studio Code with Azure IoT Tools extension package

  1. 安装 Arduino IDEInstall Arduino IDE. 此 IDE 提供必要的工具链用于编译和上传 Arduino 代码。It provides the necessary toolchain for compiling and uploading Arduino code.

    • Windows :使用 Windows Installer 版本。Windows: Use Windows Installer version. 不要从应用商店安装。Do not install from the app store.
    • macOS:将解压缩的 Arduino.app 拖放到 /Applications 文件夹中。macOS: Drag and drop the extracted Arduino.app into /Applications folder.
    • Ubuntu:解压缩到某个文件夹中,例如 $HOME/Downloads/arduino-1.8.8Ubuntu: Unzip it into folder such as $HOME/Downloads/arduino-1.8.8
  2. 安装 Visual Studio Code,这是一个跨平台的源代码编辑器,具有强大的智能、代码自动填写和调试支持,以及可从市场安装的丰富扩展。Install Visual Studio Code, a cross platform source code editor with powerful intellisense, code completion and debugging support as well as rich extensions can be installed from marketplace.

  3. 启动 VS Code,在扩展市场中找到 Arduino 并安装它。Launch VS Code, look for Arduino in the extension marketplace and install it. 此扩展提供在 Arduino 平台上进行开发的增强体验。This extension provides enhanced experiences for developing on Arduino platform. 安装 ArduinoInstall Arduino

  4. 在扩展市场中找到 Azure IoT Tools 并安装它。Look for Azure IoT Tools in the extension marketplace and install it. 安装 Azure IoT ToolsInstall Azure IoT Tools

    或者使用以下直接链接:Or use this direct link:

    Note

    Azure IoT Tools 扩展包包含用于在各种 IoT devkit 设备上进行开发和调试的 Azure IoT Device WorkbenchThe Azure IoT Tools extension pack contains the Azure IoT Device Workbench which is used to develop and debug on various IoT devkit devices. 同样随附在 Azure IoT Tools 扩展包中的 Azure IoT 中心工具包用于管理 Azure IoT 中心以及与之交互。The Azure IoT Hub Toolkit, also included with the Azure IoT Tools extension pack, is used to manage and interact with Azure IoT Hubs.

  5. 为 VS Code 配置 Arduino 设置。Configure VS Code with Arduino settings.

    在 Visual Studio Code 中,单击“文件”>“首选项”>“设置” (在 MacOS 上,单击“代码”>“首选项”>“设置” )。In Visual Studio Code, click File > Preferences > Settings (on macOS, Code > Preferences > Settings). 然后单击“设置” 页右上角的“打开设置(JSON)” 图标。Then click the Open Settings (JSON) icon in the upper-right corner of the Settings page.

    安装 Azure IoT Tools

    根据你的平台添加以下行来配置 Arduino:Add following lines to configure Arduino depending on your platform:

    • Windows :Windows:

      "arduino.path": "C:\\Program Files (x86)\\Arduino",
      "arduino.additionalUrls": "https://raw.githubusercontent.com/VSChina/azureiotdevkit_tools/master/package_azureboard_index.json"
      
    • macOSmacOS:

      "arduino.path": "/Applications",
      "arduino.additionalUrls": "https://raw.githubusercontent.com/VSChina/azureiotdevkit_tools/master/package_azureboard_index.json"
      
    • UbuntuUbuntu:

      将下面的 {username} 占位符替换为你的用户名。Replace the {username} placeholder below with your username.

      "arduino.path": "/home/{username}/Downloads/arduino-1.8.8",
      "arduino.additionalUrls": "https://raw.githubusercontent.com/VSChina/azureiotdevkit_tools/master/package_azureboard_index.json"
      
  6. 单击 F1 以打开命令面板,键入并选择 Arduino:Board ManagerClick F1 to open the command palette, type and select Arduino: Board Manager. 搜索 AZ3166 并安装最新版本。Search for AZ3166 and install the latest version. 安装 DevKit SDKInstall DevKit SDK

ST-Link/V2 是 IoT DevKit 用来与开发计算机通信的 USB 接口。ST-Link/V2 is the USB interface that IoT DevKit uses to communicate with your development machine. 需要将它安装在 Windows 上才能将编译的设备代码刷写到 DevKit。You need to install it on Windows to flash the compiled device code to the DevKit. 遵循 OS 特定的步骤,使计算机能够访问你的设备。Follow the OS-specific steps to allow the machine access to your device.

  • Windows:从 STMicroelectronics 网站下载并安装 USB 驱动程序。Windows: Download and install USB driver from STMicroelectronics website.
  • macOS:macOS 无需驱动程序。macOS: No driver is required for macOS.
  • Ubuntu:在终端中运行以下命令,然后注销并登录,使组更改生效:Ubuntu: Run the commands in terminal and sign out and sign in for the group change to take effect:
    # Copy the default rules. This grants permission to the group 'plugdev'
    sudo cp ~/.arduino15/packages/AZ3166/tools/openocd/0.10.0/linux/contrib/60-openocd.rules /etc/udev/rules.d/
    sudo udevadm control --reload-rules
    
    # Add yourself to the group 'plugdev'
    # Logout and log back in for the group to take effect
    sudo usermod -a -G plugdev $(whoami)
    

现已完成开发环境的准备和配置工作。Now you are all set with preparing and configuring your development environment. 让我们生成刚刚运行的 GetStarted 示例。Let us build the GetStarted sample you just ran.

生成第一个项目Build your first project

IoT DevKit 包含丰富的示例库,可以使用这些示例来了解如何将 DevKit 连接到各种 Azure 服务。The IoT DevKit contains a rich gallery of samples that you can use to learn connect the DevKit to various Azure services.

  1. 确保 IoT DevKit 未连接到计算机。Make sure your IoT DevKit is not connected to your computer. 先启动 VS Code,然后将 DevKit 连接到计算机。Start VS Code first, and then connect the DevKit to your computer.

  2. 单击 F1 以打开命令面板,键入并选择 Azure IoT Device Workbench: Open Examples...Click F1 to open the command palette, type and select Azure IoT Device Workbench: Open Examples.... 然后选择“IoT DevKit”作为开发板。 Then select IoT DevKit as board.

  3. 在 IoT Workbench 的“示例”页中,找到“入门”并单击“打开示例”。 In the IoT Workbench Examples page, find Get Started and click Open Sample. 然后选择用于下载示例代码的默认路径。Then selects the default path to download the sample code.

    打开示例

预配 Azure IoT 中心和设备Provision Azure IoT Hub and device

如果不通过 Azure 门户预配 Azure IoT 中心和设备,可以在不退出开发环境的情况下在 VS Code 中执行此操作。Instead of provisioning Azure IoT Hub and device from the Azure portal, you can do it in the VS Code without leaving the development environment.

  1. 在 VS Code 中登录到 Azure 中国。Sign in to Azure China in VS Code. 若要执行此操作,请调用命令“Azure:登录到 Azure 云”,然后选择“Azure 中国”,在弹出的 Web 页面中填写 Azure 中国的帐户名和密码。To do so, invoke the command of ‘Azure: Sign in to Azure Cloud’, then select ‘Azure China’, in the popup web page, fill in the account name and password of Azure China. 然后,用户需要调用:“Azure:选择订阅”,选择要使用的订阅,并单击“确定”。Then, user needs to invoke: ‘Azure: Select Subscription’, select the subscription to use and click OK.

    登录 Azure

    Note

    假定已在 VS code 中安装了扩展“Azure 帐户”。It's assumed that the extension "Azure Account" has been installed in VS code.

  2. 在打开的新项目窗口中,单击 F1 以打开命令面板,键入并选择“Azure IoT Device Workbench: Provision Azure Services...”。遵照分步指南完成预配 Azure IoT 中心和创建 IoT 中心设备的操作。In the new opened project window, click F1 to open the command palette, type and select Azure IoT Device Workbench: Provision Azure Services.... Follow the step by step guide to finish provisioning your Azure IoT Hub and creating the IoT Hub device. 预配命令Provision command

    Note

    如果你尚未登录到 Azure,If you have not signed in Azure. 请遵循弹出的通知登录。Follow the pop-up notification for signing in.

  3. 选择要使用的订阅。Select the subscription you want to use. 选择订阅Select sub

  4. 然后选择或新建一个资源组Then select or create a new resource group. 选择资源组Select resource group

  5. 在指定的资源组中,遵循指南选择或新建 Azure IoT 中心。In the resource group you specified, follow the guide to select or create a new Azure IoT Hub. 选择 IoT 中心的步骤Select IoT Hub steps

    选择 IoT 中心

    选定的 IoT 中心

  6. 在输出窗口中,可以看到预配的 Azure IoT 中心预配的 IoT 中心In the output window, you will see the Azure IoT Hub provisioned IoT Hub Provisioned

  7. 在预配的 Azure IoT 中心内选择或新建一个设备。Select or create a new device in Azure IoT Hub you provisioned. 选择 IoT 设备的步骤Select IoT Device steps

    选择预配的 IoT 设备

  8. 现已预配 Azure IoT 中心并在其中创建了设备。Now you have Azure IoT Hub provisioned and device created in it. 另外,设备连接字符串将保存在 VS Code 中,供将来配置 IoT DevKit。Also the device connection string will be saved in VS Code for configuring the IoT DevKit later. 完成预配Provision done

配置并编译设备代码Configure and compile device code

  1. 在右下方的状态栏中,检查选定的开发板是否为 MXCHIP AZ3166,以及是否使用了带有 STMicroelectronics 的串行端口。In the bottom-right status bar, check the MXCHIP AZ3166 is shown as selected board and serial port with STMicroelectronics is used. 选择开发板和 COMSelect board and COM

  2. 单击 F1 打开命令面板,键入并选择 Azure IoT Device Workbench: Configure Device Settings... ,然后选择“配置设备连接字符串”>“选择 IoT 中心设备连接字符串”。 Click F1 to open the command palette, type and select Azure IoT Device Workbench: Configure Device Settings..., then select Config Device Connection String > Select IoT Hub Device Connection String.

  3. 在 DevKit 上,按住按钮 A 不放,按下再松开重置按钮,然后松开按钮 A。DevKit 将进入配置模式并保存连接字符串。On DevKit, hold down button A, push and release the reset button, and then release button A. Your DevKit enters configuration mode and saves the connection string.

    连接字符串

  4. 请保存以下代码作为 C 源文件,稍后将使用它。Save the following code as a C source file, it will be used later.

    // Copyright (c) Microsoft. All rights reserved.
    // Licensed under the MIT license. See LICENSE file in the project root for full license information.
    
    /* This file contains certs needed to communicate with Azure (IoT) */
    
    #include "azureiotcerts.h"
    
    const char certificates[] = 
            /* Baltimore */
            "-----BEGIN CERTIFICATE-----\r\n"
            "MIIElDCCA3ygAwIBAgIQAf2j627KdciIQ4tyS8+8kTANBgkqhkiG9w0BAQsFADBh\r\n"
            "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\r\n"
            "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\r\n"
            "QTAeFw0xMzAzMDgxMjAwMDBaFw0yMzAzMDgxMjAwMDBaME0xCzAJBgNVBAYTAlVT\r\n"
            "MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxJzAlBgNVBAMTHkRpZ2lDZXJ0IFNIQTIg\r\n"
            "U2VjdXJlIFNlcnZlciBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\r\n"
            "ANyuWJBNwcQwFZA1W248ghX1LFy949v/cUP6ZCWA1O4Yok3wZtAKc24RmDYXZK83\r\n"
            "nf36QYSvx6+M/hpzTc8zl5CilodTgyu5pnVILR1WN3vaMTIa16yrBvSqXUu3R0bd\r\n"
            "KpPDkC55gIDvEwRqFDu1m5K+wgdlTvza/P96rtxcflUxDOg5B6TXvi/TC2rSsd9f\r\n"
            "/ld0Uzs1gN2ujkSYs58O09rg1/RrKatEp0tYhG2SS4HD2nOLEpdIkARFdRrdNzGX\r\n"
            "kujNVA075ME/OV4uuPNcfhCOhkEAjUVmR7ChZc6gqikJTvOX6+guqw9ypzAO+sf0\r\n"
            "/RR3w6RbKFfCs/mC/bdFWJsCAwEAAaOCAVowggFWMBIGA1UdEwEB/wQIMAYBAf8C\r\n"
            "AQAwDgYDVR0PAQH/BAQDAgGGMDQGCCsGAQUFBwEBBCgwJjAkBggrBgEFBQcwAYYY\r\n"
            "aHR0cDovL29jc3AuZGlnaWNlcnQuY29tMHsGA1UdHwR0MHIwN6A1oDOGMWh0dHA6\r\n"
            "Ly9jcmwzLmRpZ2ljZXJ0LmNvbS9EaWdpQ2VydEdsb2JhbFJvb3RDQS5jcmwwN6A1\r\n"
            "oDOGMWh0dHA6Ly9jcmw0LmRpZ2ljZXJ0LmNvbS9EaWdpQ2VydEdsb2JhbFJvb3RD\r\n"
            "QS5jcmwwPQYDVR0gBDYwNDAyBgRVHSAAMCowKAYIKwYBBQUHAgEWHGh0dHBzOi8v\r\n"
            "d3d3LmRpZ2ljZXJ0LmNvbS9DUFMwHQYDVR0OBBYEFA+AYRyCMWHVLyjnjUY4tCzh\r\n"
            "xtniMB8GA1UdIwQYMBaAFAPeUDVW0Uy7ZvCj4hsbw5eyPdFVMA0GCSqGSIb3DQEB\r\n"
            "CwUAA4IBAQAjPt9L0jFCpbZ+QlwaRMxp0Wi0XUvgBCFsS+JtzLHgl4+mUwnNqipl\r\n"
            "5TlPHoOlblyYoiQm5vuh7ZPHLgLGTUq/sELfeNqzqPlt/yGFUzZgTHbO7Djc1lGA\r\n"
            "8MXW5dRNJ2Srm8c+cftIl7gzbckTB+6WohsYFfZcTEDts8Ls/3HB40f/1LkAtDdC\r\n"
            "2iDJ6m6K7hQGrn2iWZiIqBtvLfTyyRRfJs8sjX7tN8Cp1Tm5gr8ZDOo0rwAhaPit\r\n"
            "c+LJMto4JQtV05od8GiG7S5BNO98pVAdvzr508EIDObtHopYJeS4d60tbvVS3bR0\r\n"
            "j6tJLp07kzQoH3jOlOrHvdPJbRzeXDLz\r\n"
            "-----END CERTIFICATE-----\r\n";
    
  5. 由于“Azure 中国”和“Azure 全球”的证书有所不同,用户需要将以下证书文件替换为上一步中保存的文件。Due to the cert different in Azure China and Azure Global, user need to replace following cert file with the file saved in last step.

    C:\Users{你的别名}\AppData\Local\Arduino15\packages\AZ3166\hardware\stm32f4\1.6.1\cores\arduino\azure-iot-sdk-c\azureiotcerts.cC:\Users{your Alias}\AppData\Local\Arduino15\packages\AZ3166\hardware\stm32f4\1.6.1\cores\arduino\azure-iot-sdk-c\azureiotcerts.c

    请注意,该路径可能因 IoT DevKit SDK 的不同版本而异。Please note that the path could be different for different version of IoT DevKit SDK.

  6. 再次单击 F1,键入并选择 Azure IoT Device Workbench: Upload Device CodeClick F1 again, type and select Azure IoT Device Workbench: Upload Device Code. 它启动编译,并将代码上传到 DevKit。It starts compile and upload the code to DevKit.

    Arduino 上传

DevKit 将重新启动并开始运行代码。The DevKit reboots and starts running the code.

Note

如果出现了任何错误或中断,始终可以再次运行该命令予以恢复。If there is any errors or interruptions, you can always recover by running the command again.

测试项目Test the project

查看发送到 Azure IoT 中心的遥测数据View the telemetry sent to Azure IoT Hub

单击状态栏上的电源插头图标,打开串行监视器:串行监视器Click the power plug icon on the status bar to open the Serial Monitor: Serial monitor

当看到以下结果时,示例应用程序已成功运行:The sample application is running successfully when you see the following results:

  • 串行监视器显示发送到 IoT 中心的消息。The Serial Monitor displays the message sent to the IoT Hub.
  • MXChip IoT DevKit 上的 LED 灯闪烁。The LED on the MXChip IoT DevKit is blinking.

串行监视器输出

查看 Azure IoT 中心收到的遥测数据View the telemetry received by Azure IoT Hub

可以使用 Azure IoT 工具监视 IoT 中心内的设备到云 (D2C) 消息。You can use Azure IoT Tools to monitor device-to-cloud (D2C) messages in IoT Hub.

  1. 登录 Azure 门户,并找到创建的 IoT 中心。Sign in Azure portal, find the IoT Hub you created. Azure 门户Azure portal

  2. 在“共享访问策略”窗格中单击“iothubowner 策略”,并记下 IoT 中心的连接字符串。 In the Shared access policies pane, click the iothubowner policy, and write down the Connection string of your IoT hub. Azure IoT 中心连接字符串Azure IoT Hub connection string

  3. 在 VS Code 中,单击 F1,键入并选择“Azure IoT Hub: Set IoT Hub Connection String”。In VS Code, click F1, type and select Azure IoT Hub: Set IoT Hub Connection String. 将连接字符串复制到其中。Copy the connection string into it. 设置 Azure IoT 中心连接字符串Set Azure IoT Hub connection string

  4. 展开右侧的“AZURE IOT 中心设备”窗格,右键单击创建的设备名称,然后选择“开始监视内置事件终结点”。 Expand the AZURE IOT HUB DEVICES pane on the right, right click on the device name you created and select Start Monitoring Built-in Event Endpoint. 监视 D2C 消息Monitor D2C Message

  5. 在“输出”窗格中,可以看到传入 IoT 中心的 D2C 消息。 In OUTPUT pane, you can see the incoming D2C messages to the IoT Hub. D2C 消息D2C message

查看代码Review the code

GetStarted.ino 是主要的 Arduino 草图文件。The GetStarted.ino is the main Arduino sketch file.

D2C 消息

若要查看设备遥测数据如何发送到 Azure IoT 中心,请打开同一文件夹中的 utility.cpp 文件。To see how device telemetry is sent to the Azure IoT Hub, open the utility.cpp file in the same folder. 查看 API 参考,了解如何在 IoT DevKit 中使用传感器和外围设备。View API Reference to learn how to use sensors and peripherals on IoT DevKit.

使用的 DevKitMQTTClient适用于 C 的 Microsoft Azure IoT SDK 和库iothub_client 的包装器,用于与 Azure IoT 中心交互。The DevKitMQTTClient used is a wrapper of the iothub_client from the Microsoft Azure IoT SDKs and libraries for C to interact with Azure IoT Hub.

问题和反馈Problems and feedback

如果遇到问题,可在 IoT DevKit 常见问题解答中查看解决方法,或者通过 Gitter 联系我们。If you encounter problems, you can check for a solution in the IoT DevKit FAQ or reach out to us from Gitter. 还可在此页中留言,向我们提供反馈。You can also give us feedback by leaving a comment on this page.

后续步骤Next steps

现已成功将 MXChip IoT DevKit 连接到 IoT 中心,并将捕获的传感器数据发送到了 IoT 中心。You have successfully connected an MXChip IoT DevKit to your IoT hub, and you have sent the captured sensor data to your IoT hub.

若要继续了解 Azure IoT 中心入门知识并使用 IoT DevKit 探索其他 IoT 方案,请参阅:To continue to get started with Azure IoT Hub and to explore other IoT scenarios using IoT DevKit, see the following: