| @@ -20,6 +20,7 @@ from colorama import Style | |||||
| from subprocess import check_call | from subprocess import check_call | ||||
| from signal import pause | from signal import pause | ||||
| import smtplib, ssl | import smtplib, ssl | ||||
| from collections import deque | |||||
| picdir = os.path.realpath("/home/firestone/pi-zero-tank/pic") | picdir = os.path.realpath("/home/firestone/pi-zero-tank/pic") | ||||
| libdir = os.path.realpath("/home/firestone/pi-zero-tank/lib") | libdir = os.path.realpath("/home/firestone/pi-zero-tank/lib") | ||||
| @@ -37,6 +38,7 @@ import traceback | |||||
| logging.basicConfig(level=logging.DEBUG) | logging.basicConfig(level=logging.DEBUG) | ||||
| NUM_SAMPLES = 100 | NUM_SAMPLES = 100 | ||||
| AVG_WINDOWSIZE = 20 | |||||
| FILE = Path("config.ini") | FILE = Path("config.ini") | ||||
| ALARM_LEVEL_CM = 5.0 | ALARM_LEVEL_CM = 5.0 | ||||
| @@ -128,6 +130,15 @@ class Buttons: | |||||
| def none_event(self): | def none_event(self): | ||||
| pass | 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): | def shutdown(epd): | ||||
| print("System wird heruntergefahren:") | print("System wird heruntergefahren:") | ||||
| print("Erstelle Backup...") | print("Erstelle Backup...") | ||||
| @@ -307,35 +318,10 @@ if __name__ == '__main__': | |||||
| buttons = Buttons(button_cl, button_ph) | buttons = Buttons(button_cl, button_ph) | ||||
| print("Starte Hauptprogramm...") | print("Starte Hauptprogramm...") | ||||
| mavg_cl = MovingAverage(AVG_WINDOWSIZE) | |||||
| mavg_ph = MovingAverage(AVG_WINDOWSIZE) | |||||
| while True: | while True: | ||||
| try: | 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() | state = buttons.get_state() | ||||
| if state == States.MEASURE_CL_FULL: | if state == States.MEASURE_CL_FULL: | ||||
| buttons.deactivate_events() | buttons.deactivate_events() | ||||
| @@ -407,6 +393,34 @@ if __name__ == '__main__': | |||||
| buttons.activate_events() | buttons.activate_events() | ||||
| elif state == States.SHUTDOWN: | elif state == States.SHUTDOWN: | ||||
| shutdown(epd) | 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) | time.sleep(0.5) | ||||
| except KeyboardInterrupt: | except KeyboardInterrupt: | ||||
| shutdown(epd) | shutdown(epd) | ||||