入门

ThingsBoard平台入门。

入门

介绍

本教程演示的ThingsBoard的基本用法,你将从中学到如下功能:

  • 创建资产和设备;
  • 定义资产和设备间的关系;
  • 发送设备数据至ThingsBoard;
  • 创建实时仪表板;
  • 定义阀值并报警;
  • 将警报通过邮件发送.

我们将展示如何监视物体不同部分的温度,如何在温度超过特定阈值时发出警报以及如何可视化收集的数据和警报。

视频教程

我们建议您观看以下视频教程。为了方便起见,下面列出了本教程中使用的所有资源。

 

教程资源

在线实例和安装指南

如果你无法运行实例请通过在线实例 或者 安装指南 进行学习。

如果你安装在本地计算机上并使用 –loadDemo 进行初始化则使用默认的用户名及密码,你可以通过此连接进行查看。

发送设备数据

我们使用HTTP,MQTT或CoAP协议从你的本地PC发送数据。请查看设备连接指南以获取所有的连接解决方​​案和选项以及硬件示例,以了解如何将各种硬件平台连接到ThingsBoard

客户端安装

使用如下命令安装 HTTP (cURL), MQTT (Mosquitto 或者 MQTT.js) 或者 CoAP (CoAP.js) 。

resources/curl-win.sh
NOTE: Starting Windows 10 b17063, cURL is available by default.
More info here: https://blogs.msdn.microsoft.com/commandline/2018/01/18/tar-and-curl-come-to-windows/
If you are using older version of Windows OS, you may find official installation guides here: https://curl.haxx.se/
resources/curl-macos.sh
brew install curl
resources/curl-ubuntu.sh
sudo apt-get install curl
resources/node-mqtt.sh
# Assuming you have Node.js and NPM installed on your Windows/Linux/MacOS machine
npm install mqtt -g
resources/mosquitto-ubuntu.sh
sudo apt-get install mosquitto-clients
resources/mosquitto-macos.sh
brew install mosquitto-clients
resources/node-coap.sh
sudo npm install coap-cli -g

视频教程中使用cURL

如果你已经安装cURL工具则此命令适用于Windows, Ubuntu 和 macOS。

# Please replace $HOST_NAME and $ACCESS_TOKEN with corresponding values.
curl -v -X POST -d "{\"temperature\": 25}" $HOST_NAME/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"

# For example, $HOST_NAME in case of live demo server:
curl -v -X POST -d "{\"temperature\": 25}" https://demo.thingsboard.io/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"

# For example, $HOST_NAME in case of local installation:
curl -v -X POST -d "{\"temperature\": 25}" http://localhost:8080/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"

生成脚本

var msg = { temperature: +(Math.random()*5 + 25).toFixed(1)};
var metadata = {};
var msgType = "POST_TELEMETRY_REQUEST";

return { msg: msg, metadata: metadata, msgType: msgType };

规则引擎指南

规则引擎概述 - 了解规则引擎的基础知识和架构。

规则引擎指南 - 解如何使用ThingsBoard规则引擎。

邮件设置

使用本指南来配置SendGrid或使用任何其他可用的SMTP服务器。

其他测试数据文件

创建一个文件夹 来存储本教程的所有必需文件。下载到此文件夹或创建以下数据文件:

请注意,此文件中的数据基本上是键值格式。您可以使用自己的键和值。有关更多详细信息,请参见MQTTCoAPHTTP协议参考。

resources/attributes-data.json
{"firmware_version":"1.0.1", "serial_number":"SN-001"}
resources/telemetry-data.json
{"temperature":21, "humidity":55.0, "active": false}

使用MQTT,CoAP或HTTP推送数据

根据使用的客户端下载对应的文件至文件夹中

如果您使用的是Shell脚本(* .sh),请确保该脚本是可执行的:

chmod +x *.sh

在执行脚本之前,请不要忘记执行以下操作:

  • 替换 $ACCESS_TOKEN设备凭证
  • 替换 $THINGSBOARD_HOST127.0.0.1 (本地电脑) or demo.thingsboard.io (在线实例)。

最后,执行相应的* .sh或* .bat脚本将数据推送到服务器。

以下是包含所提供脚本内容的选项卡。

resources/mqtt-js.sh
#!/bin/sh

# Set ThingsBoard host to "demo.thingsboard.io" or "localhost"
export THINGSBOARD_HOST=demo.thingsboard.io

# Replace YOUR_ACCESS_TOKEN with one from Device details panel.
export ACCESS_TOKEN=YOUR_ACCESS_TOKEN

# Read serial number and firmware version attributes
ATTRIBUTES=$( cat attributes-data.json )
export ATTRIBUTES

# Read timeseries data as an object without timestamp (server-side timestamp will be used)
TELEMETRY=$( cat telemetry-data.json )
export TELEMETRY

# publish attributes and telemetry data via mqtt client
node publish.js
resources/mqtt-js.bat
@echo off

REM Set ThingsBoard host to "demo.thingsboard.io" or "localhost"
set THINGSBOARD_HOST=demo.thingsboard.io

REM Replace YOUR_ACCESS_TOKEN with one from Device details panel.
set ACCESS_TOKEN=YOUR_ACCESS_TOKEN

REM Read serial number and firmware version attributes
set /p ATTRIBUTES=<attributes-data.json

REM Read timeseries data as an object without timestamp (server-side timestamp will be used)
set /p TELEMETRY=<telemetry-data.json

REM publish attributes and telemetry data via mqtt client
node publish.js
resources/publish.js
var mqtt = require('mqtt');

console.log('Connecting to: %s using access token: %s', process.env.THINGSBOARD_HOST, process.env.ACCESS_TOKEN);

var client  = mqtt.connect('mqtt://'+ process.env.THINGSBOARD_HOST,{
    username: process.env.ACCESS_TOKEN
});

client.on('connect', function () {
    console.log('Client connected!');
    client.publish('v1/devices/me/attributes', process.env.ATTRIBUTES);
    console.log('Attributes published!');
    client.publish('v1/devices/me/telemetry', process.env.TELEMETRY);
    console.log('Telemetry published!');
    client.end();
});
resources/mosquitto.sh
#!/bin/sh

# Set ThingsBoard host to "demo.thingsboard.io" or "localhost"
THINGSBOARD_HOST="demo.thingsboard.io"
# Replace YOUR_ACCESS_TOKEN with one from Device details panel.
ACCESS_TOKEN="YOUR_ACCESS_TOKEN"
# Publish serial number and firmware version attributes
mosquitto_pub -d -h "$THINGSBOARD_HOST" -t "v1/devices/me/attributes" -u "$ACCESS_TOKEN" -f "attributes-data.json"
# Publish timeseries data as an object without timestamp (server-side timestamp will be used)
mosquitto_pub -d -h "$THINGSBOARD_HOST" -t "v1/devices/me/telemetry" -u "$ACCESS_TOKEN" -f "telemetry-data.json"
resources/coap-js.sh
# Publish serial number and firmware version attributes
cat attributes-data.json | coap post coap://$THINGSBOARD_HOST/api/v1/$ACCESS_TOKEN/attributes
# Publish timeseries data as an object without timestamp (server-side timestamp will be used)
cat telemetry-data.json | coap post coap://$THINGSBOARD_HOST/api/v1/$ACCESS_TOKEN/telemetry
resources/curl.sh
# Publish serial number and firmware version attributes
# replace $THINGSBOARD_PORT with 8080 (in case of local installation) or 80 (in case of live-demo).
curl -v -X POST -d @attributes-data.json http://$THINGSBOARD_HOST:$THINGSBOARD_PORT/api/v1/$ACCESS_TOKEN/attributes --header "Content-Type:application/json"
# Publish timeseries data as an object without timestamp (server-side timestamp will be used)
# replace $THINGSBOARD_PORT with 8080 (in case of local installation) or 80 (in case of live-demo).
curl -v -X POST -d @telemetry-data.json http://$THINGSBOARD_HOST:$THINGSBOARD_PORT/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"

ThingsBoard社区版教程

下一步

  • 安装指南 - 了解如何在各种操作系统上安装ThingsBoard。

  • 设备连接 - 了解如何根据您的连接方式或解决方案连接设备。

  • 数据看板 - 这些指南包含有关如何配置复杂的ThingsBoard仪表板的说明。

  • 数据处理 - 了解如何使用ThingsBoard规则引擎。

  • 数据分析 - 了解如何使用规则引擎执行基本的分析任务。

  • 硬件样品 - 了解如何将各种硬件平台连接到ThingsBoard。

  • 高级功能 - 了解高级ThingsBoard功能。

  • 开发指南 - 了解ThingsBoard中的贡献和开发。



意见

通过在 github 上关注ThingsBoard来帮助我们推广它。如果您对此样本有任何疑问,请将其发布在 论坛