Thingsboard Thingsboard 教程:Gateway 测试 MQTT 的例子

sds · 2020年03月04日 · 381 次阅读

欢迎大家加入 thingsboard 二次开发讨论群:121202538

thingsboard交流QQ群 121202538

thingsboard 演示地址:https://www.iotschool.com/topics/8

文档参考:https://thingsboard.io/docs/iot-gateway/getting-started/
说明:
TB 安装在 centos 系统下,地址为:TB_HOST_IP;
gateway 安装在乌班图系统下,地址为:TB_GATEWAY_HOST_IP;

  1. 在 tb 中配置一个 device,设置为 gateway
    image.png

  2. 安装 TB-gateway
    参考:https://thingsboard.io/docs/iot-gateway/install/deb-installation/

  3. 在 gateway 机器上部署 mqtt
    参考:https://www.vultr.com/docs/how-to-install-mosquitto-mqtt-broker-server-on-ubuntu-16-04

sername "dave"  password"password"
  1. 配置 tb config:
    ```java thingsboard: host: TB_HOST_IP port: 1883 security: accessToken: GATEWAY_DEVICE_TOKEN //从上图中的 gateway 的 device 中获得 storage: type: memory read_records_count: 10 max_records_count: 1000 connectors:

- 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 ====> 遥感数据

发送数据格式如下:

image.png

  1. 在 tb 中建立名称为 SN-001,type 为 Thermometer 的新 device,该设备将用来接受上面的遥感数据。
    image.png

  2. 发送测试数据

执行如下的代码:

//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 已经获得遥感数据:
image.png

client 属性已经获得:
image.png

感谢:寒碧
来源:https://note.youdao.com/ynoteshare1/index.html?id=d53ce0f23a3987a3d4470be333bd652b&type=note

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册