-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sensor: Add PZEM004T V3.0 #2283
Conversation
re
Don't mind this. Will update down the line when settings are merged, Codacy wants to fix test framework. |
Also, in case TX and RX are connected to hardware serial, don't forget to add |
So that will allow hardware serial rather than using software serial on the wemos D1 mini? |
This... depends? If I remember correctly how d1 works, powering it through the 3v3 pin instead of USB should allow to use TX and RX pins. Easy to test with a terminal connection first? Otherwise, |
I have a basic wemos/sensor setup with the wemos running from a 240V to 5V supply on its USB port (not a PC/data enabled USB supply) and the sensor running from the 5V header pins. I built two versions, Software Serial and Hardware Serial as defined below.
Every 6 seconds (or 1 second if I change the refresh rate in the sensor webUI) I get: I get the same INFO at debug terminal gives:
|
Serial leds just see the raw signal :) idk if that really means the pzem gets any data. Only possible issues that I read of are 5v vs. 3v3 signals, since the the pzem board itself is supposed to be 5v, isn't it? Note the:
Idk how sound that advice is though. Does example software & hardware serial code from the https://github.com/mandulaj/PZEM-004T-v30/tree/master/examples work? [platformio]
src_dir = ./
[env:example]
platform = espressif8266
framework = arduino
board = d1_mini
lib_deps =
https://github.com/mandulaj/PZEM-004T-v30#d4590976
https://github.com/plerup/espsoftwareserial#3.4.1 Note that I also used the swserial library version that we use here, otherwise it will use a more recent one. In case swserial is somehow broken, that can also test this theory. But, we can't exactly mix them as PIO will be confused when building the source, .pio/libdeps/example can only have one of espsoftwareserial versions (i.e. you'd need to remove libdeps directory and remove the espsoftwareserial line from lib_deps entry). |
code/espurna/sensor.cpp
Outdated
// Core does not allow us to begin(baud, cfg, rx, tx) / pins(rx, tx) before begin(baud) | ||
// b/c internal UART handler does not exist yet | ||
// Also see https://github.com/esp8266/Arduino/issues/2380 as to why there is flush() | ||
if (getSetting("pzemv30HwsSwap", 1 == PZEM004TV30_HARDWARE_SERIAL_SWAP)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for debugging only, configuration is very confusing right now...
Maybe
- have
pzemv30Soft
0 or 1, like existingpzemSoft
, instead of this magic - make
pzemv30RX
andTX
checks for 13 and 15, 1 and 3 as valid HardwareSerial pins, don't do anything otherwise.
code/espurna/config/sensors.h
Outdated
#ifndef PZEM004TV30_USE_SOFT | ||
#define PZEM004TV30_USE_SOFT 0 // By default, use Hardware serial with GPIO15 (TX) and GPIO13 (RX) | ||
#endif | ||
|
||
#ifndef PZEM004TV30_RX_PIN | ||
#define PZEM004TV30_RX_PIN 13 // Serial RX GPIO (if PZEM004T_USE_SOFT == 1, creates a SoftwareSerial object) | ||
#endif | ||
|
||
#ifndef PZEM004TV30_TX_PIN | ||
#define PZEM004TV30_TX_PIN 15 // Serial TX GPIO (if PZEM004T_USE_SOFT == 1, creates a SoftwareSerial object) | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that flags have changed.
Default is HardwareSerial on 13 & 15
_ready = (_stream != nullptr); | ||
_last_reading = millis() - _update_interval; | ||
#if TERMINAL_SUPPORT | ||
terminalRegisterCommand(F("PZ.ADDRESS"), [](const terminal::CommandContext& ctx) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pz.address N
should change PZEM's address to N, where N is some number between 1 and 247 (per http://www.modbus.org/docs/Modbus_over_serial_line_V1_01.pdf)
PZEM pdf never mentioned 0 as broadcast, only 248 (0xF8), so it is unclear what exactly happens with numbers 0,248...255.
code/espurna/settings.cpp
Outdated
kvs_type kv_store( | ||
EepromStorage{}, | ||
#if DEBUG_SUPPORT | ||
EepromReservedSize + CrashReservedSize, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davebuk I also noticed a bug here with the current storage system. Settings get confused and 'hide' keys after a certain point, so kv might get lost accidentally when value length is the same as the previous one. Easy way to reproduce is to:
set one 1
set two 2
set one 2
get two
two
will get lost
code/espurna/settings_embedis.h
Outdated
// we also need to pad the space *after* the value | ||
// but, only when we still have some space left | ||
if ((start_pos - need) >= 2) { | ||
_cursor_set_position(writer.begin); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I thought this would be harder to find. Never-mind the previous workaround comment, can you please test this instead?
_cursor_set_position(writer.begin); | |
_cursor_set_position(writer.begin - _cursor.begin); |
Suggested 'test' still stands, key should not disappear.
Built using
gives With the suggested change in line 409, it now gives Energy reset also works. |
Thanks for testing. I'll need to update the test suite to check for this
condition and merge both PRs. But first, GitHub web needs to start working
again...
|
- fix crc byte order, comments - less assertive read loop - allow changing address - fix std::array comparison comparing all of the array - try to be nice with buffer sizes and iterators
@davebuk
Fix #2277
Add new V3.0 version of PZEM004T, not tested. Default to factory default address 0xf8, configurable through
pzemv30Addr
8-bit numberAdd 'frequency' measurement & 'hz' unit
Add
pz.address
that configures addressImplemented energy reset
Right now we only support a single device. No way to test this properly (mostly, how exactly we need to connect this stuff), so left as not implemented.