65.9K
CodeProject 正在变化。 阅读更多。
Home

开始使用 Microsoft IoT Edge

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.60/5 (2投票s)

2017年9月18日

CPOL

4分钟阅读

viewsIcon

13978

以下是如何开始使用 Azure IoT Edge。了解如何将 SensorTag 与在 Raspberry Pi 上运行的 IoT Edge 集成。

Azure IoT Edge 是您自己的本地网关,可帮助您将数据通信到云,同时过滤发送的数据。

通常,人们不理解 IoT Edge 计算与雾计算的区别。使用 IoT Edge,处理发生在边缘;而在雾计算架构中,处理发生在集中式计算设备上。

本文将学到什么?

  • 什么是 IoT Edge,它与 Fog 有何不同
  • Azure IoT Edge 的工作原理
  • 使用 Raspberry Pi 和 TI Sensor Tag 实现 Azure IoT Edge

注意:对于不熟悉 Raspberry Pi 的朋友来说,它是一款单板计算机,可用于构建您的 IoT 概念验证。我经常使用 Raspberry Pi 作为概念验证网关来快速构建和测试我的原型。由于开源社区慷慨捐赠的可用库,在 Raspberry Pi 上进行开发非常快速。

Azure IoT Edge 如何工作?

首先,让我们看看 Azure IoT Edge 包含哪些组件。

  • IoT 设备发送数据
  • 促使数据传输到 IoT 网关的 IoT 协议
  • IoT 网关,具有:协议集成、过滤器、云通信能力

此外,理解这个庞大的生态系统很重要。它具有以下功能的特性:

  • 设备到云通信:帮助您在设备到云消息、数字孪生的报告属性等之间进行选择。
  • 云到设备通信:选择直接方法、数字孪生的所需属性等。
  • 通过 IoT Hub 进行设备到云和云到设备的通信,该 Hub:使用 IoT Hub 发送设备到云消息,从内置终端读取设备消息。
  • 为设备到云消息创建自定义终端和路由规则。
  • 创建和读取 IoT Hub 消息。

使用 Azure IoT Hub,您可以:

  • 从设备获取文件
  • 管理设备标识
  • 控制谁可以访问什么
  • 同步状态和配置
  • 在多个设备上计划作业
  • 消息路由、限制配额和限流。

这是我构建的 Azure IoT Edge 的一个示例。

左侧终端显示从设备发送的实际数据,右侧终端显示使用 iothub-explorer 监控的值。

iothub-explorer monitor-events --login "HostName={Your iot hub name}.azure-
devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey={Your IoT Hub key}"

Azure IoT Edge 中的协议采集模块详细信息

  1. 模块从物理设备读取数据。
  2. 在消息中添加具有 MAC 地址的属性。

以上构成了一个协议采集模块。该模块负责接收来自设备的数据,并将其从 IoT 网关推送到云。

在接下来的步骤中,我们将学习如何在 Raspberry Pi 上运行 IoT Edge,该设备使用 BLE 协议从 TI Sensor Tag 收集数据。

安装 Raspberry Pi 上的 IoT Edge 先决条件

  • 打开您的 shell 并输入:
    sudo-apt-get update
  • 通过以下方式安装所有依赖项:>
    sudo apt-get install curl build-essential libcurl4-openssl-dev
    	git cmake libssl-dev uuid-dev valgrind libglib2.0-dev libtool autoconf

在您的 Azure 订阅上,执行以下操作:

  • 创建 IoT Hub
  • 向您的 IoT Hub 添加两个设备,并记下它们的 ID 和密钥。使用 iothub-explorer 工具进行操作。

现在,通过以下方式将 Azure IoT Edge 存储库克隆到您的本地计算机(Raspberry Pi):

 git clone https://github.com/Azure/iot-edge.git

请注意,尽管这是微软自己的技术,我在处理 Windows 时遇到了巨大的问题,而它在 Linux 上却运行得非常顺利。

如果您以前从未使用过 Raspberry Pi,我建议您先阅读以下教程。请确保 Raspbian 已正确安装。

安装完成后,您可以轻松地 SSH 进入您的 Raspberry Pi。

将 BLE Sensor Tag 与 Raspberry Pi 集成

BlueZ 堆栈是最常用的库之一,需要 TI Sensor Tag 才能与您的 Raspberry Pi 一起工作。

在您的 Raspberry Pi 上打开控制台并输入以下命令:

sudo apt-get update<br /> sudo apt-get install bluetooth blues-tools 
build-essential autoconf glib2.0 libglib2.0-dev libdbus-1-dev libudev-dev libical-dev libreadline—dev

wget https://linuxkernel.org.cn/pub/linux/bluetooth/bluez-5.37.tar.xz

现在,转到您刚刚创建的目录并配置 BlueZ 代码。

cd blues-5.37 ./configure —disable-dev —disable-systems —enable-experimental

make sudo make install

现在,更改系统服务配置以支持 BLE。

ExecStart=/usr/local/libexec/bluetooth/bluetoothd -E

现在,您可以将 Raspberry Pi 与 Sensor Tag 连接。

在之前的笔记中,我写了关于将设备添加到 IoT Hub 的内容。在这里,您可以添加您的 BLE Sensor Tag,我们称之为 My_sensor_tag

配置您的 Raspberry Pi 上的 IoT Edge

安装依赖项。

sudo apt-get install cmake uuid-dev curl libcurl4-openssl-dev libssl-dev

我们在之前的笔记中已经克隆了 IoT Edge 和所有必需的依赖项。

让我们构建存储库。

cd ~/iot-edge ./tools/build.sh  --disable-native-remote-modules

配置每个 IoT Edge 模块以在您的 Raspberry Pi 3 上运行 BLE。

配置位于 /home/pi/iot-edge/ 的日志记录器。

模块应该如下所示:

{
 "name": "Logger",
 "loader": {
   "name" : "native",
   "entrypoint" : {
     "module.path" : "build/modules/logger/liblogger.so"
   }
 },
 "args":
 {
   "filename": "<</path/to/log-file.log>>"
 }
}

BLE 模块配置。

{
 "name": "SensorTag",
 "loader": {
   "name" : "native",
   "entrypoint" : {
     "module.path": "build/modules/ble/libble.so"
   }
 },
 "args": {
   "controller_index": 0,
   "device_mac_address": "<<AA:BB:CC:DD:EE:FF>>",
   "instructions": [
     {
       "type": "read_once",
       "characteristic_uuid": "00002A24-0000-1000-8000-00805F9B34FB"
     },
     {
       "type": "read_once",
       "characteristic_uuid": "00002A25-0000-1000-8000-00805F9B34FB"
     },
     {
       "type": "read_once",
       "characteristic_uuid": "00002A26-0000-1000-8000-00805F9B34FB"
     },
     {
       "type": "read_once",
       "characteristic_uuid": "00002A27-0000-1000-8000-00805F9B34FB"
     },
     {
       "type": "read_once",
       "characteristic_uuid": "00002A28-0000-1000-8000-00805F9B34FB"
     },
     {
       "type": "read_once",
       "characteristic_uuid": "00002A29-0000-1000-8000-00805F9B34FB"
     },
     {
       "type": "write_at_init",
       "characteristic_uuid": "F000AA02-0451-4000-B000-000000000000",
       "data": "AQ=="
     },
     {
       "type": "read_periodic",
       "characteristic_uuid": "F000AA01-0451-4000-B000-000000000000",
       "interval_in_ms": 1000
     },
     {
       "type": "write_at_exit",
       "characteristic_uuid": "F000AA02-0451-4000-B000-000000000000",
       "data": "AA=="
     }
   ]
 }
}

IoT Hub 模块配置。

{
 "name": "IoTHub",
 "loader": {
   "name" : "native",
   "entrypoint" : {
     "module.path": "build/modules/iothub/libiothub.so"
   }
 },
 "args": {
   "IoTHubName": "<<Azure IoT Hub Name>>",
   "IoTHubSuffix": "<<Azure IoT Hub Suffix>>",
   "Transport" : "amqp"
 }
}

身份管理配置。

{
 "name": "mapping",
 "loader": {
   "name" : "native",
   "entrypoint" : {
     "module.path": "build/modules/identitymap/libidentity_map.so"
   }
 },
 "args": [
   {
     "macAddress": "<<AA:BB:CC:DD:EE:FF>>",
     "deviceId": "<<Azure IoT Hub Device ID>>",
     "deviceKey": "<<Azure IoT Hub Device Key>>"
   }
 ]
}

BLE 打印机模块配置。

{
 "name": "BLE Printer",
 "loader": {
   "name" : "native",
   "entrypoint" : {
     "module.path": "build/samples/ble_gateway/ble_printer/libble_printer.so"
   }
 },
 "args": null
}

BLEC2D 模块配置。

{
 "name": "BLEC2D",
 "loader": {
   "name" : "native",
   "entrypoint" : {
     "module.path": "build/modules/ble/libble_c2d.so"
   }
 },
 "args": null
}

路由模块配置应如下所示:

"links" : [
   {"source" : "*", "sink" : "Logger" },
   {"source" : "SensorTag", "sink" : "mapping" },
   {"source" : "SensorTag", "sink" : "BLE Printer" },
   {"source" : "mapping", "sink" : "IoTHub" },
   {"source" : "IoTHub", "sink" : "mapping" },
   {"source" : "mapping", "sink" : "BLEC2D" },
   {"source" : "BLEC2D", "sink" : "SensorTag"}
]

现在,为了运行它,将 JSON 配置文件路径作为参数传递给 ble_gateway 二进制文件。如果您使用的是 gateway_sample.json,请在您的 Raspberry Pi 上运行以下命令:

./build/samples/ble_gateway/ble_gateway./samples/ble_gateway/src/gateway_sample.json

您可以使用以下命令监控设备到云的消息:

iothub-explorer monitor-events --login "HostName={Your iot hub name}.azure-
devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey={Your IoT Hub key}"

好了!您使用 Azure IoT Edge 实现的第一个 IoT Edge 网关!

我还在探索一些关于 IoT Edge 的其他数据模型,如果您有任何看法,我很乐意向您学习。

© . All rights reserved.