Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- sys.path.insert(0, "..")
- import time
- import datetime
- import subprocess
- import logging
- from opcua import Server
- import TSL2561
- import MySQLdb
- ##### Sensor Data of TSL2561 Parameters ######################
- tsl =TSL2561.TSL2561()
- luminosity = tsl.read_raw_luminosity()
- lux = tsl.read_lux()
- ################################################
- db = MySQLdb.connect(host ='192.168.1.15',port='3306',user='root',password='admin',database='OPCUA')
- cursor = db.cursor()
- #192.168.1.15 is de IP Address of MySQL Server
- if __name__ == "__main__":
- logging.basicConfig(level = logging.WARNING)
- # setup our server
- server = Server()
- server.set_endpoint("opc.tcp://192.168.1.11:4840/freeopcua/server/")
- # setup our own namespace, not really necessary but should as spec
- uri = "http://examples.freeopcua.github.io"
- idx = server.register_namespace(uri)
- # get Objects node, this is where we should put our nodes
- objects = server.get_objects_node()
- # populating our address space
- ########################### Object ######################################
- sensordata = objects.add_object(idx, "Sensor TSL2561")
- ################# Variables of Object ###################################
- luminosity_var_node = sensordata.add_variable(idx, "Luminosity", luminosity)
- lux_var_node = sensordata.add_variable(idx, "Lux", lux)
- ##########################################################################
- luminosity_var_node.set_writable() # Set MyVariable to be writable by clients
- lux_var_node.set_writable()
- # starting server!
- server.start()
- try:
- while True:
- luminosity_var_node.set_value(tsl.read_raw_luminosity())
- time.sleep(2)
- lux_var_node.set_value(tsl.read_lux())
- time.sleep(2)
- sql = "INSERT INTO OPCUA.OPCUA_SERVER_RASP3(Lux, Luminosity) values (lux, luminosity)"
- #database is OPCUA and the table is OPCUA_SERVER_RASP3
- cursor.execute(sql)
- db.commit()
- finally:
- #close connection, remove subcsriptions, etc
- server.stop()
- db.close()
Add Comment
Please, Sign In to add comment