nanopi_setup/hydro_lights/hydro_lights.py

56 lines
1.4 KiB
Python

# 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):
subprocess.run(["gpio", "mode", gpio_lights, "out"])
if state == 'on':
print('on')
subprocess.run(["gpio", "write", gpio_lights, "1"])
else:
print('off')
subprocess.run(["gpio", "write", gpio_lights, "0"])
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=8 )
#schedule to turn off
scheduler.add_job(turn_off_lights, trigger='cron', hour=20 )
while True:
time.sleep(30)