| @@ -20,6 +20,7 @@ from colorama import Style | |||
| from subprocess import check_call | |||
| from signal import pause | |||
| import smtplib, ssl | |||
| from collections import deque | |||
| picdir = os.path.realpath("/home/firestone/pi-zero-tank/pic") | |||
| libdir = os.path.realpath("/home/firestone/pi-zero-tank/lib") | |||
| @@ -37,6 +38,7 @@ import traceback | |||
| logging.basicConfig(level=logging.DEBUG) | |||
| NUM_SAMPLES = 100 | |||
| AVG_WINDOWSIZE = 20 | |||
| FILE = Path("config.ini") | |||
| ALARM_LEVEL_CM = 5.0 | |||
| @@ -128,6 +130,15 @@ class Buttons: | |||
| def none_event(self): | |||
| pass | |||
| class MovingAverage: | |||
| def __init__(self, size: int): | |||
| self.size = size | |||
| self.buffer = deque(maxlen=size) | |||
| def add_value(self, value: float): | |||
| self.buffer.append(value) | |||
| return sum(self.buffer) / len(self.buffer), len(self.buffer) == self.size | |||
| def shutdown(epd): | |||
| print("System wird heruntergefahren:") | |||
| print("Erstelle Backup...") | |||
| @@ -307,35 +318,10 @@ if __name__ == '__main__': | |||
| buttons = Buttons(button_cl, button_ph) | |||
| print("Starte Hauptprogramm...") | |||
| mavg_cl = MovingAverage(AVG_WINDOWSIZE) | |||
| mavg_ph = MovingAverage(AVG_WINDOWSIZE) | |||
| while True: | |||
| try: | |||
| cl = measure(sensor_cl) | |||
| ph = measure(sensor_ph) | |||
| clp = get_percent(cl_leer, cl_voll, cl) | |||
| php = get_percent(ph_leer, ph_voll, ph) | |||
| display_site_main(epd, clp, php) | |||
| print("Chlor: {:10.2f} cm, pH-Minus: {:10.2f} cm".format(cl, ph)) | |||
| print("Chlor: {:10.2f} %, pH-Minus: {:10.2f} %".format(clp, php)) | |||
| if abs(cl_leer - ALARM_LEVEL_CM) < cl: | |||
| print(f"{Fore.RED}Chlor-Tank fast leer!{Style.RESET_ALL}") | |||
| if cl_mail == 0: | |||
| print("Sende E-Mail...") | |||
| send_email("Chlor", abs(cl_leer - cl)) | |||
| cl_mail = 1 | |||
| config.set('Chlor', 'mail', str(cl_mail)) | |||
| config.set('Chlor', 'datum_mail', str(datetime.datetime.now())) | |||
| with open(FILE, 'w') as configfile: | |||
| config.write(configfile) | |||
| if abs(ph_leer - ALARM_LEVEL_CM) < ph: | |||
| print(f"{Fore.RED}pH-Minus-Tank fast leer!{Style.RESET_ALL}") | |||
| if ph_mail == 0: | |||
| print("Sende E-Mail...") | |||
| send_email("pH-Minus", abs(ph_leer - ph)) | |||
| ph_mail = 1 | |||
| config.set('pH-', 'mail', str(ph_mail)) | |||
| config.set('pH-', 'datum_mail', str(datetime.datetime.now())) | |||
| with open(FILE, 'w') as configfile: | |||
| config.write(configfile) | |||
| state = buttons.get_state() | |||
| if state == States.MEASURE_CL_FULL: | |||
| buttons.deactivate_events() | |||
| @@ -407,6 +393,34 @@ if __name__ == '__main__': | |||
| buttons.activate_events() | |||
| elif state == States.SHUTDOWN: | |||
| shutdown(epd) | |||
| else: | |||
| cl, cl_status = mavg_cl.add_value(measure(sensor_cl)) | |||
| ph, ph_status = mavg_cl.add_value(measure(sensor_ph)) | |||
| clp = get_percent(cl_leer, cl_voll, cl) | |||
| php = get_percent(ph_leer, ph_voll, ph) | |||
| display_site_main(epd, clp, php) | |||
| print("Chlor: {:10.2f} cm, pH-Minus: {:10.2f} cm".format(cl, ph)) | |||
| print("Chlor: {:10.2f} %, pH-Minus: {:10.2f} %".format(clp, php)) | |||
| if cl_status and abs(cl_leer - ALARM_LEVEL_CM) < cl: | |||
| print(f"{Fore.RED}Chlor-Tank fast leer!{Style.RESET_ALL}") | |||
| if cl_mail == 0: | |||
| print("Sende E-Mail...") | |||
| send_email("Chlor", abs(cl_leer - cl)) | |||
| cl_mail = 1 | |||
| config.set('Chlor', 'mail', str(cl_mail)) | |||
| config.set('Chlor', 'datum_mail', str(datetime.datetime.now())) | |||
| with open(FILE, 'w') as configfile: | |||
| config.write(configfile) | |||
| if ph_status and abs(ph_leer - ALARM_LEVEL_CM) < ph: | |||
| print(f"{Fore.RED}pH-Minus-Tank fast leer!{Style.RESET_ALL}") | |||
| if ph_mail == 0: | |||
| print("Sende E-Mail...") | |||
| send_email("pH-Minus", abs(ph_leer - ph)) | |||
| ph_mail = 1 | |||
| config.set('pH-', 'mail', str(ph_mail)) | |||
| config.set('pH-', 'datum_mail', str(datetime.datetime.now())) | |||
| with open(FILE, 'w') as configfile: | |||
| config.write(configfile) | |||
| time.sleep(0.5) | |||
| except KeyboardInterrupt: | |||
| shutdown(epd) | |||