#Code samples: Tuya Smart Plug Energy Meter

Code tips and tricks for beginners
Post Reply
User avatar
Oli
Posts: 47
Joined: Tue Feb 09, 2021 10:07 am
Location: Germany, Meissen
Has thanked: 13 times
Been thanked: 45 times
Contact:

#Code samples: Tuya Smart Plug Energy Meter

Post by Oli »

This is a functioning demo, it is made very easy for better understanding.
The Code is for Annex 1.42 newer dont work with 1 or 2 MB Flash, still in progress, I hope.
This is 1. version, later there are logger,
and communication for more smart,
It can be switched
It shows the power in watts and counts KW/H
The calibration can be set
It could still show ampere and volt, but I don't need that at the moment,
The Tuya module must be exchanged for an ESP-02S module (approx. € 2),

I hope it helps someone, have fun ...

Code: [Local Link Removed for Guests]

DevName$ = "Plug1"
' HLW8012 / BL0937 / CSE7759
pin.mode 04, input     'Power Takt  .800W ca 550 Hz
pin.mode 05, input     'Volt A Takt   ,240V=ca 1880Hz 3A=ca 250Hz
PIN.MODE 12, OUTPUT    'select  0=Ampere  1=Volt für pin 04
pin.mode 03, input     'Taste gedückt=0
PIN.MODE 14, OUTPUT    'Relais 1=ein + rote LED
PIN.MODE 13, OUTPUT    'blau LED 0=ein

COUNTER.SETUP 1, 04, 1 'counter1, Pin 04 , rising  POWER
'COUNTER.SETUP 2, 05, 1 'counter1, Pin 05 , rising  VOLT AMPERE
timer0 1000, Sekunde
INTERRUPT 03, Taste

Watt = 0
kwStunden = 0
kwSekunde=0
wSekunde =0
kwh=0.00001
WattS=0.000000001
a=0
f=0
LED = 0
ld = 0
box$ = "   "
corr = 1

if FILE.EXISTS("/"+DevName$+"/corr.txt") = 0 then file.save "/"+DevName$+"/corr.txt", str$(corr)
corr = val(file.read$("/"+DevName$+"/corr.txt"))

OnHtmlReload main_Page
OnHtmlChange Jump1

main_Page:   'neuzeichnen der webseite
CLS
a$ = ""
a$ = a$ + DevName$+ " = name of this device<br> <br>"'zeigt String  + ändert  stringe
a$ = a$ + button$("ON", but_on, "but1") + "......"'ON knopf  und sprungziel bei drücken
a$ = a$ + cssid$("but1", "background-color: green;")'Farbe des Knopfes
a$ = a$ + button$("OFF", but_off, "but2") + "......"'Off knopf  und sprungziel bei drücken
a$ = a$ + cssid$("but2", "background-color: red;")'Farbe des Knopfes
a$ = a$ + button$("change", but_change, "but3") + "......"'Off knopf  und sprungziel bei drücken ohne Farbe
a$ = a$ + led$(ld) + "<br> <br>" 'LED  1 oder 1 = rot oder grün   + Zeilenumbruch
a$ = a$ + meter$(Watt, 0, 1500, "but4")+ "<br>"  ' meter
a$ = a$ + cssid$("but4", "width: 500px")

a$ = a$ + textbox$(Watt) + "Watt<br>"
a$ = a$ + textbox$(kwStunden) + "kw/h<br> <br>"
a$ = a$ + textbox$(corr) + " Kalibrierwert Watt, Beispiel 1.437 "'Variable + ändert variable 
HTML a$
AUTOREFRESH 500



anf:

Watt = COUNTER.FREQ(1)
Watt = Watt * corr'1.437             'Eichung
temp1$ = str$(Watt,"%2.0f")     'Runden                  
Watt = val(temp1$)              'Runden  
temp1$ = str$(kwh,"%4.3f")      'Runden                  
kwStunden = val(temp1$)         'Runden  


pause 300
goto anf


but_on:
ld = 1
PIN(14) = 1
'refresh 'aktualisiere webseite, zeichne aber nicht neu
return

but_off:
ld = 0
PIN(14) = 0
'refresh'aktualisiere webseite, zeichne aber nicht neu
return

Taste:
if pin(03) = 1 then return
but_change:
ld = 1 - ld
PIN(14) = 1 - PIN(14)
'refresh'aktualisiere webseite, zeichne aber nicht neu
return

Jump1:
wlog "schreiben"
'file.save "/"+DevName$+"/DevName$.txt", DevName$
file.save "/"+DevName$+"/corr.txt", str$(corr)
pause 200
Return



Sekunde:
wSekunde = Watt * 0.00027777778 'Watt/Sekunde zu w/h
kwSekunde = wSekunde / 1000 'w/h zu kw/h
kwh = kwh + kwSekunde       'jede Sekunde die Partion kW/Sekunde dazuzählen
if LED = 1 then PIN(13) = 1 - PIN(13) 'blink
'refresh
return
You do not have the required permissions to view the files attached to this post.
Post Reply