Abluft im Growroom steuern mit dem Raspberry Pi
Das Script zum steuern:
/grow/steuern_abluft.py
# -*- coding: UTF-8 -*-
import RPi.GPIO as GPIO
import MySQLdb
import time
from time import *
import datetime
PIN_ABLUFT = 5
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(PIN_ABLUFT, GPIO.OUT)
lt = localtime()
jahr, monat, tag, stunde, minute, sekunde = lt[0:6]
tag = "%04i-%02i-%02i" % (jahr,monat,tag)
zeit = "%02i:%02i:%02i" % (stunde,minute,sekunde)
print "Aktuelle Uhrzeit: " + str(zeit)
db=MySQLdb.connect("127.0.0.1","pi","f6g7h8","grow")
cursor = db.cursor()
cursor.execute("select wert from soll where name='R1Tmax'")
max_t = cursor.fetchone()
cursor.execute("select wert from soll where name='R1Hmax'")
max_h = cursor.fetchone()
cursor.execute("select wert from log where sensor='R0T1' order by id desc limit 0,1")
temperatur_aussen = cursor.fetchone()
cursor.execute("select wert from log where sensor='R0H1' order by id desc limit 0,1")
luftfeuchtigkeit_aussen = cursor.fetchone()
cursor.execute("select wert from log where sensor='R1T1' order by id desc limit 0,1")
temperatur_innen = cursor.fetchone()
cursor.execute("select wert from log where sensor='R1H1' order by id desc limit 0,1")
luftfeuchtigkeit_innen = cursor.fetchone()
cursor.execute("select wert from log where sensor='R1A1' and datum='" + tag + "' order by id desc limit 0,1")
abluft_war = cursor.fetchone()
print "Temperatur Maximal: ",max_t[0],"°C"
print "Temperatur innen: ",temperatur_innen[0],"°C"
print "Temperatur aussen: ",temperatur_aussen[0],"°C"
print "Luftfeuchtigkeit Maximal: ",max_h[0],"%"
print "Luftfeuchtigkeit innen: ",luftfeuchtigkeit_innen[0],"%"
print "Luftfeuchtigkeit aussen: ",luftfeuchtigkeit_aussen[0],"%"
if ( temperatur_innen[0] > int(max_t[0]) and temperatur_aussen[0] < temperatur_innen[0] ) or ( luftfeuchtigkeit_innen[0] > int(max_h[0]) and luftfeuchtigkeit_aussen[0] < luftfeuchtigkeit_innen[0] ):
abluft_wird = "100"
GPIO.output(PIN_ABLUFT,0)
else:
abluft_wird = "1"
GPIO.output(PIN_ABLUFT,1)
if abluft_war:
print"Abluft war : ",abluft_war[0]
print"Abluft wird: ",abluft_wird
if not abluft_war or str(abluft_war[0]) != str(abluft_wird):
cursor.execute("insert into log(datum,zeit,sensor,wert) values('%s','%s','%s','%s')" % (tag, zeit, 'R1A1', abluft_wird) )
print"Datenbank aktualisiert"
db.commit()
db.close()
Zum automatischen ausführen als Cronjob, zB alle 5 Minuten:
crontab -e
*/5 * * * * /usr/bin/python /home/pi/grow/steuern_abluft.py