sysadmin/scripts/arduino.py

37 lines
869 B
Python
Raw Normal View History

2017-09-15 14:11:31 -04:00
#!/usr/bin/env python
""" arduino reader by afx """
import time, serial
from sys import argv
def query_arduino():
global serial
fo = open('/etc/scripts/.arduino.db', 'wb')
try:
serial = serial.Serial('/dev/ttyACM0', 9600)
serial.write('1')
query = serial.readline().strip('\r\n').split()
except:
query = ("0", "0")
2017-09-15 14:11:31 -04:00
fo.write(','.join(query))
fo.close()
def print_arduino(pmode):
fr = open('/etc/scripts/.arduino.db', 'r+')
rquery = fr.read(100);
print(rquery.split(',')[pmode])
fr.close()
if __name__ == "__main__":
mode = argv
try:
if mode[1] == 'temp':
print_arduino(0)
elif mode[1] == 'humid':
print_arduino(1)
elif mode[1] == 'query':
query_arduino()
except IndexError:
2017-09-15 14:11:31 -04:00
print('Usage: script.py [temp] [humid]')