Licht im Growroom steuern mit dem Raspberry Pi
Das Script zum steuern:
/grow/steuern_licht.py
import RPi.GPIO as GPIO
import MySQLdb
from datetime import datetime as DateTime
# Lampen [Bezeichnung1,pin1][Bezeichnung2,Pin2], ...
LAMPEN = [ ["R1L1",23] , ["R1L2",24] , ["R1L3",25] , ["R1L4",13] ]
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
zeit=DateTime.now().strftime('%H:%M:%S')
print "Aktuelle Uhrzeit: " + zeit
db=MySQLdb.connect("127.0.0.1","pi","f6g7h8","grow")
cursor = db.cursor()
for i in LAMPEN:
GPIO.setup(i[1], GPIO.OUT)
cursor.execute("select wert from soll where name='" + i[0] + "an'")
an = cursor.fetchone()
cursor.execute("select wert from soll where name='" + i[0] + "aus'")
aus = cursor.fetchone()
if an[0] < zeit and zeit < aus[0]:
licht = "AN"
GPIO.output(i[1],0)
else:
licht = "AUS"
GPIO.output(i[1],1)
print( i[0] + " : " + an[0] + " bis " + aus[0] + " Zustand: " + licht )
db.close()
#GPIO.cleanup()
Zum automatischen ausführen als Cronjob, zB alle 5 Minuten:
crontab -e
*/5 * * * * /usr/bin/python /home/pi/grow/steuern_licht.py