>D ; ============================================================ ; S0 Energiezaehler fuer zwei Eltako-Zaehler ; auf einem ESP32-WROOM-32 ; Peter F. aus H. Alpensichtung@bsky.social ; Counter1 / GPIO27 = Gartenstudio ; Counter2 / GPIO26 = Pelletheizung ; ; Zaehler: 100 Impulse/kWh ; 1 Impuls = 10 Wh ; 100 Imp/kWh 10 Wh gwh=10 ; 500 Imp/kWh 2 Wh gwh=2 ; 1000 Imp/kWh 1 Wh gwh=1 ; 2000 Imp/kWh 0,5 Wh gwh=0.5 ; ============================================================ ;gwh = Gartenstudio Wh/Impuls ;pwh = Pelletheizung Wh/Impuls ; Wh pro Impuls getrennte Vorbelegung gwh=1 pwh=1 ; ------------------------------------------------------------ ; PERMANENTE VARIABLEN ; Reihenfolge NICHT mehr veraendern! ; ------------------------------------------------------------ p:gtot=0 p:gday=0 p:gyest=0 p:ptot=0 p:pday=0 p:pyest=0 p:date=0 ; ------------------------------------------------------------ ; NICHT PERMANENTE VARIABLEN ; ------------------------------------------------------------ ; aktuelle Leistung gpwr=0 ppwr=0 ; Hardware-Counter gc=0 pc2=0 ; letzter bekannter Counterstand gold=0 pold=0 ; Anzahl neuer Impulse gd=0 pd=0 ; Zeitpunkt letzter Impuls in ms gtm=0 ptm=0 ; Impulsabstand gdt=0 pdt=0 ; Tageskennung today=0 tmp=0 >BS ; Wird beim Start ausgefuehrt, nachdem Sensoren initialisiert sind ; aktuellen Hardware-Counter uebernehmen ; vorhandene Impulse werden dadurch nach Neustart NICHT ; nochmals zum Verbrauch addiert gold=pc[1] pold=pc[2] ; Leistung startet nach Neustart bei 0 gpwr=0 ppwr=0 ; fuer Leistungsberechnung warten wir auf neue Pulse gtm=0 ptm=0 print S0 Script gestartet print Garten Counter %gold% print Pellet Counter %pold% print Gespeichert Garten Heute %gday% Wh print Gespeichert Garten Gestern %gyest% Wh print Gespeichert Garten Gesamt %gtot% Wh print Gespeichert Pellet Heute %pday% Wh print Gespeichert Pellet Gestern %pyest% Wh print Gespeichert Pellet Gesamt %ptot% Wh >F ; alle 100ms Impulse erfassen und Leistung berechnen ; ------------------------------------------------------------ ; GARTENSTUDIO / Counter1 ; ------------------------------------------------------------ gc=pc[1] if gc>gold then gd=gc-gold ; 100 Imp/kWh -> 10 Wh je Impuls gday+=gd*gwh gtot+=gd*gwh ; Leistung aus Abstand zwischen zwei Impulsen ; P(W) = 36.000.000 / Abstand(ms) if gtm>0 then gdt=millis-gtm if gdt>0 then gpwr=(3600000*gwh*gd)/gdt print GS Pulse %gd% Abstand %gdt% ms Power %gpwr% W endif endif gtm=millis gold=gc endif ; Hardware-Counter wurde extern zurueckgesetzt if gc0 then if millis-gtm>600000 then gpwr=0 endif endif ; ------------------------------------------------------------ ; PELLETHEIZUNG / Counter2 ; ------------------------------------------------------------ pc2=pc[2] if pc2>pold then pd=pc2-pold ; 100 Imp/kWh -> 10 Wh je Impuls oder so pday+=pd*pwh ptot+=pd*pwh ; Leistung aus Abstand zwischen zwei Impulsen if ptm>0 then pdt=millis-ptm if pdt>0 then ppwr=(3600000*pwh*pd)/pdt print PE Pulse %pd% Abstand %pdt% ms Power %ppwr% W endif endif ptm=millis pold=pc2 endif ; Hardware-Counter wurde extern zurueckgesetzt if pc20 then if millis-ptm>600000 then ppwr=0 endif endif >S ; Wird einmal pro Sekunde ausgefuehrt ; Tageswechsel und permanentes Speichern ; Tageslogik erst benutzen, wenn die Uhr ein plausibles Jahr hat if year>2025 then ; Tageskennung erzeugen: ; (Jahr-2020)*512 + Monat*32 + Tag ; Beispiel 24.10.2026: ; 6*512 + 10*32 + 24 = 3416 today=year today-=2020 today*=512 tmp=month tmp*=32 today+=tmp today+=day ; ----------------------------------------------------------- ; Erstinitialisierung oder Migration eines alten date-Wertes if date==0 then date=today svars print Tageskennung initialisiert %date% else if date>10000 then date=today svars print Alte Tageskennung korrigiert %date% else ; --------------------------------------------------------- ; ECHTER TAGESWECHSEL if date!=today then gyest=gday pyest=pday gday=0 pday=0 date=today svars print Tageswechsel print Garten gestern %gyest% Wh print Pellet gestern %pyest% Wh endif endif endif endif ; alle 5 Minuten permanente Variablen speichern if upsecs%300==0 then svars endif >R ; Wird bei einem geregelten Tasmota-Neustart ausgefuehrt. ; Permanente Variablen werden nach >R ohnehin gespeichert. print S0 Werte werden gespeichert >J ; JSON-Ausgabe fuer Status 10 / Telemetrie ,"Gartenstudio":{"Power":%0gpwr%,"Today_Wh":%0gday%,"Yesterday_Wh":%0gyest%,"Total_Wh":%0gtot%} ,"Pelletheizung":{"Power":%0ppwr%,"Today_Wh":%0pday%,"Yesterday_Wh":%0pyest%,"Total_Wh":%0ptot%} >W ; Ausgabe fuer Webinterface Gartenstudio Heute: %0gday% Wh / %0gpwr% W
Pelletheizung Heute: %0pday% Wh / %0ppwr% W