#!/usr/bin/env python3
import serial, time

ser=serial.Serial('/dev/ttyACM0', 9600,timeout=.5) # serial connection

logFile=open('lightLog.txt','w') # open and write to lightLog.txt

while True:
    now = time.localtime() # year[0], month[1], day[2], hour[3], minute[4], second[5], weekday[6], yearday[7]
    timestamp=(str(now[3])+":"+str(now[4])+":"+str(now[5])+" "+str((now[1]))+"/"+str(now[2])+"/"+str(now[0]))
    if int(now[3]) == 7: # if hour is 7 am or greater, STOP collecting data
        logFile.close()
        exit() # no error message, or breaks crontab
    else:
        data=ser.readline()
        light=str(data).encode("utf-8")
        a=light.strip(b"b'") # remove beginning empty space
        Light=int(float(a[:-4])) # remove last 4 bits, leaving only float->int of light_val
        logFile.write("Ambient Light: "+str(Light)+" "+timestamp+"\n")
        logFile.flush()
        print((Light),timestamp)
        time.sleep(60) # how often to sample data line