Multi-SNICS_Interlock/Listen2Arduino.py

70 lines
2.4 KiB
Python
Raw Normal View History

#!/usr/bin/python
import serial
import os
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
ser.reset_input_buffer()
outFile = "/home/pi02/data.txt"
cmd='curl -s -XPOST "http://fsunuc.physics.fsu.edu:8086/write?db=interlock" --data-binary @/home/pi02/data.txt'
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
components = line.split(';')
format_value = {}
for component in components:
#print(component)
parts = component.strip().split(': ')
if len(parts) == 2:
key, valueUnit = parts
part2 = valueUnit.split(' ')
if len(part2) == 2:
value, unit = part2
format_value[key] = value
else:
format_value[key] = valueUnit
#print(line)
#for key, value in format_value.items():
# print(f"{key} value={value}")
with open(outFile, "w") as file:
for key, value in format_value.items():
file.write(f"{key} value={value}\n")
if key == "State" :
if value == "0" or value == "1":
file.write(f"preaccl value=1\n")
file.write(f"valve1 value=1\n")
file.write(f"valve2 value=1\n")
file.write(f"hv value=1\n")
file.write(f"boiler value=1\n")
file.write(f"ion value=1\n")
if value == "2":
file.write(f"preaccl value=0\n")
file.write(f"valve1 value=1\n")
file.write(f"valve2 value=1\n")
file.write(f"hv value=0\n")
file.write(f"boiler value=1\n")
file.write(f"ion value=1\n")
if value == "3":
file.write(f"preaccl value=0\n")
file.write(f"valve1 value=0\n")
file.write(f"valve2 value=0\n")
file.write(f"hv value=0\n")
file.write(f"boiler value=1\n")
file.write(f"ion value=0\n")
if value == "4":
file.write(f"preaccl value=0\n")
file.write(f"valve1 value=0\n")
file.write(f"valve2 value=0\n")
file.write(f"hv value=0\n")
file.write(f"boiler value=0\n")
file.write(f"ion value=0\n")
os.system(cmd)