Guest User

Untitled

a guest
Mar 24th, 2019
59
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import serial
  2. import json
  3. import opcua
  4. from opcua import Server
  5.  
  6. ser = serial.Serial("COM6", 115200)
  7.  
  8. server = Server()
  9. server.set_endpoint("opc.tcp://localhost:4840")
  10. server.start()
  11.  
  12. addspace = server.register_namespace("ESP32")
  13. node = server.get_objects_node()
  14. var = node.add_object(addspace, "Variaveis")
  15.  
  16. def handleRequest(data):
  17. tipo = data['tipo']
  18. if(tipo == 'declarar'):
  19. nome = data['nome']
  20. nodeid = "ns=2;s=" + nome
  21. print("declarando variavel:", nome)
  22. var.add_variable(nodeid, nome, 0).set_writable()
  23. if(tipo == 'atualizar'):
  24. variavel = data['nome']
  25. valor = data['valor']
  26. nodeid = "ns=2;s=" + variavel
  27. print("atualizando " + variavel + " para", valor)
  28. server.get_node(nodeid).set_value(valor)
  29.  
  30.  
  31. while(1):
  32. if(ser.in_waiting > 0):
  33. leitura = ser.read_until(b'}').decode('ascii')
  34. leitura = leitura[leitura.rfind("{"):]
  35. data = json.loads(leitura)
  36. handleRequest(data)
RAW Paste Data