Kaa IoT setup

本页介绍 MikroTik RouterOS 的 Kaa IoT 设置,涵盖 MQTT 和 HTTP 协议支持、通过脚本进行的系统资源监控,以及 Kaa IoT 门户配置步骤(包括创建应用/设备和管理端点令牌)。它还概述了 RouterOS 前提条件,例如互联网访问,以及使用 MQTT 时可选的 iot 软件包安装。

Kaa IoT 设置

MQTT 和 HTTP 是用于传输各种数据的最流行协议之一。这两种协议在各种 IoT 设置中被大量使用,并且它们都受到 RouterOS 支持。

您可能会问,是什么类型的数据?几乎任何东西…… RouterOS 脚本是一个非常强大的工具,可以借助调度帮助您自动化设备。

例如,您可以借助命令 /system/resource/print 查看系统资源信息:

/system/resource/print
                   uptime: 4d1h37m55s
                  version: 7.14.3 (stable)
               build-time: 2024-04-17 12:47:58
         factory-software: 6.45.9
              free-memory: 926.0MiB
             total-memory: 1024.0MiB
                      cpu: ARM
                cpu-count: 4
            cpu-frequency: 533MHz
                 cpu-load: 0%
           free-hdd-space: 88.5MiB
          total-hdd-space: 128.0MiB
  write-sect-since-reboot: 1107
         write-sect-total: 1447413
               bad-blocks: 0%
        architecture-name: arm
               board-name: RB1100AHx4
                 platform: MikroTik

此命令向您显示有用的信息,例如 CPU 使用率、RAM 内存使用率、设备的运行时间和固件版本。另一个命令将打印 GPS 坐标(如果设备内置 GPS 芯片)等等……

这实质上意味着,任何可以"打印"到 RouterOS 终端的内容,都可以通过脚本构造为 JSON 格式的消息,并按配置的间隔发送出去。我们将在指南后面展示更详细的示例。

换言之,您能够将数据从 MikroTik 发送到您选择的任何 MQTT 或 HTTP 服务器。Kaa IoT 就是这样一个服务器。

为什么选择 Kaa IoT?

Kaa IoT 配置

请务必查看 KAA:连接您的第一个设备。

登录门户后,首先按照操作手册中的步骤操作:

a) 在 "Home>Device management>Applications" 下,点击 "Add application" 按钮创建一个应用,为其命名,然后 "Create":

b) 在 "Home>Device management>Devices" 下,点击 "Add device" 按钮创建一个设备,为其命名,然后 "Create":

输入您自己的 "Endpoint token" 或让系统为您自动生成一个。请务必"保存"它(因为您将无法再次访问它)。

剩下要做的就是使用 MQTT 或 HTTP 将我们的路由器连接到服务器,发布一些数据,并自定义我们的第一个仪表盘以便更轻松地将其可视化。

RouterOS 配置

首先,设备应具有互联网访问。请查看我们的首次配置指南。

互联网启动并运行后,如果您计划使用 MQTT 协议,请确保已安装 iot 软件包。您可以从我们的下载页面下的 "Extra packages" 文件(适用于您设备相应的架构)中下载它。解压 "Extra packages" 文件并将 iot 软件包上传到 "Files"(之后重启设备)。如果您只计划使用 HTTP posting,则无需安装 iot 软件包。

在继续之前,请访问 Kaa IoT 的 MQTT 和 HTTP 设备 API 指南。

使用 HTTP 发布数据

要发布一个基本的 JSON 消息:

{"test":"data"}

只需运行以下命令:

/tool/fetch url="https://next.kaaiot.com/kpc/kp1/<app-version>/epmx/<token>/update/keys" http-method=post  http-header-field="Content-Type: application/json" http-data="{\"test\":\"data\"}" output=user mode=https

,其中您应将 <app-version>(可在 "Home>Device management>Devices>Specific device" 选项卡下查看)和 <token>(您在平台上创建设备后生成的)更改为各自的值。

您应能在 "Home>Device management>Devices>Specific device>Overview" 选项卡下看到新的 "Metadata" 值出现,或在 "Home>Device management>Devices>Specific device>Data logs" 选项卡下查看日志。

要收集 /system/resource/print 信息并发布它,我们可以使用脚本。将下面所示脚本的内容复制并粘贴到命令行中:

/system/script
add dont-require-permissions=no name=systeminfo owner=admin policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="#####################\
    ############### System ###################################\r\
    \n:put (\"[*] Gathering system info...\")\r\
    \n:local cpuLoad [/system/resource/get cpu-load];\r\
    \n:local freeMemory [/system/resource/get free-memory];\r\
    \n:local usedMemory ([/system/resource/get total-memory] - \$freeMemory);\r\
    \n:local rosVersion [/system/package/get value-name=version \\\r\
    \n[/system/package/find where name ~ \"^routeros\"]];\r\
    \n:local model [/system/routerboard/get value-name=model];\r\
    \n:local serialNumber [/system/routerboard/get value-name=serial-number];\r\
    \n:local upTime [/system/resource/get uptime];\r\
    \n\r\
    \n#################################### message #####################################\r\
    \n:local message \\\r\
    \n\"{\\\"model\\\":\\\"\$model\\\",\\\r\
    \n\\\"sn\\\":\\\"\$serialNumber\\\",\\\r\
    \n\\\"ros\\\":\\\"\$rosVersion\\\",\\\r\
    \n\\\"cpu\\\":\\\"\$cpuLoad\\\",\\\r\
    \n\\\"umem\\\":\\\"\$usedMemory\\\",\\\r\
    \n\\\"fmem\\\":\\\"\$freeMemory\\\",\\\r\
    \n\\\"uptime\\\":\\\"\$upTime\\\"}\"\r\
    \n\r\
    \n:log info \"\$message\";\r\
    \n:put (\"[*] Total message size: \$[:len \$message] bytes\")\r\
    \n:put (\"[*] Sending message...\")\r\
    \n/tool/fetch url=\"https://next.kaaiot.com/kpc/kp1/<app-version>/epmx/<token>/up\
    date/keys\" http-method=post  http-header-field=\"Content-Type: application/json\" http-data=\
    \"\$message\" output=user mode=https\r\
    \n:put (\"[*] Done\")"

更改 URL 的 <app-version> 和 <token> 值。然后,使用以下命令运行脚本:

/system/script/run systeminfo

JSON 消息将如下所示:

{
  "model": "RB924iR-2nD-BT5&BG77",
  "sn": "XXXXXXX",
  "ros": "7.99",
  "cpu": "7",
  "umem": "45113344",
  "fmem": "21995520",
  "uptime": "4d22:16:08"
}

使用 MQTT 发布数据

为了使用单向 SSL MQTT 场景,请从 "Home>Device management>Credentials" 中点击 "Get root certificate" 按钮获取根证书。更多信息可在此处找到。

将 ca.pem 证书文件上传到 RouterOS 并使用以下命令导入:

/certificate/import file-name=ca.pem passphrase=""

添加新的 MQTT broker:

/iot/mqtt/brokers
add address=mqtt.next.kaaiot.com name=kaaiot port=8883 ssl=yes

连接到 broker 并借助 "print" 命令检查连接是否持续(应显示 "connected=yes"):

/iot/mqtt/connect broker=kaaiot

/iot/mqtt/brokers/print
0 name="kaaiot" address="mqtt.next.kaaiot.com" port=8883 ssl=yes auto-connect=no keep-alive=60 parallel-scripts-limit=off connected=yes

要发布一个基本的 JSON 消息:

{"test":"data"}

只需运行以下命令:

/iot/mqtt/publish broker=kaaiot message="{\"test\":\"data\"}" topic="kp1/<app-version>/epmx/<token>/update/keys/88"

,其中您应将 <app-version>(可在 "Home>Device management>Devices>Specific device" 选项卡下查看)和 <token>(您在平台上创建设备后生成的)更改为各自的值。

您应能在 "Home>Device management>Devices>Specific device>Overview" 选项卡下看到新的 "Metadata" 值出现,或在 "Home>Device management>Devices>Specific device>Data logs" 选项卡下查看日志。

要收集 /system/resource/print 信息并发布它,我们可以使用脚本。将下面所示脚本的内容复制并粘贴到命令行中:

/system/script
add dont-require-permissions=no name=systeminfo owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="#####\
    ############################### System ###################################\r\
    \n:put (\"[*] Gathering system info...\")\r\
    \n:local cpuLoad [/system/resource/get cpu-load];\r\
    \n:local freeMemory [/system/resource/get free-memory];\r\
    \n:local usedMemory ([/system/resource/get total-memory] - \$freeMemory);\r\
    \n:local rosVersion [/system/package/get value-name=version \\\r\
    \n[/system/package/find where name ~ \"^routeros\"]];\r\
    \n:local model [/system/routerboard/get value-name=model];\r\
    \n:local serialNumber [/system/routerboard/get value-name=serial-number];\r\
    \n:local upTime [/system/resource/get uptime];\r\
    \n\r\
    \n#################################### message #####################################\r\
    \n:local message \\\r\
    \n\"{\\\"model\\\":\\\"\$model\\\",\\\r\
    \n\\\"sn\\\":\\\"\$serialNumber\\\",\\\r\
    \n\\\"ros\\\":\\\"\$rosVersion\\\",\\\r\
    \n\\\"cpu\\\":\\\"\$cpuLoad\\\",\\\r\
    \n\\\"umem\\\":\\\"\$usedMemory\\\",\\\r\
    \n\\\"fmem\\\":\\\"\$freeMemory\\\",\\\r\
    \n\\\"uptime\\\":\\\"\$upTime\\\"}\"\r\
    \n\r\
    \n:log info \"\$message\";\r\
    \n:put (\"[*] Total message size: \$[:len \$message] bytes\")\r\
    \n:put (\"[*] Sending message...\")\r\
    \n/iot/mqtt/publish broker=kaaiot message=\$message topic=\"kp1/<app-version>/epmx/<token>/update/keys/88\"\r\
    \n:put (\"[*] Done\")"

更改 topic 的 <app-version> 和 <token> 值。然后,使用以下命令运行脚本:

/system/script/run systeminfo

JSON 消息将如下所示:

{
  "model": "RB924iR-2nD-BT5&BG77",
  "sn": "XXXXXXX",
  "ros": "7.99",
  "cpu": "7",
  "umem": "45113344",
  "fmem": "21995520",
  "uptime": "4d22:16:08"
}

使用仪表盘进行数据可视化

要可视化先前发布的数据,请转到 "Home>Solutions>Your_Solution>Dashboards>Your_Dashboard" 并点击 "Add widget":

选择一个 widget 类型(我们将使用 "Device management"),以及一个预设 widget(我们将使用 "Endpoint metadata")。

"Edit" 该 widget,并在 "Data source>Endpoint ID" 下选择您的 "Endpoint ID"。从这里,您可以进一步自定义仪表盘:

这样,您可以创建自己的脚本,改为收集对您重要的信息,然后只需应用一个调度器以您选择的间隔运行脚本。也许您想从 LTAP 收集 GPS 坐标并使用 "map" widget 将它们可视化?这完全取决于您!