Dit is uit te lezen met een Python3 programma:
#!/usr/bin/python3 import serial import string import re ser = serial.Serial('/dev/ttyUSB0', 115200, 8, 'N', 1, timeout=1) time = "" kw = 0.0 f = open('pir.txt', 'w') while True: ser_data = ser.readline().decode('ascii').strip() while ser_data != "": if re.match(r'(?=1-0:22.7.0)', ser_data): print("20" + time[0:6] + "-" + time[6:8] + ":" + time[8:10] + ":" + time[10:12] + "|" + f"{actual_delivered:.3f}" + "|" + f"{actual_returned:.3f}" + "|" + f"{volt:.2f}", file=f ) f.flush() if re.match(r'(?=0-0:1.0.0)', ser_data): time = re.split(r'[()]',ser_data)[1] if re.match(r'(?=0-0:96.14.0)', ser_data): tariff = int(re.split(r'[()]',ser_data)[1]) if re.match(r'(?=1-0:1.8.1)', ser_data): try: delivered1 = float(re.split(r'[(*]',ser_data)[1]) except: delivered1 = -1.0 if re.match(r'(?=1-0:1.8.2)', ser_data): try: delivered2 = float(re.split(r'[(*]',ser_data)[1]) except: delivered2 = -1.0 if re.match(r'(?=1-0:2.8.1)', ser_data): try: returned1 = float(re.split(r'[(*]',ser_data)[1]) except: returned1 = -1.0 if re.match(r'(?=1-0:2.8.2)', ser_data): try: returned2 = float(re.split(r'[(*]',ser_data)[1]) except: returned2 = -1.0 if re.match(r'(?=1-0:1.7.0)', ser_data): try: actual_delivered = float(re.split(r'[(*]',ser_data)[1]) except: actual_delivered = -1.0 if re.match(r'(?=1-0:2.7.0)', ser_data): try: actual_returned = float(re.split(r'[(*]',ser_data)[1]) except: actual_returned = -1.0 if re.match(r'(?=1-0:32.7.0)', ser_data): try: volt = float(re.split(r'[(*]',ser_data)[1]) except: volt = -1.0 ser_data = ser.readline().decode('ascii').strip()Dit zal elke seconde een regel schrijven in de file 'pir.txt'. Om dit wat toegangkelijk te maken gebruik ik een bash shell script dat via 'nc' een webserver levert:
while true; do { echo -ne "HTTP/1.1 200 OK\n\n<!DOCTYPE html> <html> <head> <meta http-equiv='refresh' content='4'> </head> <body><h3>"; tail -n 30 pir.txt | sed '{:q;N;s/\n/<br>/g;t q}'; echo "</h3></body></html>"; } | nc -l -k -p 8080 -q 2; doneBeide applicaties start ik nu nog handmatig met nohup.
Elke 4 seconden zal dit op poort 8080 van de Raspberry Pi de laatste 30 regels van 'pir.txt' tonen met behulp ban de refresh html metatag.
Zo heb je real time inzicht in je stroomverbruik/levering door je zonnepanelen. Getoond wordt: datum-tijd '|' verbruikt kWh '|' tregug geleverd kWh '|' voltage.
No comments:
Post a Comment