注册 Azure IoT Edge 设备Register an Azure IoT Edge device

在 Azure IoT Edge 中使用 IoT 设备之前,必须在 IoT 中心中注册这些设备。Before you can use your IoT devices with Azure IoT Edge, you must register them with your IoT hub. 在注册设备后,即可检索连接字符串,以便针对 IoT Edge 工作负荷设置设备。Once a device is registered, you can retrieve a connection string to set up your device for IoT Edge workloads.

使用以下工具之一即可选择注册:You have the choice of registering by using one of the following tools:

在 Azure 门户中注册Register in the Azure portal

可在 Azure 门户中执行所有注册任务。You can perform all registration tasks in the Azure portal.

Azure 门户的必备组件Prerequisites for the Azure portal

Azure 订阅中的免费或标准 IoT 中心A free or standard IoT hub in your Azure subscription.

在 Azure 门户中创建 IoT Edge 设备Create an IoT Edge device in the Azure portal

在 Azure 门户的 IoT 中心,IoT Edge 设备的创建和管理独立于不支持 Edge 的 IoT 设备。In your IoT Hub in the Azure portal, IoT Edge devices are created and managed separately from IOT devices that are not edge enabled.

  1. 登录 Azure 门户,导航到 IoT 中心。Sign in to the Azure portal and navigate to your IoT hub.
  2. 在左窗格的菜单中,选择“IoT Edge” 。In the left pane, select IoT Edge from the menu.
  3. 选择“添加 IoT Edge 设备” 。Select Add an IoT Edge device.
  4. 提供一个描述性的设备 ID。Provide a descriptive device ID. 使用默认设置自动生成身份验证密钥并将新设备连接到中心。Use the default settings to auto-generate authentication keys and connect the new device to your hub.
  5. 选择“保存”。 Select Save.

在 Azure 门户中查看 IoT Edge 设备View IoT Edge devices in the Azure portal

所有连接到 IoT 中心并已启用 Edge 的设备都列在 IoT Edge 页上。All the edge-enabled devices that connect to your IoT hub are listed on the IoT Edge page.

查看 IoT 中心中所有的 IoT Edge 设备

在 Azure 门户中检索连接字符串Retrieve the connection string in the Azure portal

如果已准备好设置设备,则需要连接字符串,该字符串使用物理设备在 IoT 中心内的标识链接该设备。When you're ready to set up your device, you need the connection string that links your physical device with its identity in the IoT hub.

  1. 在门户的 IoT Edge 页中,单击 IoT Edge 设备列表中的设备 ID。From the IoT Edge page in the portal, click on the device ID from the list of IoT Edge devices.
  2. 复制“主连接字符串”或“辅助连接字符串”的值 。Copy the value of either Primary Connection String or Secondary Connection String.

注册到 Visual Studio CodeRegister with Visual Studio Code

有多种方法可以执行 VS Code 中的大部分操作。There are multiple ways to perform most operations in VS Code. 本文使用了资源管理器,但你也可使用命令面板来执行相关步骤。This article uses the Explorer, but you can also use the Command Palette to run the steps.

Visual Studio Code 的必备组件Prerequisites for Visual Studio Code

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

可使用适用于 Visual Studio Code 的 Azure IoT 扩展来执行与 IoT 中心相关的操作。You can use the Azure IoT extensions for Visual Studio Code to perform operations with your IoT Hub. 为了使这些操作顺利执行,你需要登录到 Azure 帐户并选择 IoT 中心。For these operations to work, you need to sign in to your Azure account and select your IoT Hub.

  1. 在 Visual Studio Code 中打开“资源管理器”视图 。In Visual Studio Code, open the Explorer view.

  2. 在资源管理器底部,展开“Azure IoT 中心”部分 。At the bottom of the Explorer, expand the Azure IoT Hub section.

    展开“Azure IoT 中心设备”部分

  3. 单击“Azure IoT 中心”部分标题中的“...” 。Click on the ... in the Azure IoT Hub section header. 如果没有看到省略号,请单击标题或将鼠标指针悬停在标题上。If you don't see the ellipsis, click on or hover over the header.

  4. 选择“选择 IoT 中心” 。Choose Select IoT Hub.

  5. 如果尚未登录到 Azure 帐户,请按照相关提示执行此操作。If you aren't signed in to your Azure account, follow the prompts to do so.

  6. 选择 Azure 订阅。Select your Azure subscription.

  7. 选择 IoT 中心。Select your IoT hub.

使用 Visual Studio Code 创建 IoT Edge 设备Create an IoT Edge device with Visual Studio Code

  1. 在 VS Code 资源管理器中,展开“Azure IoT 中心设备”部分 。In the VS Code Explorer, expand the Azure IoT Hub Devices section.
  2. 单击“Azure IoT 中心设备”部分标题中的“...” 。Click on the ... in the Azure IoT Hub Devices section header. 如果没有看到省略号,请单击标题或将鼠标指针悬停在标题上。If you don't see the ellipsis, click on or hover over the header.
  3. 选择“创建 IoT Edge 设备” 。Select Create IoT Edge Device.
  4. 在打开的文本框中提供设备 ID。In the text box that opens, give your device an ID.

在输出屏幕中,可以看到命令的结果。In the output screen, you see the result of the command. 其中显示有设备信息,包括所提供的“deviceId”以及可用于将物理设备连接到 IoT 中心的“connectionString” 。The device info is printed, which includes the deviceId that you provided and the connectionString that you can use to connect your physical device to your IoT hub.

在输出屏幕中,可以看到命令的结果。In the output screen, you see the result of the command. 其中显示有设备信息,包括所提供的“deviceId”以及可用于将物理设备连接到 IoT 中心的“connectionString” 。The device info is printed, which includes the deviceId that you provided and the connectionString that you can use to connect your physical device to your IoT hub.

使用 Visual Studio Code 查看 IoT Edge 设备View IoT Edge devices with Visual Studio Code

Visual Studio Code 资源管理器的“Azure IoT 中心”部分中列出了连接到 IoT 中心的所有设备 。All the devices that connect to your IoT hub are listed in the Azure IoT Hub section of the Visual Studio Code Explorer. IoT Edge 设备和非 Edge 设备可通过不同的图标加以区别。事实上, $edgeAgent$edgeHub 模块会部署到每个 IoT Edge 设备。IoT Edge devices are distinguishable from non-Edge devices with a different icon, and the fact that the $edgeAgent and $edgeHub modules are deployed to each IoT Edge device.

查看 IoT 中心中所有的 IoT Edge 设备

通过 Visual Studio Code 检索连接字符串Retrieve the connection string with Visual Studio Code

如果已准备好设置设备,则需要连接字符串,该字符串使用物理设备在 IoT 中心内的标识链接该设备。When you're ready to set up your device, you need the connection string that links your physical device with its identity in the IoT hub.

  1. 右键单击“Azure IoT 中心”部分中的设备 ID 。Right-click on the ID of your device in the Azure IoT Hub section.

  2. 选择“复制设备连接字符串” 。Select Copy Device Connection String.

    连接字符串会复制到剪贴板。The connection string is copied to your clipboard.

还可从右键菜单中选择“获取设备信息”,在输出窗口中查看包括连接字符串在内的所有设备信息 。You can also select Get Device Info from the right-click menu to see all the device info, including the connection string, in the output window.

注册到 Azure CLIRegister with the Azure CLI

Azure CLI 是一个开源跨平台命令行工具,用于管理 IoT Edge 等 Azure 资源。The Azure CLI is an open-source cross platform command-line tool for managing Azure resources such as IoT Edge. 使用 Azure CLI 2.0 可以管理 Azure IoT 中心资源、设备预配服务实例和现成的链接中心。It enables you to manage Azure IoT Hub resources, device provisioning service instances, and linked-hubs out of the box. IoT 扩展丰富了 Azure CLI 的功能,例如设备管理和完整的 IoT Edge 功能。The IoT extension enriches Azure CLI with features such as device management and full IoT Edge capability.

Azure CLI 的必备组件Prerequisites for the Azure CLI

  • Azure 订阅中的 IoT 中心An IoT hub in your Azure subscription.
  • 环境中的 Azure CLIAzure CLI in your environment. Azure CLI 版本必须至少是 2.0.70 或更高版本。At a minimum, your Azure CLI version must be 2.0.70 or above. 请使用 az --version 验证版本。Use az --version to validate. 此版本支持 az 扩展命令,并引入了 Knack 命令框架。This version supports az extension commands and introduces the Knack command framework.
  • 适用于 Azure CLI 的 IoT 扩展The IoT extension for Azure CLI.

通过 Azure CLI 创建 IoT Edge 设备Create an IoT Edge device with the Azure CLI

使用 az iot hub device-identity create 命令在 IoT 中心创建新设备标识。Use the az iot hub device-identity create command to create a new device identity in your IoT hub. 例如:For example:

az iot hub device-identity create --device-id [device id] --hub-name [hub name] --edge-enabled

此命令包括三个参数:This command includes three parameters:

  • device-id:提供对 IoT 中心唯一的描述性名称。device-id: Provide a descriptive name that's unique to your IoT hub.

  • hub-name:提供 IoT 中心的名称。hub-name: Provide the name of your IoT hub.

  • edge-enabled:此参数声明该设备用于 IoT Edge。edge-enabled: This parameter declares that the device is for use with IoT Edge.

    az iot hub device-identity create output

通过 Azure CLI 查看 IoT Edge 设备View IoT Edge devices with the Azure CLI

使用 az iot hub device-identity list 命令在 IoT 中心查看所有设备。Use the az iot hub device-identity list command to view all devices in your IoT hub. 例如:For example:

az iot hub device-identity list --hub-name [hub name]

注册为 IoT Edge 设备的任何设备的 capabilities.iotEdge 属性都会设置为 trueAny device that is registered as an IoT Edge device will have the property capabilities.iotEdge set to true.

通过 Azure CLI 检索连接字符串Retrieve the connection string with the Azure CLI

如果已准备好设置设备,则需要连接字符串,该字符串使用物理设备在 IoT 中心内的标识链接该设备。When you're ready to set up your device, you need the connection string that links your physical device with its identity in the IoT hub. 使用 az iot hub device-identity show-connection-string 命令返回单个设备的连接字符串:Use the az iot hub device-identity show-connection-string command to return the connection string for a single device:

az iot hub device-identity show-connection-string --device-id [device id] --hub-name [hub name]

device-id 参数的值区分大小写。The value for the device-id parameter is case-sensitive. 不要复制连接字符串两端的引号。Don't copy the quotation marks around the connection string.

后续步骤Next steps

在 IoT 中心注册设备标识以后,即可在设备上安装 IoT Edge 运行时。Now that you have a device identity registered in your IoT hub, you're ready to install the IoT Edge runtime on your devices. 根据设备的操作系统安装运行时:Install the runtime according to the device's operating system: