欢迎大家加入 thingsboard 二次开发讨论群:121202538
thingsboard 演示地址:https://www.iotschool.com/topics/8
文档参考:
说明:
tb 安装在 centos 系统下,地址为:tb_host_ip;
gateway 安装在乌班图系统下,地址为:tb_gateway_host_ip;
在 tb 中配置一个 device,设置为 gateway
安装 tb-gateway
参考:
在 gateway 机器上部署 mqtt
参考:
sername: "dave" password:"password"
- name: mqtt broker connector type: mqtt configuration: mqtt.json
systemctl restart thingsboard-gateway.service
5. 配置mqtt.json(参考https://thingsboard.io/docs/iot-gateway/config/mqtt/)
# 用户名对应标题3的内容
```java
"security": {
"type": "basic",
"username": "dave",
"password": "password"
}
# connector 的内容定义
{
"topicfilter": "/sensor/data",
"converter": {
"type": "json",
"devicenamejsonexpression": "${serialnumber}",
"devicetypejsonexpression": "${sensortype}",
"timeout": 60000,
"attributes": [
{
"type": "string",
"key": "model",
"value": "${sensormodel}"
}
],
"timeseries": [
{
"type": "double",
"key": "temperature",
"value": "${temp}"
},
{
"type": "double",
"key": "humidity",
"value": "${hum}"
}
]
}
}
# 数据说明:
serialnumber ===》deviceid
sensortype ===> devicetype
sensormodel ===> client attribute
temp,hum ====> 遥感数据
在 tb 中建立名称为 sn-001,type 为 thermometer 的新 device,该设备将用来接受上面的遥感数据。
发送测试数据
执行如下的代码:
//requires node.js and mqtt library installed.
var mqtt = require('mqtt');
const thingsboardhost = "tb_host_ip";
// reads the access token from arguments
// const accesstoken = process.argv[2];
const accesstoken = "gateway_device_token";
const mintemperature = 17.5, maxtemperature = 30, minhumidity = 12, maxhumidity = 90;
// initialization of temperature and humidity data with random values
var data = {
serialnumber: "sn-001", sensortype: "thermometer", sensormodel: "t1000",
temp: mintemperature (maxtemperature - mintemperature) * math.random() ,
hum: minhumidity (maxhumidity - minhumidity) * math.random()
};
// initialization of mqtt client using thingsboard host and device access token
console.log('connecting to: %s using access token: %s', thingsboardhost, accesstoken);
var client = mqtt.connect('mqtt://' thingsboardhost, { username: "dave", password: "password" });
const obj={"serialnumber": "sn-001",
"sensortype": "thermometer", "sensormodel": "t1000", "temp": 42, "hum": 58};
// triggers when client is successfully connected to the thingsboard server
client.on('connect', function () {
console.log('client connected!');
// uploads firmware version as device attribute using 'v1/devices/me/attributes' mqtt topic
client.publish('/sensor/data', json.stringify(obj));
// schedules telemetry data upload once per second
console.log('uploading temperature and humidity data once per second...');
setinterval(publishtelemetry, 1000);
});
// uploads telemetry data using 'v1/devices/me/telemetry' mqtt topic
function publishtelemetry() {
data.temp = gennextvalue(data.temp, mintemperature, maxtemperature);
data.hum = gennextvalue(data.hum, minhumidity, maxhumidity);
client.publish('/sensor/data', json.stringify(data));
}
// generates new random value that is within 3% range from previous value
function gennextvalue(prevvalue, min, max) {
var value = prevvalue ((max - min) * (math.random() - 0.5)) * 0.03;
value = math.max(min, math.min(max, value));
return math.round(value * 10) / 10;
}
//catches ctrl c event
process.on('sigint', function () {
console.log();
console.log('disconnecting...');
client.end();
console.log('exited!');
process.exit(2);
});
//catches uncaught exceptions
process.on('uncaughtexception', function(e) {
console.log('uncaught exception...');
console.log(e.stack);
process.exit(99);
});
8.验证测试结果:
device 已经获得遥感数据:
client 属性已经获得:
感谢:寒碧
来源: