ThingsBoard通过tb-gateway接入odbc数据

目录

    • 1.版本信息
    • 2.TB-Gateway的安装
    • 3.TB-Gateway的配置
    • 4.ODBC的配置(重要)
        • 1)安装 unixodbc、pyodbc
        • 2)安装mysql驱动
        • 3)配置mysql驱动信息
        • 4)配置odbc.json文件
    • 5.效果展示

1.版本信息

ThingsBoard端版本信息

系统:Ubuntu 18.04
ThingsBoard:ThingsBoard-3.0.1
PostgreSQL:12.3

TB-Gateway端版本信息

系统:Ubuntu 18.04
TB-Gateway:ThingsBoard IoT gateway version 2.4.0
MySql:Ver 14.14 Distrib 5.7.30

效果图
ThingsBoard通过tb-gateway接入odbc数据_第1张图片

2.TB-Gateway的安装

安装步骤参考官网:https://thingsboard.io/docs/iot-gateway/install/deb-installation/

主要步骤:

#下载安装包
wget https://github.com/thingsboard/thingsboard-gateway/releases/latest/download/python3-thingsboard-gateway.deb

#安装启动
sudo apt install ./python3-thingsboard-gateway.deb -y

#查看状态
systemctl status thingsboard-gateway

装好之后启动会有些错误,这个是还没有配置的原因,后面再进行配置

3.TB-Gateway的配置

配置步骤参考官网:https://thingsboard.io/docs/iot-gateway/configuration/

安装的tb-gateway一般在 /etc/thingsboard-gateway 下,下面对 /etc/thingsboard-gateway/conf/tb_gateway 进行配置:

thingsboard:
  host: 127.0.0.1       #thingsboard的地址
  port: 1883			#thingsboard mqtt的服务端口,默认是1883
  remoteConfiguration: false      #远程配置关闭
  security:
    accessToken: LnV3ZmPP6lq1cQSq4gkf	#token,这个需要在thingsboard新建一个网关设备,复制其令牌
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:							#配置了2个odbc的Connector,如果你是一个也可以只配置一个					
  -
    name: ODBC Connector
    type: odbc
    configuration: odbc.json  

  -
    name: ODBC Connector2
    type: odbc
    configuration: odbc_gzy.json   

4.ODBC的配置(重要)

参考官网:https://thingsboard.io/docs/iot-gateway/config/odbc/

odbc 的先决条件:

  • 1.安装Linux的ODBC包;
  • 2.安装ODBC驱动程序;
  • 3.在Unix系统上将驱动程序信息(名称,库路径等)添加到ODBC配置文件odbcinst.ini;

1)安装 unixodbc、pyodbc

tb-gateway使用pyodbc来操作odbc,pyodbc是ODBC的一个python封装,它允许任何平台上的python具有使用ODBC API的能力

#如果装过python的话可以不执行安装python的命令了
sudo apt install python3-pip   
sudo apt install python3-dev
sudo apt install unixodbc-dev
pip3 install --user pyodbc

2)安装mysql驱动

apt-get install libmyodbc

可能报错:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package libmyodbc is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package 'libmyodbc' has no installation candidate

那么我们可以手动下载安装包
下载地址:https://dev.mysql.com/downloads/connector/odbc/
在这里插入图片描述
执行命令:

dpkg -i  mysql-connector-odbc_8.0.20-1ubuntu18.04_amd64.deb

3)配置mysql驱动信息

修改文件 /etc/odbcinst.ini

[MySQL ODBC 8.0 Unicode Driver]
Driver=/usr/lib/x86_64-linux-gnu/odbc/libmyodbc8w.so
UsageCount=1

[MySQL ODBC 8.0 ANSI Driver]
Driver=/usr/lib/x86_64-linux-gnu/odbc/libmyodbc8a.so
UsageCount=1

记住上面的名字:MySQL ODBC 8.0 Unicode Driver

4)配置odbc.json文件

{
  "connection": {
    "str": "Driver={MySQL ODBC 8.0 Unicode Driver};Server=localhost;Port=3306;Database=xx;Uid=root;Pwd=xx;",
    "attributes": {
      "autocommit": true,
      "timeout": 0
    },
    "encoding": "utf-8",
    "decoding": {
      "char": "utf-8",
      "wchar": "utf-8",
      "metadata": "utf-16le"
    },
    "reconnect": true,
    "reconnectPeriod": 60
  },
  "pyodbc": {
    "pooling": false
  },
  "polling": {
    "query": "SELECT u_id, v_r, v_avg, v_max, v_min, v_std, temperature_r, temperature_avg, temperature_max, temperature_min, temperature_std, poa_r, poa_avg, poa_max, poa_min, poa_std, poa_sum, poa_interval_sum FROM cr1000data.`1mindatas` WHERE u_id > ? ORDER BY u_id ASC LIMIT 1",   
    "period": 20,
    "iterator": {
      "column": "u_id",
      "query": "SELECT Max(u_id) FROM cr1000data.1mindatas",
      "persistent": false
    }
  },
  "mapping": {
    "device": {
      "type": "odbc",
      "name": "'gateway_ecs'"
    },
    "sendDataOnlyOnChange": false,
    "timeseries": "*"
  },
  "serverSideRpc": {
    "enableUnknownRpc": false,
    "overrideRpcConfig": false,
    "methods": [
    ]
  }
}

其中:

  • 1.mysql的连接字符串str根据自己的来设定,Driver={MySQL ODBC 8.0 Unicode Driver} 这个要和上文odbcinst.ini中的一致;
  • 2.polling中的query一般都需要加上迭代器,限定每次的读取数量,上面的设定是每次启动时迭代器的初值是最大的u_id,每次读取1条数据库中的数据。
  • 3.period指的是查询的间隔,此处是20秒一次;
  • 4.mapping中的信息填thingsboard中的新建的设备信息。

我的mysql表的字段:
ThingsBoard通过tb-gateway接入odbc数据_第2张图片

5.效果展示

开启tb-gateway:

service thingsboard-gateway start #开启
service thingsboard-gateway stop  #关闭
service thingsboard-gateway status #状态

成功后thingsboard上会有最新的遥测值更新:
ThingsBoard通过tb-gateway接入odbc数据_第3张图片
添加仪表盘,展示数据:
ThingsBoard通过tb-gateway接入odbc数据_第4张图片

你可能感兴趣的:(ThingsBoard)