]> vault307.fbx.one Git - mu_code.git/blob - uart.py
early learning in mu editor
[mu_code.git] / uart.py
1 #!/usr/bin/env python3
2 import serial, time
3
4 ser=serial.Serial('/dev/ttyACM0', 9600,timeout=.5) # serial connection
5
6 logFile=open('lightLog.txt','w') # open and write to lightLog.txt
7
8 while True:
9 now = time.localtime() # year[0], month[1], day[2], hour[3], minute[4], second[5], weekday[6], yearday[7]
10 timestamp=(str(now[3])+":"+str(now[4])+":"+str(now[5])+" "+str((now[1]))+"/"+str(now[2])+"/"+str(now[0]))
11 if int(now[3]) == 7: # if hour is 7 am or greater, STOP collecting data
12 logFile.close()
13 exit() # no error message, or breaks crontab
14 else:
15 data=ser.readline()
16 light=str(data).encode("utf-8")
17 a=light.strip(b"b'") # remove beginning empty space
18 Light=int(float(a[:-4])) # remove last 4 bits, leaving only float->int of light_val
19 logFile.write("Ambient Light: "+str(Light)+" "+timestamp+"\n")
20 logFile.flush()
21 print((Light),timestamp)
22 time.sleep(60) # how often to sample data line