47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
|
#!/usr/bin/python
|
||
|
|
||
|
import serial
|
||
|
import RPi.GPIO as GPIO
|
||
|
import time
|
||
|
import random
|
||
|
|
||
|
#Open serial port
|
||
|
ser = serial.Serial('/dev/ttyUSB0',baudrate=9600,timeout =1)
|
||
|
|
||
|
GPIO.setmode(GPIO.BCM)
|
||
|
# init list with pin numbers
|
||
|
pinList = [17]
|
||
|
# loop through pins and set mode and state to 'high'
|
||
|
for i in pinList:
|
||
|
GPIO.setup(i, GPIO.OUT)
|
||
|
|
||
|
#text file for saving the reading
|
||
|
file_path = '/home/pi03/TRI_reading.txt'
|
||
|
|
||
|
try:
|
||
|
while True:
|
||
|
#Read ASCII data from serial port
|
||
|
data = ser.readline().decode('ascii').strip()
|
||
|
if data:
|
||
|
try:
|
||
|
reading = (int(data[3:8],16)-160)*0.3
|
||
|
print("Received:", data, ":", data[3:8], "=", int(data[3:8],16),":",round(reading))
|
||
|
#write to file
|
||
|
with open(file_path, 'w') as file:
|
||
|
file.write(str(round(reading)))
|
||
|
if reading >50:
|
||
|
GPIO.output(17, GPIO.LOW)
|
||
|
#print ("ON")
|
||
|
else:
|
||
|
GPIO.output(17, GPIO.HIGH)
|
||
|
#print ("OFF")
|
||
|
except :
|
||
|
os.putenv('TRI_reading', 'no_data')
|
||
|
except KeyboardInterrupt:
|
||
|
#Close serial port on Crt+C
|
||
|
ser.close()
|
||
|
#print ("Quit")
|
||
|
|
||
|
# Reset GPIO settings
|
||
|
GPIO.cleanup()
|