Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import time as Time, datetime, timedelta
- from time import time, sleep
- from gpiozero import LED
- from multiprocessing import Process
- from functools import partial
- from tkinter.ttk import Combobox
- from tkinter import Tk, Label, Button, Frame
- hrs = [('0' + str(hr))[-2:] for hr in range(25)]
- mins = [('0' + str(min_))[-2:] for min_ in range(60)]
- procs = []
- growlight = LED(17)
- def light_on():
- """ every 1 minute, checks if it is time for the light to be on or off """
- while True:
- now = datetime.now()
- now = (now.hour, now.minute)
- start = (int(hr_cb.get()), int(min_cb.get()))
- hrs = int(len_cb.get())
- stop = (start[0] + hrs, start[1])
- with open('/home/pi/code/python/log.txt','a') as af: # debug
- # debug
- af.write(f'start: {start}\n')
- af.write(f'now: {now}\n')
- af.write(f'stop: {stop}\n')
- af.write(f'hrs: {hrs}\n')
- af.write(f'before logic: {growlight}\n')
- if start <= now < stop:
- if not growlight.is_active:
- growlight.on()
- af.write('!on signal sent\n') # debug
- else:
- af.write('light was already on, and should be.\n')
- elif growlight.is_active:
- growlight.off()
- af.write('!off signal sent\n') # debug
- else:
- af.write('light was already off, and should be.\n')
- af.write(f'after logic: {growlight}\n\n')
- af.close() # debug
- sleep(59)
- def light_timer():
- global timer
- global procs
- if len(procs) > 0:
- for proc in procs:
- proc.terminate()
- timer = Process(target=light_on)
- procs.append(timer)
- timer.start()
- def cleanup_kill():
- global procs
- if len(procs) > 0:
- for proc in procs:
- proc.terminate()
- root.destroy()
- root = Tk()
- root.title('growlight')
- timeframe = Frame(root)
- hr_cb = Combobox(timeframe, values=hrs, width=5)
- hr_cb.set('07')
- colon = Label(timeframe, text=':')
- min_cb = Combobox(timeframe, values=mins, width=5)
- min_cb.set('00')
- len_cb = Combobox(root, values=hrs, width=5)
- len_cb.set(12)
- hr_lbl = Label(root, text="Start Time:")
- len_lbl = Label(root, text="Length of daylight:")
- go = Button(root, text="reset cycle", command=light_timer)
- hr_cb.grid(row=1, column=0)
- colon.grid(row=1, column=1)
- min_cb.grid(row=1, column=2)
- hr_lbl.grid(row=0, column=0)
- timeframe.grid(row=1, column=0)
- len_lbl.grid(row=2, column=0)
- len_cb.grid(row=3, column=0)
- go.grid(row=4, column=0)
- root.protocol("WM_DELETE_WINDOW", cleanup_kill)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement