本文将帮助你熟悉有关iot网关中MQTT连接器的配置。在通用配置中启用该种连接器。这个连接器能够连接到外部的MQTT Broker并订阅,从而获取从设备中上报的数据,同时,连接器也可以推送来自ThingsBoard数据到MQTT Broker。

连接器配置:mqtt.json

连接器配置是一个json文件,它包含了MQTT Broker的连接信息、订阅哪些主题、如何处理数据等。

mqtt.json样例

{
  "broker": {
    "name":"Default Local Broker",
    "host":"192.168.1.100",
    "port":1883,
    "security": {
      "type": "basic",
      "username": "user",
      "password": "password"
    }
  },
  "mapping": [
    {
      "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}"
          }
        ]
      }
    },
    {
      "topicFilter": "/sensor/+/data",
      "converter": {
        "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}"
          }
        ]
      }
    },
    {
      "topicFilter": "/custom/sensors/+",
      "converter": {
        "type": "custom",
        "extension": "CustomMqttUplinkConverter",
        "extension-config": {
            "temperatureBytes" : 2,
            "humidityBytes" :  2,
            "batteryLevelBytes" : 1
        }
      }
    }
  ],
  "connectRequests": [
    {
      "topicFilter": "sensor/connect",
      "deviceNameJsonExpression": "${SerialNumber}"
    },
    {
      "topicFilter": "sensor/+/connect",
      "deviceNameTopicExpression": "(?<=sensor\/)(.*?)(?=\/connect)"
    }
  ],
  "disconnectRequests": [
    {
      "topicFilter": "sensor/disconnect",
      "deviceNameJsonExpression": "${SerialNumber}"
    },
    {
      "topicFilter": "sensor/+/disconnect",
      "deviceNameTopicExpression": "(?<=sensor\/)(.*?)(?=\/disconnect)"
    }
  ],
  "attributeUpdates": [
    {
      "deviceNameFilter": "SmartMeter.*",
      "attributeFilter": "uploadFrequency",
      "topicExpression": "sensor/${deviceName}/${attributeKey}",
      "valueExpression": "{\"${attributeKey}\":\"${attributeValue}\"}"
    }
  ],
  "serverSideRpc": [
    {
      "deviceNameFilter": ".*",
      "methodFilter": "echo",
      "requestTopicExpression": "sensor/${deviceName}/request/${methodName}/${requestId}",
      "responseTopicExpression": "sensor/${deviceName}/response/${methodName}/${requestId}",
      "responseTimeout": 10000,
      "valueExpression": "${params}"
    },
    {
      "deviceNameFilter": ".*",
      "methodFilter": "no-reply",
      "requestTopicExpression": "sensor/${deviceName}/request/${methodName}/${requestId}",
      "valueExpression": "${params}"
    }
  ]
}

配置中broker模块

参数默认值描述
nameDefault Broker日志和保存到设备的代理名称
hostlocalhostmqtt broker的主机名或者ip地址 
port1883mqtt broker端口

配置中的security模块

该模块配置提供了mqtt broker客户端授权的访问信息

三种配置:Basic,Anonymous,Certificates

Basic

参数默认值描述
typebasic认证类型
usernameusername认证中的用户名
passwordpassword认证中的密码

配置如下所示:

    "security": {
      "type": "basic",
      "username": "username",
      "password": "password"
    }

Anonymous

参数默认值描述
typeanonymous认证类型

配置如下所示:

    "security": {
      "type": "anonymous"
    }

Certificates

参数默认值描述
caCert/etc/thingsboard-gateway/ca.pemca文件路径
privateKey/etc/thingsboard-gateway/privateKey.pem私钥文件路径
cert/etc/thingsboard-gateway/certificate.pem证书文件路径

配置代码如下:

  "security":{
    "caCert": "/etc/thingsboard-gateway/ca.pem",
    "privateKey": "/etc/thingsboard-gateway/privateKey.pem",
    "cert": "/etc/thingsboard-gateway/certificate.pem"
  }

配置文件中的mapping模块

该配置包含一组主题数组,网关将会订阅这些主题并处理设备上报的数据

参数默认值描述
topicFilter/sensor/data订阅的主题地址

topicFilter支持特殊字符,其中包括:‘#’ 和 ‘+’,这样就可以实现订阅多个主题

样例名称主题主题过滤报文内容备注
Example 1/sensor/data/sensor/data{“serialNumber”: “SN-001”, “sensorType”: “Thermometer”, “sensorModel”: “T1000”, “temp”: 42, “hum”: 58}设备名称是报文的一部分
Example 2/sensor/SN-001/data/sensor/+/data{ “sensorType”: “Thermometer”, “sensorModel”: “T1000”, “temp”: 42, “hum”: 58}设备名称是主题的一部分

配置中的转换器模块

mqtt转换器类型: json和custom

json

参数默认值描述
typejson向连接器提供默认转换器用于处理设备上报的数据
deviceNameJsonExpression${serialNumber}简单的json表达式,用于获取消息体中的设备名称
deviceTypeJsonExpression${sensorType}简单的json表达式,用于获取消息中的设备类型
timeout60000触发“设备断开”的超时时间
attributes包含上报消息的参数,这些参数将被解释为设备的属性
...typestring当前属性传入的数据类型
...keymodel属性名,这个将发送到ThingsBoard平台
...value${sensorModel}简单的json表达式,用于获取消息中的值
timeseries包含上报数据的参数,这些参数将被解释为设备的遥测参数
...typedouble当前遥测参数类型
...keytemperature遥测参数名称
...value${temp}简单的json表达式,用于获取消息中的值

备注:实际attributes和telemetry中的参数可能与上述参数不同,但具有相同的结构

Example 1 对应的代码如下:

    {
      "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}"
          }
        ]
      }
    }

Example 2 对应的代码:

    {
      "topicFilter": "/sensor/+/data",
      "converter": {
        "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}"
          }
        ]
      }
    }

custom

自定义转换器是为某些设备编写的转换器,如下所示:

参数默认值描述
typecustom向连接器提供信息,自定义转换器用于处理上报的数据
extensionCustomMqttUplinkConverter自定义转换器类的名称
extension-config
temperatureBytes2遥测数据:温度
humidityBytes2遥测数据:湿度
batteryLevelBytes1遥测数据:电池

配置代码如下:

      "converter": {
        "type": "custom",
        "extension": "CustomMqttUplinkConverter",
        "extension-config": {
            "temperatureBytes" : 2,
            "humidityBytes" :  2,
            "batteryLevelBytes" : 1
        }
      }

配置中的“connectRequests”模块

ThingsBoard允许向设备发送RPC命令和有关设备属性更新的通知。但是为了发生这些指令,平台需要知道目标设备是否已连接和它使用的什么网关或者会话连接设备。如果您的设备不断地发送遥测数据,ThingsBoard已经知道怎么推送通知了。如果您的设备刚刚连接到MQTT broker冰鞋等待指令或者更新,您需要向网关发送消息并通知设备已连接到mqtt broker。

1. 设备名称在消息体中

参数默认值描述
topicFiltersensors/connectbroker发送关于新连接设备的信息
deviceNameJsonExpression${SerialNumber}json表达式,获取设备名称

2. 设备名称在主题地址中

参数默认值描述
topicFiltersensors/+/connectbrocker中topic 地址
deviceNameJsonExpression(?<=sensor\/)(.*?)(?=\/connect)正则表达式,获取设备名称

配置代码如下所示:

  "connectRequests": [
    {
      "topicFilter": "sensors/connect",
      "deviceNameJsonExpression": "${$.SerialNumber}"
    },
    {
      "topicFilter": "sensor/+/connect",
      "deviceNameTopicExpression": "(?<=sensor\/)(.*?)(?=\/connect)"
    }
  ]

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐