python_OPC(2)

  • Post author:
  • Post category:python




python_OPCUA+influxdb+docker部署

一、部署influxdb

#1:安装influxdb就不说了,直接pull
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
influxdb                latest              95750833bc56        8 weeks ago         304MB


#2:接下看来就是创建容器
2.1)docker run -d -p 8086:8086 --name influxdb -v vfluxdb:/etc/influxdb -e TZ=Asia/Shanghai 95
#当然在上一步之前别忘了创建volume 
#目的是映射配置db选项
docker volume create vinfluxdb 

二、配置grafana

#这个不是必须的,就要是这东西可以可视化,方便配置和数据可视化,同样,我也弄了,这是option
grafana/grafana         latest              b43aa01dd9f7        2 weeks ago         180MB

三、存储数据到时序数据库influxdb 中

#同样没有封装,仅仅测试
from influxdb import InfluxDBClient
import time
from opcua import Server
from random import randint
import threading



def connect_db(_temp,_time):

    #connect influxdb
    client = InfluxDBClient(host='192.168.20.79',port='8086',username='',password='',database='testDB')

    #data
    w_json = [{
        "measurement": 'luzi_test',
        "tags": {
            'id': '2',
            'n': '2'
            },
        "fields": {
            'TagName': "Temperature",
            'value': _temp
            }
        }]

    
    client.write_points(w_json,time_precision='ms',database='testDB',retention_policy='',tags=None,batch_size=None,protocol='json',consistency=None)
    
    result = client.query('select value from luzi_test order by time desc limit 1;')

    print("Result is :{0}".format(result))




if __name__=='__main__':

    server = Server()

    url = "opc.tcp://192.168.20.79:48401"
    now_time = int(time.time()*1000)

    server.set_endpoint(url)

    name = "OPCUA_SERVER_DEMO"

    addspace = server.register_namespace(name)

    node = server.get_objects_node()
    Param = node.add_object(addspace,"test_Server")

    Temp = Param.add_variable(addspace,"Temperature",0)

    Temp.set_writable()

    server.start()
    print("server started{}".format(url))

    while True:
        
        Temperature = randint(1,100)

        Temp.set_value(Temperature)
        #
        # print("------------------------------------------------------")
        # print("Temperature:{0}".format(Temperature))
        # print("------------------------------------------------------")
        #
        time.sleep(1)
        threading.Thread(target=connect_db,args=(Temperature,now_time),name='insert_data').start()


四、看一下插入数据情况

docker exec -it influxdb bash
--------------------------------
root@83f3fdae956b:/# influxd

 8888888           .d888 888                   8888888b.  888888b.
   888            d88P"  888                   888  "Y88b 888  "88b
   888            888    888                   888    888 888  .88P
   888   88888b.  888888 888 888  888 888  888 888    888 8888888K.
   888   888 "88b 888    888 888  888  Y8bd8P' 888    888 888  "Y88b
   888   888  888 888    888 888  888   X88K   888    888 888    888
   888   888  888 888    888 Y88b 888 .d8""8b. 888  .d88P 888   d88P
 8888888 888  888 888    888  "Y88888 888  888 8888888P"  8888888P"

2020-08-06T02:44:12.528250Z	info	InfluxDB starting	{"log_id": "0OSQJvS0000", "version": "1.8.0", "branch": "1.8", "commit": "781490de48220d7695a05c29e5a36f550a4568f5"}
2020-08-06T02:44:12.528279Z	info	Go runtime	{"log_id": "0OSQJvS0000", "version": "go1.13.8", "maxprocs": 4}
run: open server: listen: listen tcp 127.0.0.1:8088: bind: address already in use
root@83f3fdae956b:/# influx  
Connected to http://localhost:8086 version 1.8.0
InfluxDB shell version: 1.8.0
> show databases
name: databases
name
----
_internal
mydb
testDB
> use testDB
Using database testDB
> select * from luzi_test limit 10
name: luzi_test
time                TagName     id n value
----                -------     -- - -----
1257894000000000000 Temperature 2  2 
1595324252498000000 Temperature 2  2 37
1595324288577000000 Temperature 2  2 36
1595324853904000000 Temperature 2  2 36
1595325122528000000 Temperature 2  2 58
1595325731865000000 Temperature 2  2 83
1595380348004000000 Temperature 2  2 1
1595380486317000000 Temperature 2  2 80
1595380501864000000 Temperature 2  2 68
1595381919200000000 Temperature 2  2 83
> 

五、可视化grafana就没什么可说的,当然你也可以用nodered,接下来因为需要照顾老的plc,因为da不是跨平台,接下来整合opcua&opcda连接的整合

python-snap7



版权声明:本文为weixin_37966295原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。