Advertisement
Guest User

light

a guest
Jan 18th, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. from datetime import time as Time, datetime, timedelta
  2. from time import time, sleep
  3. from gpiozero import LED
  4. from multiprocessing import Process
  5. from functools import partial
  6. from tkinter.ttk import Combobox
  7. from tkinter import Tk, Label, Button, Frame
  8.  
  9. hrs = [('0' + str(hr))[-2:] for hr in range(25)]
  10. mins = [('0' + str(min_))[-2:] for min_ in range(60)]
  11. procs = []
  12. growlight = LED(17)
  13.  
  14. def light_on():
  15.     """ every 1 minute, checks if it is time for the light to be on or off """
  16.     while True:
  17.         now = datetime.now()
  18.         now = (now.hour, now.minute)
  19.         start = (int(hr_cb.get()), int(min_cb.get()))
  20.         hrs = int(len_cb.get())
  21.         stop = (start[0] + hrs, start[1])
  22.  
  23.         with open('/home/pi/code/python/log.txt','a') as af: # debug
  24.             # debug
  25.             af.write(f'start: {start}\n')
  26.             af.write(f'now:   {now}\n')
  27.             af.write(f'stop:  {stop}\n')
  28.             af.write(f'hrs: {hrs}\n')
  29.             af.write(f'before logic: {growlight}\n')
  30.  
  31.             if  start <= now < stop:
  32.                 if not growlight.is_active:
  33.                     growlight.on()
  34.                     af.write('!on signal sent\n') # debug
  35.                 else:
  36.                     af.write('light was already on, and should be.\n')
  37.             elif growlight.is_active:
  38.                 growlight.off()
  39.                 af.write('!off signal sent\n') # debug
  40.             else:
  41.                 af.write('light was already off, and should be.\n')
  42.  
  43.             af.write(f'after logic: {growlight}\n\n')
  44.  
  45.             af.close() # debug
  46.  
  47.         sleep(59)
  48.  
  49. def light_timer():
  50.     global timer
  51.     global procs
  52.     if len(procs) > 0:
  53.         for proc in procs:
  54.             proc.terminate()
  55.     timer = Process(target=light_on)
  56.     procs.append(timer)
  57.     timer.start()
  58.  
  59. def cleanup_kill():
  60.     global procs
  61.     if len(procs) > 0:
  62.         for proc in procs:
  63.             proc.terminate()
  64.     root.destroy()
  65.  
  66.  
  67. root = Tk()
  68. root.title('growlight')
  69. timeframe = Frame(root)
  70.  
  71. hr_cb = Combobox(timeframe, values=hrs, width=5)
  72. hr_cb.set('07')
  73. colon = Label(timeframe, text=':')
  74. min_cb = Combobox(timeframe, values=mins, width=5)
  75. min_cb.set('00')
  76. len_cb = Combobox(root, values=hrs, width=5)
  77. len_cb.set(12)
  78. hr_lbl = Label(root, text="Start Time:")
  79. len_lbl = Label(root, text="Length of daylight:")
  80. go = Button(root, text="reset cycle", command=light_timer)
  81.  
  82. hr_cb.grid(row=1, column=0)
  83. colon.grid(row=1, column=1)
  84. min_cb.grid(row=1, column=2)
  85.  
  86. hr_lbl.grid(row=0, column=0)
  87. timeframe.grid(row=1, column=0)
  88. len_lbl.grid(row=2, column=0)
  89. len_cb.grid(row=3, column=0)
  90. go.grid(row=4, column=0)
  91.  
  92. root.protocol("WM_DELETE_WINDOW", cleanup_kill)
  93. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement