- 介绍
- !/bin/sh
- Set ThingsBoard host to “demo.thingsboard.io” or “localhost”
- Replace YOUR_ACCESS_TOKEN with one from Device details panel.
- Read serial number and firmware version attributes
- Read timeseries data as an object without timestamp (server-side timestamp will be used)
- publish attributes and telemetry data via mqtt client
介绍
本教程的目的是演示最流行的ThingsBoard功能的基本用法。你将学到如何:
- 在系统中预配资产和设备;
- 定义资产和设备之间的关系;
- 将数据从设备推送到ThingsBoard;
- 建立实时的最终用户仪表板;
- 定义阈值并触发警报;
- 通过电子邮件推送有关新警报的通知。 本教程基于一个流行的设施监控用例。我们将展示如何监视建筑物不同部分的温度,如何在温度超过特定阈值时发出警报以及如何可视化收集的数据和警报。
影片教学
我们建议您阅读以下视频教程。为了方便起见,下面列出了本教程中使用的所有资源。
教程资源
Live Demo和安装指南
如果您无权访问正在运行的ThingsBoard实例,请使用Live Demo或 安装 修复此问题。
如果您决定安装自己的ThingsBoard服务器并加载了演示数据(根据安装指南,为“ –loadDemo”选项),则默认帐户(登录名/密码)和设备凭据的列表位于此处。
从设备推送数据
为了简化本指南,我们将使用HTTP,MQTT或CoAP协议从您的本地PC推送数据。请查看连接设备指南以获取所有可用的连接解决方案和选项以及硬件示例,以了解如何将各种硬件平台连接到ThingsBoard。
客户端库安装
使用以下命令安装首选的HTTP(cURL),MQTT(Mosquitto或MQTT.js)或CoAP(CoAP.js)客户端。
cURL(Windows)
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/
cURL(macoS)
brew install curl
cURL(Ubuntu)
sudo apt-get install curl
MQTT.js
# Assuming you have Node.js and NPM installed on your Windows/Linux/MacOS machine
npm install mqtt -g
Mosquitto(Ubuntu)
sudo apt-get install mosquitto-clients
Mosquitto(macOS)
brew install mosquitto-clients
CoAP.js
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服务器。
其他样本数据文件
创建一些文件夹来存储本教程的所有必需文件。下载到此文件夹或创建以下数据文件:
- attributes-data.json-包含两个设备属性值:固件版本和序列号。
- telemetry-data.json-包含三个时间序列值:温度,湿度和活动标记。 请注意,此文件中的数据基本上是键值格式。您可以使用自己的键和值。有关更多详细信息,请参见MQTT,CoAP 或HTTP协议参考。
attributes-data.json
{"firmware_version":"1.0.1", "serial_number":"SN-001"}
telemetry-data.json
{"temperature":21, "humidity":55.0, "active": false}
使用MQTT,CoAP或HTTP推送数据
根据首选客户端将以下文件下载到以前创建的文件夹:
- MQTT.js(MQTT)
- mqtt-js.sh(Ubuntu和MacOS)或 mqtt-js.bat(Windows)
- publish.js
- Mosquitto(MQTT)
- CoAP.js(CoAP)
- cURL(HTTP)
- curl.sh
如果您使用的是Shell脚本(* .sh),请确保该脚本是可执行的:
chmod +x *.sh
- curl.sh
如果您使用的是Shell脚本(* .sh),请确保该脚本是可执行的:
在执行脚本之前,不要忘记:
- 将 $ACCESS_TOKEN 替换为 设备凭据窗口中的一个。
- 将 $THINGSBOARD_HOST 替换为 127.0.0.1(在本地安装的情况下)或 demo.thingsboard.io(在实时演示的情况下)。
最后,执行相应的 .sh 或 .bat脚本将数据推送到服务器。
以下是包含所提供脚本内容的选项卡。
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
* MQTT.js(Windows)
> resources/mqtt-js.bat
``` 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
- publish.js
resources/publish.js ``` 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(); });
* Mosquitto(MQTT)
> resources/mosquitto.sh
``` 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"
- CoAP.js(CoAP)
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
- cURL(HTTP)
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规则引擎。
IoT数据分析 -了解如何使用规则引擎执行基本分析任务。
硬件示例 -了解如何将各种硬件平台连接到ThingsBoard。
高级功能 -了解高级ThingsBoard功能。
贡献和发展 -了解ThingsBoard中的贡献和发展。
您的反馈
不要犹豫,在github上加注ThingsBoard 来帮助我们传播这个词。如果您对此样本有疑问,请将其发布在论坛上。