Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

Moderators: grovkillen, Stuntteam, TD-er

Message
Author
LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#51 Post by LisaM » 23 Jun 2017, 00:51

LisaM wrote: 23 Jun 2017, 00:50
papperone wrote: 22 Jun 2017, 23:54 under testing... hope to relase it during the weekend ;)
Capture.JPG
You're to fast! ;) The pow is on it's way to me, but hasn't arrived yet. I'll test with the dragon and when the pow arrived also.

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#52 Post by LisaM » 23 Jun 2017, 00:53

Shardan wrote: 19 Jun 2017, 20:48 Will you sign a paper that you will pay every liability for the forum resulting from this?

As nobody knows where a reader starts in the wiki or the forum it's better to keep warnings up.
Then let's start every item with a warning: everything you do can kill you and everything posted on this forum is no acception to that! :lol: :lol: :lol:

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#53 Post by papperone » 23 Jun 2017, 13:14

Prior to release first ALPHA version into the Plugin Playground I'd like to collect some feedback and inputs.

As i design the plugin it shoudl be able to work both with ITEAD POW, ElectroDragon HLW8012 module and virtually any HLW8012 module as far as pinout is compatible.
The Plugin requires to know the 3 PINS used for the connection of ESP8266 to HLW module, which for POW are fixed (GPIO5/13/14) but for ED module can be freely selected.
In the Plugin GUI you can see and edit the internal multipliers used for the conversion from the chip data to the values output, while you can manually edit them I suggest not as it's not easy to manually find right values.
(the multipliers are not fixed as they depends on the "real" values of resistors, and in the 2 ED Module I do have they are in tolerance but not exactly what expected, thus some calibration is needed).
Fortunately the HLW8012 library (which is needed for the plugin to compile) can handle the calibration so I coded the plugin to accept below commands:
1) "HLWRESET" --> it will reset multipliers to the default ones in case they are messed up
2) "HLWCALIBRATE,V,A,W" --> it will calibrate the internal multipliers assuming the current values are the ones provided; parameters are not mandatory so if you want to calibrate only V you can send "HLWCALIBRATE,231" and so on (e.g. if you don't have a way to measure properly current you can still setup the others parameters with "HLWCALIBRATE,230,,100")

Ideally you shoudl connect a know pure resistive device (e.g. a 100W incandescent bulb) and measure V (using a volt meter), A (using an amperometic clamp), and provide W (actual rating of the bulb).
Doing so the reading will autoadjust and the device should be able to deliver more accurate output values (you need to calibrate only once as the setting are saved into config.dat of ESPEasy).

So far that's what I've ready, I've some ideas but will require time to be develop so during this weekend (need some extra testing this evening hopefully) I will upload current version to the playground.
Please if you download and use it (specially if you have a real POW which I don't have!) give me info, feedback, bugreport, anything that can be useful to fix/improve the plugin and make it stable and candidate to be included as standard in next mileston release 2.1.0 of ESPEasy.

NOTE: all HWL8012 modules and devices work with HIGH VOLTAGE and they do expose it to the user; make sure you connect LIVE and NEUTRAL correctly, as doing the other way around LIVE will be tied to chip GND and increasing the risks!!!
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#54 Post by LisaM » 23 Jun 2017, 18:32

papperone wrote: 23 Jun 2017, 13:14 NOTE: all HWL8012 modules and devices work with HIGH VOLTAGE and they do expose it to the user; make sure you connect LIVE and NEUTRAL correctly, as doing the other way around LIVE will be tied to chip GND and increasing the risks!!!
Thank god cars don't yell at me (in german) that a car is dangerous and might kill you every time i start the car... :lol:
In german, since i have a german car. And cars kill a lot of people every day.

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#55 Post by papperone » 23 Jun 2017, 19:17

Well to be openly honest I did a mistake during the test even myself, and swapped Live and Neutral... As a results at next power up of the module small sparks and 2 resistors on the module got burnt (all ok after I replaced them!).
Nobody was killed and I can oly balme myslef that overlooked the connections... :mrgreen:
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#56 Post by LisaM » 23 Jun 2017, 19:39

papperone wrote: 23 Jun 2017, 19:17 Well to be openly honest I did a mistake during the test even myself, and swapped Live and Neutral... As a results at next power up of the module small sparks and 2 resistors on the module got burnt (all ok after I replaced them!).
Nobody was killed and I can oly balme myslef that overlooked the connections... :mrgreen:
Join the club, i blew up a zenerdiode on my Sonoff TH16 due to a faulty breadboard voltage regulator spitting out 16V instead of the promised 5V. An AMS1117-5 DOA, go figure that...

Shardan
Normal user
Posts: 1156
Joined: 03 Sep 2016, 23:27
Location: Bielefeld / Germany

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#57 Post by Shardan » 24 Jun 2017, 02:19

LisaM wrote: 23 Jun 2017, 18:32
papperone wrote: 23 Jun 2017, 13:14 NOTE: all HWL8012 modules and devices work with HIGH VOLTAGE and they do expose it to the user; make sure you connect LIVE and NEUTRAL correctly, as doing the other way around LIVE will be tied to chip GND and increasing the risks!!!
Thank god cars don't yell at me (in german) that a car is dangerous and might kill you every time i start the car... :lol:
In german, since i have a german car. And cars kill a lot of people every day.
Thats why you need a driving license.
Regards
Shardan

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#58 Post by papperone » 25 Jun 2017, 07:37

_P170_HLW8012.ino into the playground.
Waiting for feedback if it works on an original Sonoff POW; instructions on previous posts...
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

kniazio
Normal user
Posts: 71
Joined: 12 Jun 2016, 11:12

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#59 Post by kniazio » 25 Jun 2017, 12:32

Compilation error

Code: Select all

C:\Users\kniazio\Desktop\ESP Releases\ESPEasy_v2.0.0-dev10\Source\ESPEasy\_P170_HLW8012.ino:17:21: fatal error: HLW8012.h: No such file or directory

 #include <HLW8012.h>

                     ^

compilation terminated.

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#60 Post by papperone » 25 Jun 2017, 12:45

you need to install HLW8012 library --> http://www.electrodragon.com/w/File:HLW ... 0_1281.zip
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

bluejedi
Normal user
Posts: 35
Joined: 26 Sep 2016, 14:27

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#61 Post by bluejedi » 25 Jun 2017, 19:50

papperone wrote: 23 Jun 2017, 13:14 NOTE: all HWL8012 modules and devices work with HIGH VOLTAGE and they do expose it to the user; make sure you connect LIVE and NEUTRAL correctly, as doing the other way around LIVE will be tied to chip GND and increasing the risks!!!
What makes things worse is that wiring colors are relevant only when hard-wired. When using a wall socket and power cord with mains plug to connect the POW it is very easy to to switch Live and Neutral because in many countries the mains plug fits in two ways (normal and 180 degrees rotated).
In Europe there is no standard for which pins of wall sockets are Live and which are Neutral (it mainly depends on the make of wall socket and how its connections are labelled).
In France there seems to be preference (maybe even a local standard) to have the Live pin on the right.
As far as I'm aware it is just the opposite in Germany. (I have German make wall sockets which have the left pin labelled as Live).
(So colors of 'flexible power cord' wires can never be trusted for which one is Live and which one is Neutral).

kniazio
Normal user
Posts: 71
Joined: 12 Jun 2016, 11:12

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#62 Post by kniazio » 25 Jun 2017, 20:33

papperone wrote: 25 Jun 2017, 12:45 you need to install HLW8012 library --> http://www.electrodragon.com/w/File:HLW ... 0_1281.zip
All right.
Thanks a lot

kniazio
Normal user
Posts: 71
Joined: 12 Jun 2016, 11:12

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#63 Post by kniazio » 26 Jun 2017, 06:01

What is sending data to Thingspeak?

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#64 Post by papperone » 26 Jun 2017, 06:08

kniazio wrote: 26 Jun 2017, 06:01 What is sending data to Thingspeak?
What? My plugin is not using any data logging. That's not in its scope....
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

kniazio
Normal user
Posts: 71
Joined: 12 Jun 2016, 11:12

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#65 Post by kniazio » 26 Jun 2017, 06:19

I mean, is it possible to send readings to Thingspeak
https://thingspeak.com or Domoticz

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#66 Post by papperone » 26 Jun 2017, 06:34

kniazio wrote: 26 Jun 2017, 06:19 I mean, is it possible to send readings to Thingspeak
https://thingspeak.com
Directly from ESPEasy it should be possible.but not sure how much ram it will eat.
I do lots of Thingspeak data logging but I use my Raspberry PI via Node Red which is much simpler and not hard coded into ESPEasy. Like this I am not linked to only one service and I can enable/disable much easily...

Anyhow I will add it to the "to do feature and will do some tests to check how much memory we need as I don't want to create more problem here as we are already in low memory situation....
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#67 Post by papperone » 26 Jun 2017, 06:36

By the way are you using it with a POW?
Does it work?
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

kniazio
Normal user
Posts: 71
Joined: 12 Jun 2016, 11:12

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#68 Post by kniazio » 26 Jun 2017, 06:46

papperone wrote: 26 Jun 2017, 06:36 By the way are you using it with a POW?
Does it work?
I do not use yet.
Waiting for the programmer

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#69 Post by LisaM » 02 Jul 2017, 01:28

It's compiling, programming and working and also not...

The pow:
IMG_2304.JPG
IMG_2304.JPG (1.27 MiB) Viewed 42941 times
The bottom:
IMG_2305.JPG
IMG_2305.JPG (1.7 MiB) Viewed 42941 times
Programming:
IMG_2306.JPG
IMG_2306.JPG (1.56 MiB) Viewed 42941 times
Reboot and running:
IMG_2307.JPG
IMG_2307.JPG (1.16 MiB) Viewed 42941 times
But, trying to submit this is failing:
pow-devices.JPG
pow-devices.JPG (202.19 KiB) Viewed 42941 times
Am i doing something wrong?

RE:
New user
Posts: 5
Joined: 11 Oct 2016, 15:01

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#70 Post by RE: » 02 Jul 2017, 23:10

How about setting the calibration values to 1.0 instead of 0.0 and tick the Enable box

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#71 Post by LisaM » 03 Jul 2017, 01:10

RE: wrote: 02 Jul 2017, 23:10 How about setting the calibration values to 1.0 instead of 0.0 and tick the Enable box
Done that, no effect...

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#72 Post by papperone » 03 Jul 2017, 08:38

that's weird, as the first time the plugin init itself it shoudl be saving the calibration values if they are equal to zero.
Can you try to send the command "HLWCalibrate" to calibrate manually and recalculate the multipliers?
By the way the default multipliers must be:
Power Mult: 10343611.74
Current Mult: 14484.49
Voltage Mult: 408636.51

Will check the code deeply later today but I just tried again 2 times in the 2 test modules and after erasing completely the SPIFF I've the deafult multipliers when I enable this device :(
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#73 Post by papperone » 03 Jul 2017, 08:43

As well the GUI is slightly different (probably in the latest GIT you are using they did some changes) as I do have the "delay" value in the bottom after the Controller (no big deal).
Can you try as well to setup the delay to some values? The pluing is supposed to read the values at a given interval (I tried up to 15 seconds and it does not delay the whole firmware noticeably)
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#74 Post by LisaM » 03 Jul 2017, 09:47

papperone wrote: 03 Jul 2017, 08:38 that's weird, as the first time the plugin init itself it shoudl be saving the calibration values if they are equal to zero.
Can you try to send the command "HLWCalibrate" to calibrate manually and recalculate the multipliers?
By the way the default multipliers must be:
Power Mult: 10343611.74
Current Mult: 14484.49
Voltage Mult: 408636.51

Will check the code deeply later today but I just tried again 2 times in the 2 test modules and after erasing completely the SPIFF I've the deafult multipliers when I enable this device :(
I have to admit, that it's not running in test-mode but normal mode. I've removed the #if test condition from the ino file.

Entered everything:
dev-init.JPG
dev-init.JPG (208.38 KiB) Viewed 42494 times
Leading to the browser error message: 'The connection was reset'

Going back to the page shows this pictures:
dev-reset.JPG
dev-reset.JPG (208.18 KiB) Viewed 42494 times
So i does save the multipliers, but then completely stalls...

By the way, i only connected the 230V input side and not the output side. I did check what wire was null and phase... ;)

The node info:
info.JPG
info.JPG (122.05 KiB) Viewed 42494 times
Update: The flash size seems odd, 4MB instead of the usual 1MB. I'll look into that...
Last edited by LisaM on 03 Jul 2017, 09:57, edited 1 time in total.

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#75 Post by papperone » 03 Jul 2017, 09:55

Ok I will test it with latest GIT this evening and let you know...
As POW has a relay maybe you can't have any reading as the relay is probably cutting the Live before the HLW8012 chip. Did you try it?

PS: Delay of 1 sec. can kill the ESPEasy performaces....
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#76 Post by LisaM » 03 Jul 2017, 09:58

papperone wrote: 03 Jul 2017, 09:55 Ok I will test it with latest GIT this evening and let you know...
As POW has a relay maybe you can't have any reading as the relay is probably cutting the Live before the HLW8012 chip. Did you try it?

PS: Delay of 1 sec. can kill the ESPEasy performaces....
Please also check out my update:

Code: Select all

Update: The flash size seems odd, 4MB instead of the usual 1MB. I'll look into that...
This might also be the cause of the problems.

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#77 Post by papperone » 03 Jul 2017, 10:50

LisaM wrote: 03 Jul 2017, 09:58
papperone wrote: 03 Jul 2017, 09:55 Ok I will test it with latest GIT this evening and let you know...
As POW has a relay maybe you can't have any reading as the relay is probably cutting the Live before the HLW8012 chip. Did you try it?

PS: Delay of 1 sec. can kill the ESPEasy performaces....
Please also check out my update:

Code: Select all

Update: The flash size seems odd, 4MB instead of the usual 1MB. I'll look into that...
This might also be the cause of the problems.
AFAIK ESPEasy needs to be compiled with 3Mb firmware / 1Mb SPIFF (for 4Mb units); about 1Mb I'm not sure which are the right build options...
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#78 Post by LisaM » 03 Jul 2017, 13:44

The Sonoff TH16 and basic have 1MB flash size. According to the iTead POW schematic, the POW has an 25q80bvs1g flash chip which is 8Mb. So it should be 1MB flash size instead of 4MB, i'll rebuild with the proper build settings and then try again...

UPDATE: Same problem, used these Arduino IDE settings:
settings.JPG
settings.JPG (23.34 KiB) Viewed 42455 times
The Info section also shows still this:
Flash Size: 4096 kB
Maybe the POW is 4MB?

UPDATE2: It's using a 'HK 25032' flash chip and that might wel be 4MB instead of the 1MB from the schematic...
Retrying with 4MB flash compile and flash.

UPDATE3: Recompiled with 4MB flash and flashed again: same problem... :(

UPDATE4: Bumped syslog to 4 and have now two debug entries in the syslog when trying to submit the hlw8012:

Code: Select all

Unit: 5 : DEBUG: String size:8366
Unit: 5 : DEBUG: String size:2632
Have no idea what it means, debug is at minimum level in this modules...

@papperone: Can you add more addLog statements in the ino? Then i can follow the proces through the syslog. Something simular to what P049 is doing would be great. I can do that to, but it will be lost at the next update of the source code...

mrwee
Normal user
Posts: 225
Joined: 31 Aug 2016, 12:52

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#79 Post by mrwee » 06 Jul 2017, 12:50

I've flashed my POW with Latest 2.0 and the plugin, using 1M / 128K SPIFFS.
When I try to change *any* setting in the plugin, the ESP always reboots. E.g. changing the delay, results in:
Exception (29):
epc1=0x402130a8 epc2=0x00000000 epc3=0x00000000 excvaddr=0x0000001c depc=0x00000000

ctx: cont
sp: 3fff3960 end: 3fff3f90 offset: 01a0

>>>stack>>>
3fff3b00: 3fff3c30 00000008 3fff1ed8 4021309f
3fff3b10: 40100296 00000023 3fff6da8 ffff8000
3fff3b20: 00000000 00000000 00000000 00000000
3fff3b30: 00000000 00000000 00000000 00000000
3fff3b40: 00000000 00000000 00000000 00000000
3fff3b50: 3fff67e4 0000001f 00000012 3fff67cc
3fff3b60: 0000000f 00000004 3fff3bd4 4023df6e
3fff3b70: 3ffea97c 3fff3d04 00000000 4010053d
3fff3b80: 3ffe88d8 3fff3c10 3ffea97c 00000001
3fff3b90: 3fff0b3c 00000008 3fff3c30 4020ebf5
3fff3ba0: 00000010 3fff3d9c 00000000 4010053d
3fff3bb0: 3ffea97c 00000000 3fff3d04 4023deeb
3fff3bc0: 3fff6e54 00000002 3fff6ddc 0000000f
3fff3bd0: 00000000 3fff6df4 0000000f 00000000
3fff3be0: 3ffea97c 00000000 3fff3d9c 00000001
3fff3bf0: 00000024 00000032 00000000 40201020
3fff3c00: 3fff6e55 00000004 3fff3d9c 4023df6e
3fff3c10: 3ffea97c 00000004 3fff1ed8 00000001
3fff3c20: 00000004 3fff0b3c 00000000 40220e37
3fff3c30: 3ffe0055 400056cf 3ffee7e0 00000030
3fff3c40: 40103105 3ffee7e0 3fff3c70 4010068c
3fff3c50: 00007fff 01c24c18 3fff6a84 0000000f
3fff3c60: 00000000 3fff69f4 0000000f 00000000
3fff3c70: 00000000 3fff6e0c 0000000f 00000001
3fff3c80: 3fff6e24 0000000f 00000001 3fff6e3c
3fff3c90: 0000000f 00000001 3fff6e54 0000000f
3fff3ca0: 00000001 3fff6c8c 0000000f 0000000b
3fff3cb0: 3fff6dc4 0000000f 0000000b 3fff677c
3fff3cc0: 0000001f 00000010 3fff67a4 0000001f
3fff3cd0: 00000010 3fff65f4 0000000f 00000000
3fff3ce0: 3fff6c44 0000000f 00000000 3fff6c5c
3fff3cf0: 0000000f 00000000 3fff6c74 0000000f
3fff3d00: 00000000 65776f50 61462072 726f7463
3fff3d10: 29252820 3fffda00 3fff2f68 00000030
3fff3d20: 40000f83 00000030 00000010 ffffffff
3fff3d30: 3fff6e6c 0000000f 00000002 3fff6e84
3fff3d40: 0000000f 00000000 3fff6e9c 0000000f
3fff3d50: 00000000 3fff6cec 0000000f 00000001
3fff3d60: 3fff715c 0000000f 00000000 3fff65dc
3fff3d70: 0000000f 00000000 00000000 00000000
3fff3d80: 00000000 00000000 00000000 00000000
3fff3d90: 00000000 00000000 00000000 3fff6eb4
3fff3da0: 0000000f 00000000 3fff6efc 0000000f
3fff3db0: 00000001 3fff6ee4 0000000f 00000000
3fff3dc0: 3fff6ecc 0000000f 00000000 3fff6c2c
3fff3dd0: 0000000f 00000000 3fff6c14 0000000f
3fff3de0: 00000000 3fff6bfc 0000000f 00000000
3fff3df0: 3fff6be4 0000000f 00000000 3fff6bcc
3fff3e00: 0000000f 00000002 3fff6624 0000000f
3fff3e10: 00000002 3fff660c 0000000f 00000002
3fff3e20: 3fff7144 0000000f 00000002 3fff7594
3fff3e30: 0000000f 00000003 3fff6cbc 0000000f
3fff3e40: 00000001 00000476 00000476 3fff2e84
3fff3e50: 00000001 3fff17ec 00000001 3fff17ec
3fff3e60: 00000010 3fff3ec0 00000003 000002bc
3fff3e70: 3fff70bc 00000008 3fff3ec0 4023623c
3fff3e80: 00000002 00000001 3fff4a24 40221d59
3fff3e90: 00000002 00000001 3fff3ec0 4023fe6a
3fff3ea0: 00000000 00000000 3fff4a24 40236232
3fff3eb0: 3fff4a24 3fff1f18 3fff4a24 4023626e
3fff3ec0: 00000000 00000000 00000000 4023e04c
3fff3ed0: 3fff4a24 3fff1f18 3fff1ed8 40236301
3fff3ee0: 3fff70bc 0000000f 00000008 3fff2f70
3fff3ef0: 00000000 00000132 00000132 00000001
3fff3f00: 00000002 00000132 00000019 402403c4
3fff3f10: 00000000 00000000 3fff27b4 3fff2f68
3fff3f20: 00000001 3fff1efc 3fff1ed8 4023653b
3fff3f30: 3fffdad0 3fff2f68 4023edbc 3fff2f70
3fff3f40: 00000000 3fff70d4 00000000 4023d17e
3fff3f50: 3fffdad0 00000000 3fff0b38 40223173
3fff3f60: 00000000 00000000 3fff0b38 40228444
3fff3f70: 3fffdad0 00000000 3fff2f62 4023ee08
3fff3f80: feefeffe feefeffe 3fff2f70 40100718
<<<stack<<<

ets Jan 8 2013,rst cause:1, boot mode:(3,7)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v09826c6d
~ld
⸮U


INIT : Booting version: (custom)
FS : Mount successful
INIT : Free RAM:26016
INIT : SPI not enabled
bcn 0
del if1
usl
add if1
dhcp server start:(ip:192.168.4.1,mask:255.255.255.0,gw:192.168.4.1)
bcn 100
bcn 0
del if1
usl
mode : sta(5c:cf:7f:20:db:c2)
add if0
WIFI : Connecting... 1
f r-1, scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt

Also I can't figure out the two "HLWRESET" & "HLWCALIBRATE,V,A,W" commands. Both results in 'Unknown command' when trying from serial or "Tools\Command".

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#80 Post by papperone » 06 Jul 2017, 14:00

I think the problem could be related to the fact that to save the parameters as they are "DOUBLE" I am forced to save them to SPIFF memory but I was able to test it only with 4Mb module.
I am not sure I can simulate a 1Mb module just compiling it with different paramentes, I will check.

@LisaM: this evening I will add few DEBUG points on the plugin in order to track down what is causing the issue...
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#81 Post by papperone » 06 Jul 2017, 17:16

new version with added DEBUG info just uploaded and "pull request" created....
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#82 Post by LisaM » 07 Jul 2017, 13:25

papperone wrote: 06 Jul 2017, 17:16 new version with added DEBUG info just uploaded and "pull request" created....
I see no debug changes here: https://github.com/letscontrolit/ESPEas ... l/51/files
Are you sure you commited the right file?

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#83 Post by papperone » 07 Jul 2017, 13:57

well I see indeeed something went wrong, just redid the pull request, hope it works now :cry:
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#84 Post by LisaM » 07 Jul 2017, 22:23

P170 with debug updated, compiled and flashed. The result from the syslog server:
Unit: 0 : EVENT: System#Boot
Unit: 0 : INIT : Normal boot
Unit: 0 : Subscribed to: domoticz/out
Unit: 0 : MQTT : Connected to broker
Unit: 0 : WIFI : Connected! IP: 192.168.178.89
None of the debug statements in P170 is listed...
Looks like the ESPEasy is resetting itself when the HLW8012 html form is send.

I'll add a shitload of debug statements to see what exactly is happening...

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#85 Post by papperone » 07 Jul 2017, 23:08

WOW...very disappointing, indeed it looks like it crashes as soon as the HLW is inited :(
Let me know your findings, and let's see what can be done, as on a 4Mb ESP8266 it is working flawlessly...
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#86 Post by LisaM » 08 Jul 2017, 01:20

Unit: 5 : MQTT : Payload: Connection Lost
Unit: 5 : MQTT : Topic: domoticz/out
Unit: 5 : MQTT : Payload: Connected
Unit: 5 : MQTT : Topic: domoticz/out
Unit: 5 : EVENT: Processing time:16 milliSeconds
Unit: 5 : WD : Uptime 0 ConnectFailures 0 FreeMem 22104
Unit: 5 : EVENT: Processing time:19 milliSeconds
Unit: 5 : EVENT: System#Boot
Unit: 5 : NTP : NTP replied: 21 mSec
Unit: 5 : NTP : NTP send to 194.109.6.2
Unit: 5 : NTP : NTP sync request:1
Unit: 5 : INIT : Normal boot
Unit: 5 : Subscribed to: domoticz/out
Unit: 5 : MQTT : Connected to broker
Unit: 5 : HLW8012: End PLUGIN_DEVICE_ADD
Unit: 5 : HLW8012: Begin PLUGIN_DEVICE_ADD
Unit: 5 : WIFI : Connected! IP: 192.168.178.89
Unit: 5 : HLW8012: Begin PLUGIN_WEBFORM_SAVE
It completes the plugin device add function and then resets itself...

The modified file:

Code: Select all

//#######################################################################################################
//#################### Plugin 170 HLW8012 AC Current and Voltage measurement sensor #####################
//#######################################################################################################
//
// This plugin is interfacing with HLW8012 IC which is use with some commercial devices:
// -- Sonoff POW
// -- ElectroDragon HLW8012 Breakout board
//
// The Sonoff POW uses the following PINs: SEL=GPIO05(D1), CF1=GPIO13(D8), CF=GPIO14(D5)
// The ED Module has pinheaders so any available PIN on the ESP8266 can be used.
// 
// HLW8012 IC works with 5VDC (it seems at 3.3V is not stable in reading)
//

//#ifdef PLUGIN_BUILD_TESTING

#include <HLW8012.h>
HLW8012 *Plugin_170_hlw;

#define PLUGIN_170
#define PLUGIN_ID_170        170
#define PLUGIN_170_DEBUG     true    //activate extra log info in the debug
#define PLUGIN_NAME_170       "Voltage & Current (AC) - HLW8012"
#define PLUGIN_VALUENAME1_170 "Voltage (V)"
#define PLUGIN_VALUENAME2_170 "Current (A)"
#define PLUGIN_VALUENAME3_170 "Active Power (W)"
#define PLUGIN_VALUENAME4_170 "Power Factor (%)"

//----------------- HLW8012 Default parameters --------------------------------------------------
// Set SEL_PIN to HIGH to sample current
// This is the case for Itead's Sonoff POW, where the SEL_PIN drives a transistor that pulls down
// the SEL pin in the HLW8012 when closed
#define HLW_CURRENT_MODE         HIGH

// These are the nominal values for the resistors in the circuit
#define HLW_CURRENT_RESISTOR       0.001
#define HLW_VOLTAGE_RESISTOR_UP    ( 5 * 470000 )   // Real: 2280k
#define HLW_VOLTAGE_RESISTOR_DOWN  ( 1000 )         // Real 1.009k
//-----------------------------------------------------------------------------------------------

byte StoredTaskIndex;

boolean Plugin_170(byte function, struct EventStruct *event, String& string)
{
  boolean success = false;

  switch (function)
  {
    case PLUGIN_DEVICE_ADD:
      {
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: Begin PLUGIN_DEVICE_ADD");
          addLog(LOG_LEVEL_INFO, log);
        }
        Device[++deviceCount].Number = PLUGIN_ID_170;
        Device[deviceCount].Type = DEVICE_TYPE_TRIPLE;
        Device[deviceCount].VType = SENSOR_TYPE_QUAD;
        Device[deviceCount].Ports = 0;
        Device[deviceCount].PullUpOption = false;
        Device[deviceCount].InverseLogicOption = false;
        Device[deviceCount].FormulaOption = true;
        Device[deviceCount].ValueCount = 4;
        Device[deviceCount].SendDataOption = true;
        Device[deviceCount].TimerOption = true;
        Device[deviceCount].GlobalSyncOption = false;
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: End PLUGIN_DEVICE_ADD");
          addLog(LOG_LEVEL_INFO, log);
        }
        break;
      }

    case PLUGIN_GET_DEVICENAME:
      {
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: Begin PLUGIN_GET_DEVICENAME");
          addLog(LOG_LEVEL_INFO, log);
        }
        string = F(PLUGIN_NAME_170);
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: End PLUGIN_GET_DEVICENAME");
          addLog(LOG_LEVEL_INFO, log);
        }
        break;
      }

    case PLUGIN_GET_DEVICEVALUENAMES:
      {
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: Begin PLUGIN_GET_DEVICEVALUENAMES");
          addLog(LOG_LEVEL_INFO, log);
        }
        strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_170));
        strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_170));
        strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_170));
        strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[3], PSTR(PLUGIN_VALUENAME4_170));
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: End PLUGIN_GET_DEVICEVALUENAMES");
          addLog(LOG_LEVEL_INFO, log);
        }
        break;
      }
      
    case PLUGIN_WEBFORM_LOAD:
      {
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: Begin PLUGIN_WEBFORM_LOAD");
          addLog(LOG_LEVEL_INFO, log);
        }
        addFormNote(string, F("Sonoff POW: 1st(SEL)=GPIO-5, 2nd(CF1)=GPIO-13, 3rd(CF)=GPIO-14"));
        addFormSubHeader(string, F("Calibration Values"));
        double hlwMultipliers[3];
        LoadCustomTaskSettings(event->TaskIndex, (byte*)&hlwMultipliers, sizeof(hlwMultipliers));
        addFormTextBox(string, F("Current Multiplier"), F("plugin_170_currmult"), String(hlwMultipliers[0], 2), 25);
        addFormTextBox(string, F("Voltage Multiplier"), F("plugin_170_voltmult"), String(hlwMultipliers[1], 2), 25);
        addFormTextBox(string, F("Power Multiplier"),   F("plugin_170_powmult"),  String(hlwMultipliers[2], 2), 25);
        success = true;
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: End PLUGIN_WEBFORM_LOAD");
          addLog(LOG_LEVEL_INFO, log);
        }
        break;
      }

    case PLUGIN_WEBFORM_SAVE:
      {
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: Begin PLUGIN_WEBFORM_SAVE");
          addLog(LOG_LEVEL_INFO, log);
        }
        double hlwMultipliers[3];
        String tmpString, arg1;
          arg1 = F("plugin_170_currmult"); tmpString = WebServer.arg(arg1);
          hlwMultipliers[0] = atof(tmpString.c_str());
          arg1 = F("plugin_170_voltmult"); tmpString = WebServer.arg(arg1);
          hlwMultipliers[1] = atof(tmpString.c_str());
          arg1 = F("plugin_170_powmult");  tmpString = WebServer.arg(arg1);
          hlwMultipliers[2] = atof(tmpString.c_str());
        SaveCustomTaskSettings(event->TaskIndex, (byte*)&hlwMultipliers, sizeof(hlwMultipliers));
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: Saved Calibration from Config Page");
          addLog(LOG_LEVEL_INFO, log);
        }
        Plugin_170_hlw->setCurrentMultiplier(hlwMultipliers[0]);
        Plugin_170_hlw->setVoltageMultiplier(hlwMultipliers[1]);
        Plugin_170_hlw->setPowerMultiplier(hlwMultipliers[2]);
        success = true;
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: End PLUGIN_WEBFORM_SAVE");
          addLog(LOG_LEVEL_INFO, log);
        }
        break;
      }

    case PLUGIN_READ:
      {
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: Begin PLUGIN_READ");
          addLog(LOG_LEVEL_INFO, log);
        }
        Plugin_170_hlw->setMode(MODE_CURRENT); delay(200); double       hcurrent  = Plugin_170_hlw->getCurrent();
        Plugin_170_hlw->setMode(MODE_VOLTAGE); delay(200); unsigned int hvoltage  = Plugin_170_hlw->getVoltage();
        unsigned int hpower    = Plugin_170_hlw->getActivePower();
        //unsigned int happpower = Plugin_170_hlw->getApparentPower();
        unsigned int hpowfact  = (int) (100 * Plugin_170_hlw->getPowerFactor());
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: Read values");
          log += F(" - V="); log += hvoltage; 
          log += F(" - A="); log += hcurrent;
          log += F(" - W="); log += hpower;
          log += F(" - Pf%="); log += hpowfact;
          addLog(LOG_LEVEL_INFO, log);
        }
        UserVar[event->BaseVarIndex]     = hvoltage;
        UserVar[event->BaseVarIndex + 1] = hcurrent;
        UserVar[event->BaseVarIndex + 2] = hpower;
        UserVar[event->BaseVarIndex + 3] = hpowfact;
        //Plugin_170_hlw->toggleMode();
        success = true;
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: End PLUGIN_READ");
          addLog(LOG_LEVEL_INFO, log);
        }
        break;
      }

    case PLUGIN_INIT:
      {
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: Begin PLUGIN_INIT");
          addLog(LOG_LEVEL_INFO, log);
        }
        if (!Plugin_170_hlw)
        {
          Plugin_170_hlw = new HLW8012;
          // This initializes the HWL8012 library.
          Plugin_170_hlw->begin(Settings.TaskDevicePin3[event->TaskIndex], Settings.TaskDevicePin2[event->TaskIndex], Settings.TaskDevicePin1[event->TaskIndex], HLW_CURRENT_MODE, false, 1000000);     
          if (PLUGIN_170_DEBUG) addLog(LOG_LEVEL_INFO, F("HLW8012: Init object done"));
          Plugin_170_hlw->setResistors(HLW_CURRENT_RESISTOR, HLW_VOLTAGE_RESISTOR_UP, HLW_VOLTAGE_RESISTOR_DOWN);
          if (PLUGIN_170_DEBUG) addLog(LOG_LEVEL_INFO, F("HLW8012: Init Basic Resistor Values done"));
          // If multipliers are empty load default ones and save all of them as "CustomTaskSettings"
          double hlwMultipliers[3];
          LoadCustomTaskSettings(event->TaskIndex, (byte*)&hlwMultipliers, sizeof(hlwMultipliers));
            if (hlwMultipliers[0] == 0) { hlwMultipliers[0] = Plugin_170_hlw->getCurrentMultiplier(); }
            if (hlwMultipliers[1] == 0) { hlwMultipliers[1] = Plugin_170_hlw->getVoltageMultiplier(); }
            if (hlwMultipliers[2] == 0) { hlwMultipliers[2] = Plugin_170_hlw->getPowerMultiplier();   }
          SaveCustomTaskSettings(event->TaskIndex, (byte*)&hlwMultipliers, sizeof(hlwMultipliers));
          if (PLUGIN_170_DEBUG) addLog(LOG_LEVEL_INFO, F("HLW8012: Saved Calibration after INIT"));
          StoredTaskIndex = event->TaskIndex; // store task index value in order to use it in the PLUGIN_WRITE routine
        }
        success = true;
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: End PLUGIN_INIT");
          addLog(LOG_LEVEL_INFO, log);
        }
        break;
      }

    case PLUGIN_WRITE:
      {
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: Begin PLUGIN_WRITE");
          addLog(LOG_LEVEL_INFO, log);
        }
        if (Plugin_170_hlw)
        {
          String tmpString  = string;
          int argIndex = tmpString.indexOf(',');
          if (argIndex)
            tmpString = tmpString.substring(0, argIndex);

          if (tmpString.equalsIgnoreCase(F("hlwreset")))
          {
            Plugin_170_hlw->resetMultipliers();
            Plugin170_SaveMultipliers();
            if (PLUGIN_170_DEBUG) addLog(LOG_LEVEL_INFO, F("HLW8012: Reset Multipliers to DEFAULT"));
            success = true;
          }
          
          if (tmpString.equalsIgnoreCase(F("hlwcalibrate")))
          {
            String tmpStr = string;
            unsigned int CalibVolt = 0;
            double       CalibCurr = 0;
            unsigned int CalibAcPwr = 0;
            int comma1 = tmpStr.indexOf(',');
            int comma2 = tmpStr.indexOf(',', comma1+1);
            int comma3 = tmpStr.indexOf(',', comma2+1);
            if (comma1 != 0) {
              if (comma2 == 0) { 
                CalibVolt  = tmpStr.substring(comma1+1).toInt();
              } else if (comma3 == 0) {  
                CalibVolt  = tmpStr.substring(comma1+1, comma2).toInt();
                CalibCurr  = atof(tmpStr.substring(comma2+1).c_str());
              } else {
                CalibVolt  = tmpStr.substring(comma1+1, comma2).toInt();
                CalibCurr  = atof(tmpStr.substring(comma2+1, comma3).c_str());
                CalibAcPwr = tmpStr.substring(comma3+1).toInt();
              }
            }
            if (PLUGIN_170_DEBUG) {
              String log = F("HLW8012: Calibration to values");
              log += F(" - Expected-V="); log += CalibVolt; 
              log += F(" - Expected-A="); log += CalibCurr;
              log += F(" - Expected-W="); log += CalibAcPwr;
              addLog(LOG_LEVEL_INFO, log);
            }
            if (CalibVolt  != 0) { Plugin_170_hlw->expectedVoltage(CalibVolt); }
            if (CalibCurr  != 0) { Plugin_170_hlw->expectedCurrent(CalibCurr); }
            if (CalibAcPwr != 0) { Plugin_170_hlw->expectedActivePower(CalibAcPwr); }
            // if at least one calibration value has been provided then save the new multipliers //
            if ((CalibVolt + CalibCurr + CalibAcPwr) != 0) { Plugin170_SaveMultipliers(); }
            success = true;  
          }
        }
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: End PLUGIN_WRITE");
          addLog(LOG_LEVEL_INFO, log);
        }
        break;
      }
      
  }
  return success;
}

void Plugin170_SaveMultipliers() {
    if (PLUGIN_170_DEBUG) {
      String log = F("HLW8012: Begin Plugin170_SaveMultipliers");
      addLog(LOG_LEVEL_INFO, log);
    }
    double hlwMultipliers[3];
    hlwMultipliers[0] = Plugin_170_hlw->getCurrentMultiplier();
    hlwMultipliers[1] = Plugin_170_hlw->getVoltageMultiplier();
    hlwMultipliers[2] = Plugin_170_hlw->getPowerMultiplier();
    SaveCustomTaskSettings(StoredTaskIndex, (byte*)&hlwMultipliers, sizeof(hlwMultipliers));  
    if (PLUGIN_170_DEBUG) {
      String log = F("HLW8012: End Plugin170_SaveMultipliers");
      addLog(LOG_LEVEL_INFO, log);
    }
}

//#endif

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#87 Post by LisaM » 08 Jul 2017, 01:30

It looks like something in here is horribly wrong:

Code: Select all

        SaveCustomTaskSettings(event->TaskIndex, (byte*)&hlwMultipliers, sizeof(hlwMultipliers));
It causes the function to abort, i suspect that the reboot is due to the fact that you're saving a local function variable pointer in a global area...

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#88 Post by papperone » 08 Jul 2017, 08:51

LisaM wrote: 08 Jul 2017, 01:30 It looks like something in here is horribly wrong:

Code: Select all

        SaveCustomTaskSettings(event->TaskIndex, (byte*)&hlwMultipliers, sizeof(hlwMultipliers));
It causes the function to abort, i suspect that the reboot is due to the fact that you're saving a local function variable pointer in a global area...
this is the official way to save variables in the SPIFF file; it's been used by various plugins (OLED, LCD, etc) and I was pointed to use that due to the fact I need "DOUBLE" variable instead of "FLOAT".
so maybe it's not my plugin broken but the ESPEasy function "SaveCustomTaskSettings"; I will have a deeper look at the function to try to understand.

PS: can you try to remote the HLW device, confirm it's not crashing, then add OLED or LCD plugin and confirm it crashes back? this will make 100% sure it's related to the above function...
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#89 Post by LisaM » 08 Jul 2017, 14:43

Code rewritten:

Code: Select all

        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: PLUGIN_WEBFORM_SAVE - SaveCustomTaskSettings size: ");
          log += sizeof(ghlwMultipliers);
          addLog(LOG_LEVEL_INFO, log);
        }
        SaveCustomTaskSettings(event->TaskIndex, (byte*)&ghlwMultipliers, sizeof(ghlwMultipliers));
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: Saved Calibration from Config Page");
          addLog(LOG_LEVEL_INFO, log);
        }
Result:
Unit: 5 : HLW8012: PLUGIN_WEBFORM_SAVE - SaveCustomTaskSettings size: 24
Unit: 5 : WIFI : Connected! IP: 192.168.178.89
So it really reboots after calling SaveCustomTaskSettings. I have renamed hlwMultipliers to ghlwMultipliers and moved to line 46:

Code: Select all

  boolean success = false;
  double ghlwMultipliers[3];
Didn't help either...

Flashed with both 1M/256K and 4M/1M, both reboot... :evil:

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#90 Post by LisaM » 08 Jul 2017, 14:52

papperone wrote: 08 Jul 2017, 08:51 PS: can you try to remote the HLW device, confirm it's not crashing, then add OLED or LCD plugin and confirm it crashes back? this will make 100% sure it's related to the above function...
It's working fine until i send the HLW8012 form...

Changing nothing, so exactly the same firmware, i can save the oled (p023) webform:
oled.png
oled.png (86.28 KiB) Viewed 42549 times
devices.png
devices.png (90.08 KiB) Viewed 42549 times
So, the OLED code is working:

Code: Select all

SaveCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemplate, sizeof(deviceTemplate));
Maybe it's a bug in function SaveCustomTaskSettings when handling doubles?
The oled is using chars:

Code: Select all

        char deviceTemplate[8][64];

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#91 Post by LisaM » 08 Jul 2017, 15:44

Changed the function to this:

Code: Select all

        char hlwMultipliers[3][32];
		
        String tmpString, arg1;
          arg1 = F("plugin_170_currmult"); tmpString = WebServer.arg(arg1);
          strncpy(hlwMultipliers[0], tmpString.c_str(), sizeof(hlwMultipliers[0])-1);
          arg1 = F("plugin_170_voltmult"); tmpString = WebServer.arg(arg1);
          strncpy(hlwMultipliers[1], tmpString.c_str(), sizeof(hlwMultipliers[1])-1);
          arg1 = F("plugin_170_powmult");  tmpString = WebServer.arg(arg1);
          strncpy(hlwMultipliers[2], tmpString.c_str(), sizeof(hlwMultipliers[2])-1);
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: PLUGIN_WEBFORM_SAVE - SaveCustomTaskSettings size: ");
          log += sizeof(hlwMultipliers);
          addLog(LOG_LEVEL_INFO, log);
        }
        SaveCustomTaskSettings(event->TaskIndex, (byte*)&hlwMultipliers, sizeof(hlwMultipliers));
        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: Saved Calibration from Config Page");
          addLog(LOG_LEVEL_INFO, log);
        }
        Plugin_170_hlw->setCurrentMultiplier(atof(hlwMultipliers[0]));
        Plugin_170_hlw->setVoltageMultiplier(atof(hlwMultipliers[1]));
        Plugin_170_hlw->setPowerMultiplier(atof(hlwMultipliers[2]));
        success = true;
        break;
Same result:
Unit: 5 : HLW8012: PLUGIN_WEBFORM_SAVE - SaveCustomTaskSettings size: 96
And then reboot

Function SaveCustomTaskSettings (misc.ino) :

Code: Select all

/********************************************************************************************\
  Save Custom Task settings to SPIFFS
  \*********************************************************************************************/
boolean SaveCustomTaskSettings(int TaskIndex, byte* memAddress, int datasize)
{
  if (datasize > DAT_TASKS_SIZE)
    return false;
  return SaveToFile((char*)"config.dat", DAT_OFFSET_TASKS + (TaskIndex * DAT_TASKS_SIZE) + DAT_TASKS_CUSTOM_OFFSET, memAddress, datasize);
}
Function SaveToFile (misc.ino):

Code: Select all

/********************************************************************************************\
  Save data into config file on SPIFFS
  \*********************************************************************************************/
boolean SaveToFile(char* fname, int index, byte* memAddress, int datasize)
{
  boolean success = false;

  if (RTC.flashDayCounter > MAX_FLASHWRITES_PER_DAY)
  {
    String log = F("FS   : Daily flash write rate exceeded!");
    addLog(LOG_LEVEL_ERROR, log);
    return false;
  }

  fs::File f = SPIFFS.open(fname, "r+");
  if (f)
  {
    f.seek(index, fs::SeekSet);
    byte *pointerToByteToSave = memAddress;
    for (int x = 0; x < datasize ; x++)
    {
      f.write(*pointerToByteToSave);
      pointerToByteToSave++;
    }
    f.close();
    String log = F("FILE : File saved");
    addLog(LOG_LEVEL_INFO, log);
    success = true;
    flashCount();
  }
  return success;
}
It never gets here:
String log = F("FILE : File saved");
addLog(LOG_LEVEL_INFO, log);

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#92 Post by papperone » 08 Jul 2017, 16:06

Didnt' get yet home to do further test/checks but as that function does work on a 4Mb ESP8266 and on a WemosD1 Mini (which is as well 4Mb) I can only think something is related to POW unit.
The function "SaveCustomTaskSettings" works with pointer and size (in bytes) so it shouldn't matter the original data type.
It's weird that in the OLED plugin it does work but not on the HLW8012 one.

I hope some of the main developpers can step in to help to understand!!
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#93 Post by papperone » 08 Jul 2017, 16:08

PS: I saw you put just 5 characters on the OLED text, can you please try to put like 30 characters on each line and try to save it ?
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#94 Post by LisaM » 08 Jul 2017, 16:17

Changed the code for more info:

Code: Select all

        if (PLUGIN_170_DEBUG) {
          String log = F("HLW8012: PLUGIN_WEBFORM_SAVE - SaveCustomTaskSettings taskindex: ");
          log += event->TaskIndex;
          addLog(LOG_LEVEL_INFO, log);
          String log2 = F("HLW8012: PLUGIN_WEBFORM_SAVE - SaveCustomTaskSettings hlwMultipliers: ");
          log2 += hlwMultipliers[0];log2 += F(":");log2 += hlwMultipliers[1];log2 += F(":");log2 += hlwMultipliers[2];
          addLog(LOG_LEVEL_INFO, log2);
          String log3 = F("HLW8012: PLUGIN_WEBFORM_SAVE - SaveCustomTaskSettings size: ");
          log3 += sizeof(hlwMultipliers);
          addLog(LOG_LEVEL_INFO, log3);
        }
The results:
HLW8012: PLUGIN_WEBFORM_SAVE - SaveCustomTaskSettings taskindex: 0
HLW8012: PLUGIN_WEBFORM_SAVE - SaveCustomTaskSettings hlwMultipliers: 10343611.74:14484.49:408636.51
HLW8012: PLUGIN_WEBFORM_SAVE - SaveCustomTaskSettings size: 96
Everything looks ok...
Still reboot
Last edited by LisaM on 08 Jul 2017, 16:22, edited 1 time in total.

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#95 Post by LisaM » 08 Jul 2017, 16:20

papperone wrote: 08 Jul 2017, 16:08 PS: I saw you put just 5 characters on the OLED text, can you please try to put like 30 characters on each line and try to save it ?
oled test30.JPG
oled test30.JPG (152.33 KiB) Viewed 42622 times
oled30.jpg
oled30.jpg (121.48 KiB) Viewed 42621 times

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#96 Post by papperone » 10 Jul 2017, 17:41

I think I've found the bug, which was my fault and not the "SaveCustomSettings" function :shock:
PullRequest submitted, please let me know if now it works as it shoudl :)
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#97 Post by LisaM » 12 Jul 2017, 00:05

papperone wrote: 10 Jul 2017, 17:41 I think I've found the bug, which was my fault and not the "SaveCustomSettings" function :shock:
PullRequest submitted, please let me know if now it works as it shoudl :)
Congrats! It's working flawless now:
pow.JPG
pow.JPG (135.1 KiB) Viewed 42517 times
Although the value is off, since it should be around 230V... No load attached though...

The used multipliers:
Current Multiplier: 10343611.74
Voltage Multiplier: 14484.49
Power Multiplier: 408636.51
Are these the right ones?

Log entries:
1820292 : HLW8012: Read values - V=7 - A=0.00 - W=0 - Pf%=0
1820293 : EVENT: Pomp#Voltage (V)=7.00
1820314 : EVENT: Pomp#Current (A)=0.00
1820330 : EVENT: Pomp#Active Power (W)=0.00
1820347 : EVENT: Pomp#Power Factor (%)=0.00
I'm currently using a custom sensor, but the syslog shows an error on the receiving side (domoticz):
MQTT: Topic: domoticz/in, Message: {"idx":1172}
Error: MQTT: Invalid data received!
Looks like the power usage data is not sent...

Looking at the code:

Code: Select all

        UserVar[event->BaseVarIndex]     = hvoltage;
        UserVar[event->BaseVarIndex + 1] = hcurrent;
        UserVar[event->BaseVarIndex + 2] = hpower;
        UserVar[event->BaseVarIndex + 3] = hpowfact;
Shouldn't this be?:

Code: Select all

        UserVar[event->BaseVarIndex]     = (float) hvoltage;
        UserVar[event->BaseVarIndex + 1] = (float) hcurrent;
        UserVar[event->BaseVarIndex + 2] = (float) hpower;
        UserVar[event->BaseVarIndex + 3] = (float) hpowfact;

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#98 Post by papperone » 12 Jul 2017, 07:32

good to hear reboot issue is confirmed fixed!!
about your points:

1) Even the ElectroDragon reads around 200/210 without calibration and default multipliers
2) You did not have controller setup for the AC Current sensor plugin so it's normal data are not sent out

In order to calibrate I do as follows:
- I connect a known load (a bulb, either filament or LED)
- I measure the real voltaga (V) with a DVM (multimeter) an current (A) with an Amperometric Clamp
- I run then when load is ON the calibration command "hlwcalibrate,VVV,AAA,WWW" where VVV is voltage measured before, AAA is current measured before, WWW is nominal bulb power in Watts (as written on the bulb)

This will recalculate the multipliers automatically with that given load and since then the readings are more accurate and realistic, even thou time to time the HLW8012 chip gives some wrong readings...

Last point is correct, the readings are not "doubles" variables, only the multipliers are.
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

LisaM
Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#99 Post by LisaM » 12 Jul 2017, 09:03

papperone wrote: 12 Jul 2017, 07:32 good to hear reboot issue is confirmed fixed!!
about your points:

1) Even the ElectroDragon reads around 200/210 without calibration and default multipliers
2) You did not have controller setup for the AC Current sensor plugin so it's normal data are not sent out

In order to calibrate I do as follows:
- I connect a known load (a bulb, either filament or LED)
- I measure the real voltaga (V) with a DVM (multimeter) an current (A) with an Amperometric Clamp
- I run then when load is ON the calibration command "hlwcalibrate,VVV,AAA,WWW" where VVV is voltage measured before, AAA is current measured before, WWW is nominal bulb power in Watts (as written on the bulb)

This will recalculate the multipliers automatically with that given load and since then the readings are more accurate and realistic, even thou time to time the HLW8012 chip gives some wrong readings...

Last point is correct, the readings are not "doubles" variables, only the multipliers are.
I don't have a clamp, but do have two multimeters. I'll put both of them to work to measure (V) and (I) at the same time... A 60W light bulb should do the trick being the load.

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Support for "Sonoff Pow" (from ITead) with Power Consumption Measurement via HLW8012 (HLW 8012) IC?

#100 Post by papperone » 12 Jul 2017, 09:54

LisaM wrote: 12 Jul 2017, 09:03I don't have a clamp, but do have two multimeters. I'll put both of them to work to measure (V) and (I) at the same time... A 60W light bulb should do the trick being the load.
surely you can, I do have a clamp and it's easier :) but you can do it with 2 multimeter of course.
I tested with both 100W filament and 12W LED, I then used the calibration done with LED bulb as the W readings are more stable.
the library also calculate VA power but as it's a simple V*A I decided to report apparent power (W) and if someone needs VA power it can always be calculated externally.

I don't know if I can report more than 4 readings and let user to choose what to see, will check if this is possible.
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 29 guests