diff --git a/hydroponics/hydroponic_lights.py b/hydroponics/hydroponic_lights.py new file mode 100644 index 0000000..4f63011 --- /dev/null +++ b/hydroponics/hydroponic_lights.py @@ -0,0 +1,60 @@ +# Toggles gpio to turn grow lights on/off and then updates grafana +import requests +import time +import subprocess +from apscheduler.schedulers.background import BackgroundScheduler +scheduler = BackgroundScheduler() +scheduler.start() + +gpio_lights = "15" + +def send_data(results): + try: + output = requests.post('http://192.168.1.127:6503/write?db=garagedb', data = results, timeout=30) + print(output) + except: + pass + +def toggle_lights(state): + + if state == 'on': + print('on') + subprocess.run(["gpio", "mode", gpio_lights, "out"]) + subprocess.run(["gpio", "write", gpio_lights, "1"]) + else: + print('off') + subprocess.run(["gpio", "write", gpio_lights, "0"]) + subprocess.run(["gpio", "mode", gpio_lights, "in"]) + + +def turn_on_lights(): + print('Turning lights on') + toggle_lights('on') + results = 'hydro_lights,host=nanopi value={}'.format(1) + print(results) + send_data(results) + +def turn_off_lights(): + print('Turning lights off') + toggle_lights('off') + results = 'hydro_lights,host=nanopi value={}'.format(0) + print(results) + send_data(results) + +print('Starting up') +#Just to check lights are off +turn_off_lights() +print('Lights are off') +#turn_on_lights() +#print('Lights are on') + +#schedule to turn on at 8am +scheduler.add_job(turn_on_lights, trigger='cron', hour=10 ) +#scheduler.add_job(turn_on_lights, trigger='cron', hour=8 ) + +#schedule to turn off +#scheduler.add_job(turn_off_lights, trigger='cron', hour=20 ) +scheduler.add_job(turn_off_lights, trigger='cron', hour=19 ) + +while True: + time.sleep(30) \ No newline at end of file