Guest User

Untitled

a guest
Aug 16th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. import sys
  2. sys.path.insert(0, "..")
  3. import time
  4. import datetime
  5. import subprocess
  6. import logging
  7.  
  8. from opcua import Server
  9. import TSL2561
  10. import MySQLdb
  11.  
  12.  
  13. ##### Sensor Data of TSL2561 Parameters ######################
  14. tsl =TSL2561.TSL2561()
  15. luminosity = tsl.read_raw_luminosity()
  16. lux = tsl.read_lux()
  17. ################################################
  18.  
  19. db = MySQLdb.connect(host ='192.168.1.15',port='3306',user='root',password='admin',database='OPCUA')
  20. cursor = db.cursor()
  21. #192.168.1.15 is de IP Address of MySQL Server
  22.  
  23.  
  24. if __name__ == "__main__":
  25. logging.basicConfig(level = logging.WARNING)
  26.  
  27.  
  28.  
  29. # setup our server
  30. server = Server()
  31. server.set_endpoint("opc.tcp://192.168.1.11:4840/freeopcua/server/")
  32.  
  33. # setup our own namespace, not really necessary but should as spec
  34. uri = "http://examples.freeopcua.github.io"
  35. idx = server.register_namespace(uri)
  36.  
  37. # get Objects node, this is where we should put our nodes
  38. objects = server.get_objects_node()
  39.  
  40. # populating our address space
  41. ########################### Object ######################################
  42. sensordata = objects.add_object(idx, "Sensor TSL2561")
  43.  
  44. ################# Variables of Object ###################################
  45. luminosity_var_node = sensordata.add_variable(idx, "Luminosity", luminosity)
  46. lux_var_node = sensordata.add_variable(idx, "Lux", lux)
  47. ##########################################################################
  48.  
  49.  
  50. luminosity_var_node.set_writable() # Set MyVariable to be writable by clients
  51. lux_var_node.set_writable()
  52.  
  53. # starting server!
  54. server.start()
  55.  
  56. try:
  57.  
  58. while True:
  59.  
  60. luminosity_var_node.set_value(tsl.read_raw_luminosity())
  61. time.sleep(2)
  62. lux_var_node.set_value(tsl.read_lux())
  63. time.sleep(2)
  64. sql = "INSERT INTO OPCUA.OPCUA_SERVER_RASP3(Lux, Luminosity) values (lux, luminosity)"
  65. #database is OPCUA and the table is OPCUA_SERVER_RASP3
  66. cursor.execute(sql)
  67. db.commit()
  68.  
  69. finally:
  70. #close connection, remove subcsriptions, etc
  71. server.stop()
  72. db.close()
Add Comment
Please, Sign In to add comment