xoseperez / espurna

Home automation firmware for ESP8266-based devices
http://tinkerman.cat
GNU General Public License v3.0
2.99k stars 636 forks source link

PZEM004T, V3.0 serial and libraries. #2277

Closed davebuk closed 4 years ago

davebuk commented 4 years ago

I've just ordered a V3 PZEM004T and plan to use it with a wemos d1. I was going to use the TX/RX pins as the entry in sensors.h says software serial is not working. I guess that actually isn't the case and I can set software serial support and choose my own GPIO pins?

Originally posted by @davebuk in https://github.com/xoseperez/espurna/issues/1583#issuecomment-643665930

As well as soft serial and hard serial settings, do the libraries that espurna uses work with the V3.0 boards?

mcspr commented 4 years ago

v3 is something different from the original pzem

https://github.com/mandulaj/PZEM-004T-v30 seems to implement the new active protocol, we can expose it as a sensor

davebuk commented 4 years ago

That would be great if you could. I believe it has a couple more magnitudes, frequency and maybe power factor, so they will need adding. I don't have the hardware yet, it'll be a week or so, but I can test it if you are happy to create the code?

mcspr commented 4 years ago

Sure, if you also don't mind testing a different settings backend (which might break) at the same time :)

It just a matter of

Power factor is an existing measurement type, not sure what frequency needs to be here as we don't have any sensor outputting that.

davebuk commented 4 years ago

Yep, I can test. It'll be connected to a wemos D1. I plan to add a 433MHz TX/RX to the board as well when it's up and running.

I found a page here that maybe of some use? There is a section with Arduino code examples. https://innovatorsguru.com/pzem-004t-v3/

davebuk commented 4 years ago

Hardware has arrived. I'll look to connect it all together over the weekend ready for some testing. It's the 100A version if that makes a difference to the expected output.

mcspr commented 4 years ago

Will add PR some time today then, what I tried with dummy data based on the brief 'datasheet' description of the protocol seemed to work OK (and at least Hardware Serial version). I did not use the library though, but I guess if that does not work you can try examples/ from there to understand any possible differences.

100A only changes some 'default' values from the sensor, should work fine from the software side. And I am not really sure what 'alarm' setting means (setAlarm() in the library, also see the .pdf), will it beep or something?

davebuk commented 4 years ago

I'll report testing in #2283

Re: 'alarm', from the data sheet I have with the device it says:

"1.7 Over power alarm Active power threshold can be set, when the measured active power exceeds the threshold, it can alarm"

You may already know this but the document shows Register address 0x0009 is Alarm status (0xFFFF - alarm, 0x0000 - alarm). The wattage trigger level can be set and when reached, the alarm register is set. I guess this could report as 0 or 1 to mqtt via one of the settings?

mcspr commented 4 years ago

You may already know this but the document shows Register address 0x0009 is Alarm status (0xFFFF - alarm, 0x0000 - alarm). The wattage trigger level can be set and when reached, the alarm register is set. I guess this could report as 0 or 1 to mqtt via one of the settings?

Yeah. But... why. When we receive data packet, we receive all at once and already have power value and can calculate things ourselves. I wonder if this is something related to the display module included in the other versions

davebuk commented 4 years ago

Agree ref the alarm status. There doesn't seem to be an immediate use case with espurna. Maybe if the sensor unit is running as a stand alone module, without input into an esp8266 style device, it would be of some use like the display module you suggested.

davebuk commented 4 years ago

I hope to try and build my "production" hardware over the weekend and connect to my house supply. It should then integrate into openHAB.

davebuk commented 4 years ago

I now have the hardware connected and the sensor works really well. Great work.

pzem final

dpeddi commented 4 years ago

@davebuk do you have a ttl converter between the wemos and the PZE? Please could you share the schematic you used?

davebuk commented 4 years ago

@dpeddi I don't have anything between the two devices. I have 5VDC, GND, Tx and Rx connected to the Wemos D1 mini. As long as have you have Tx/Rx the correct way round on the GPIOs you have you have configured in the espurna build, the firmware does all the serial communications.