#! /usr/bin/env python # -*- coding: utf-8 -*- import subprocess import time import datetime import RPi.GPIO as GPIO import math import Si7021 import pigpio MIN_FEUCHTE_INNEN=65 MAX_FEUCHTE_INNEN=70 MAX_TEMP_INNEN=26 MIN_TEMP_INNEN=15 ABS_DIFF_LOWTEMP=3 ABS_DIFF_HIGHTEMP=1 TEMP_LOW_HIGH=13 #Feuchteberechnung a = 6.112 b = 17.67 c = 243.5 def svp(t): svp = a * math.exp((b*t)/(c+t)) return svp def vp(rh, t): vp = rh/100. * svp(t) return vp # Compute the absolute humidity in g/m³ def ah(rh, t): mw = 18.016 # kg/kmol (Molekulargewicht des Wasserdampfes) rs = 8314.3 # J/(kmol*K) (universelle Gaskonstante) ah = 10**5 * mw/rs * vp(rh, t)/(t + 273.15) return ah def fan_on(): GPIO.output(12, GPIO.LOW) print("FAN ON") def fan_off(): GPIO.output(12, GPIO.HIGH) print("FAN OFF") def sensor_off(): GPIO.output(32, GPIO.HIGH) GPIO.output(36, GPIO.HIGH) GPIO.output(37, GPIO.HIGH) print("SENSOR OFF") def read_sensor(pin): #print pin GPIO.output(pin, GPIO.LOW) time.sleep(1) pi = pigpio.pi() s = Si7021.sensor(pi) s.set_resolution(0) temp = s.temperature() humi = s.humidity() s.cancel() pi.stop() GPIO.output(pin, GPIO.HIGH) time.sleep(1) return (humi, temp) def main(): print "Feuchtesteuerung V0.2" print"Init" time.sleep(1) # Mount xfer from nas for logging subprocess.call(["/home/pi/./smbmount.sh"]) # Disable GPIO Warnings GPIO.setwarnings(False) # RPi.GPIO Layout verwenden (wie Pin-Nummern) GPIO.setmode(GPIO.BOARD) # Pin 12 (GPIO 18) auf Output setzen GPIO.setup(12, GPIO.OUT) # Pin 28 (GPIO 1) auf Output setzen GPIO.setup(32, GPIO.OUT) # Pin 32 (GPIO 12) auf Output setzen GPIO.setup(36, GPIO.OUT) # Pin 36 (GPIO 16) auf Output setzen GPIO.setup(37, GPIO.OUT) # Datei für Messwerte erstellen file = datetime.datetime.now().isoformat() f = open('/mnt/ramdisk/'+file+'.log', 'w') f.write('Zeitstempel;Innen-Temp1;Innen-Temp2;Außen-Temp;rel-Feuchte1;rel-Feuchte2;rel-Außen;abs-Feuchte1;abs-Feuchte2;abs-Außen;abs-Diff;Lüfterfreigabe;Lüftersperre;Lüfterstatus\n') f.close() fan_off() sensor_off() fan_lock = 0 fan_status = 0 fan_enable = 0 fan_count = 0 time_count = 0 restart_counter=0 print "running" while 1: # Zähler für Zeitsteuerung time_count += 1 # Zähler für Lüfterinterval fan_count += 1 # akteullen Zeitstempel speichern timestamp = datetime.datetime.now().isoformat() # Sensoren abfragen rha, ta = read_sensor(32) #print rha, ta rhi1, ti1 = read_sensor(36) #print rhi1, ti1 rhi2, ti2 = read_sensor(37) #print rhi2, ti2 # Prüfe auf fehlerhafte Sensorwerte if rha is None or ta is None or rha > 120: print "rha error" # Warte bis zur nächsten Messung time.sleep(53.35) continue if rhi1 is None or ti1 is None or rhi1 > 120: print "rhi1 error" # Warte bis zur nächsten Messung time.sleep(53.35) continue if rhi2 is None or ti2 is None or rhi2 > 120: print "rhi2 error" # Warte bis zur nächsten Messung time.sleep(53.35) continue # Absolute Feuchte berechnen ahi1 = ah(rhi1,ti1) ahi2 = ah(rhi2,ti2) aha = ah(rha,ta) ti = ti1 rhi = rhi1 ahi = ahi1 if ahi1 < ahi2: ti = ti2 rhi = rhi2 ahi = ahi2 rhi = round(rhi,1) ti = round(ti,1) ahi = round(ahi,1) rhi1 = round(rhi1,1) ti1 = round(ti1,1) ahi1 = round(ahi1,1) rhi2 = round(rhi2,1) ti2 = round(ti2,1) ahi2 = round(ahi2,1) rha = round(rha,1) ta = round(ta,1) aha = round(aha,1) diff = ahi - aha # Lüfterstatus prüfen if GPIO.input(12): fan_status_real = 0 else: fan_status_real = 1 # Feuchte Differenz prüfen if (ahi - aha) > ABS_DIFF_LOWTEMP or ( (ahi - aha) > ABS_DIFF_HIGHTEMP and ta > TEMP_LOW_HIGH ): # draußen trockener: grundsätzliche Lüfterfreigabe if fan_enable < 3: fan_enable += 1 # Zusatzbedingungen prüfen: if rhi < MIN_FEUCHTE_INNEN or ti < MIN_TEMP_INNEN or ti > MAX_TEMP_INNEN: fan_enable = 0 #print "trocken genug oder zu warm/kalt innen" if rhi > MAX_FEUCHTE_INNEN: if fan_enable < 3: fan_enable += 1 #print "zu feucht" # feuchter draußen: Lüfterfreigabe AUS else: fan_enable = 0 # Prüfe 20 minütige Lüftersperre, Lüfter aus if fan_lock > 0: if not fan_count % 20: fan_count = 0 if fan_status == 1: fan_off() fan_status = 0 else: fan_lock -= 1 # Lüfter einschalten if fan_lock == 0: if fan_enable > 2: if fan_status == 0 and fan_status_real == 0: fan_on() fan_status = 1 fan_lock = 2 fan_count = 0 # Lüfterstatus prüfen if GPIO.input(12): fan_status_real = 0 else: fan_status_real = 1 # Messwerte abspeichern f = open('/mnt/ramdisk/'+file+'.log', 'a') f.write(timestamp) f.write(';') f.write(str(ti1)) f.write(';') f.write(str(ti2)) f.write(';') if ta < 10: f.write(' ') f.write(str(ta)) f.write(';') f.write(str(rhi1)) f.write(';') f.write(str(rhi2)) f.write(';') f.write(str(rha)) f.write(';') if ahi1 < 10: f.write(' ') f.write(str(ahi1)) f.write(';') if ahi2 < 10: f.write(' ') f.write(str(ahi2)) f.write(';') if aha < 10: f.write(' ') f.write(str(aha)) f.write(';') if diff >= 0: f.write(' ') f.write(str(diff)) f.write(';') f.write(str(fan_enable)) f.write(';') f.write(str(fan_lock)) f.write(';') if fan_status_real == 1: f.write('EIN') else: f.write('AUS') f.write('\n') f.close() # Daten alle 10 Minuten auf NAS synchroniseren if not time_count % 10: subprocess.Popen(["/home/pi/./syncdata.sh"]) # Warte bis zur nächsten Messung time.sleep(53.35) if __name__ == '__main__': main()