gateway网关可以将现场设备数据收集后,再联通到thingsboard后台控制中心,并自动显示设备信息
这个是网关的结构图
在这里插入图片描述
可以参照官网配置网关

建立网关设备

在这里插入图片描述
一定要勾选上是网关选项

下载运行thingsboard-gateway

我是在虚拟机环境通过docker安装的

docker run -it -v ~/.tb-gateway/logs:/thingsboard_gateway/logs -v ~/.tb-gateway/extensions:/thingsboard_gateway/extensions -v ~/.tb-gateway/config:/thingsboard_gateway/config --name tb-gateway --restart always thingsboard/tb-gateway

在这里插入图片描述
安装完成后,需要配置网关信息

配置tb_gateway.yml

thingsboard:
  host: 192.168.137.131
  port: 1883
  remoteShell: false
  remoteConfiguration: false
  security:
    accessToken: VO9ZKbJAIW9QK8IZbPhq
  qos: 1
storage:
  type: memory
  read_records_count: 100
  max_records_count: 100000
#  type: file
#  data_folder_path: ./data/
#  max_file_count: 10
#  max_read_records_count: 10
#  max_records_per_file: 10000
connectors:
  -
    name: MQTT Broker Connector
    type: mqtt
    configuration: mqtt.json


host是thingsboard的ip地址,这里我设置的是我另外一个虚拟机的IP
port就是thingsboard的MQTT的port端口
accessToken是网关的口令
在这里插入图片描述
因为这里只有一个mqtt设备,所以connectors只有一个

下一步配置mqtt远程信息,但是需要安装mqtt broker,这里我选择使用hivemq

安装hivemq

使用docker进行部署
在这里插入图片描述
然后登陆进hivemq的web控制台
在这里插入图片描述
记录好hivemq的ip地址

配置gateway中的mqtt.json

{
  "broker": {
    "name":"Default Local Broker",
    "host":"192.168.137.129",
    "port":1883,
    "clientId": "ThingsBoard_gateway",
    "security": {
      "type": "basic",
      "username": "admin",
      "password": "hivemq"
    }
  },
  "mapping": [
    {
      "topicFilter": "/sensor/data",
      "converter": {
        "type": "json",
        "deviceNameJsonExpression": "${serialNumber}",
        "deviceTypeJsonExpression": "${sensorType}",
        "timeout": 60000,
        "attributes": [
          {
            "type": "string",
            "key": "model",
            "value": "${sensorModel}"
          },
          {
            "type": "string",
            "key": "${sensorModel}",
            "value": "on"
          }
        ],
        "timeseries": [
          {
            "type": "double",
            "key": "temperature",
            "value": "${temp}"
          },
          {
            "type": "double",
            "key": "humidity",

这里面最主要的是broker这段

 "broker": {
    "name":"Default Local Broker",
    "host":"192.168.137.129",
    "port":1883,
    "clientId": "ThingsBoard_gateway",
    "security": {
      "type": "basic",
      "username": "admin",
      "password": "hivemq"
    }

host就是hivemq的ip地址
port是hivemq的port端口
security是默认的安全配置,官网推荐的是basic

使用mqttbox连接hivemq

在这里插入图片描述
这里的IP地址是hivemq的地址,不要写成thingsboard或者gateway的地址
点击save查看连接状态
在这里插入图片描述
hivemq控制台也能看到这个客户端的连接信息
在这里插入图片描述

发送设备信息到hivemq

通过mqtt.json中设置的信息规则,发送信息
在这里插入图片描述
这里一定要注意发送的Payload中的双引号一定要用英文的。
我因为复制粘贴官网上的信息,一直报错,不能发送到thingsboard中去
通过docker logs查看错误日志,发现下面的错误信息

simplejson.errors.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
""2021-01-26 08:40:49" - ERROR - [tb_utility.py] - tb_utility - 63 - No telemetry and attributes in data: {"deviceName": "SN-005", "deviceType": "Thermometer", "attributes": [], "telemetry": []}"
""2021-01-26 08:40:49" - ERROR - [tb_gateway_service.py] - tb_gateway_service - 322 - Data from MQTT Broker Connector connector is invalid."
""2021-01-26 08:43:59" - ERROR - [json_mqtt_uplink_converter.py] - json_mqtt_uplink_converter - 77 - Error in converter, for config: 
{"type": "json", "deviceNameTopicExpression": "(?<=sensor/)(.*?)(?=/data)", "deviceTypeTopicExpression": "Thermometer", "timeout": 60000, "attributes": [{"type": "string", "key": "model", "value": "${sensorModel}"}], "timeseries": [{"type": "double", "key": "temperature", "value": "${temp}"}, {"type": "double", "key": "humidity", "value": "${hum}"}]}
 and message: 
{“sensorModel”: “T1200”, “temp”: 42, “hum”: 58}
"
""2021-01-26 08:43:59" - ERROR - [json_mqtt_uplink_converter.py] - json_mqtt_uplink_converter - 78 - Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/thingsboard_gateway-2.5.5.2-py3.8.egg/thingsboard_gateway/connectors/mqtt/json_mqtt_uplink_converter.py", line 62, in convert
    value = TBUtility.get_value(datatype_config["value"], data, datatype_config["type"], expression_instead_none=True)
  File "/usr/local/lib/python3.8/site-packages/thingsboard_gateway-2.5.5.2-py3.8.egg/thingsboard_gateway/tb_utility/tb_utility.py", line 124, in get_value
    body = loads(body)
  File "/usr/local/lib/python3.8/site-packages/simplejson-3.17.2-py3.8-linux-x86_64.egg/simplejson/__init__.py", line 525, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.8/site-packages/simplejson-3.17.2-py3.8-linux-x86_64.egg/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/local/lib/python3.8/site-packages/simplejson-3.17.2-py3.8-linux-x86_64.egg/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
""2021-01-26 08:43:59" - ERROR - [tb_utility.py] - tb_utility - 63 - No telemetry and attributes in data: {"deviceName": "SN-006", "deviceType": "Thermometer", "attributes": [], "telemetry": []}"
""2021-01-26 08:43:59" - ERROR - [tb_gateway_service.py] - tb_gateway_service - 322 - Data from MQTT Broker Connector connector is invalid."
[root@localhost config]# 

错误信息中

{“sensorModel”: “T1200”, “temp”: 42, “hum”: 58}

这里的引号形式不对,改正后可以正常发送过去了

查看thingsboard控制台信息

在这里插入图片描述
可以看到我们通过网关将我们配置的设备信息已经显示到thingsboard中去了

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐