Browse Source

PZEM004T: settings & dev board (#1712)

* PZEM004T: runtime port and address settings

* Add nodemcu-pzem004t test env

* pzEneTotal -> pzemEneTotal
rules-rpn
Max Prokhorov 5 years ago
committed by GitHub
parent
commit
62a2b9e882
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 9 deletions
  1. +20
    -9
      code/espurna/sensor.ino
  2. +17
    -0
      code/espurna/settings.ino
  3. +12
    -0
      code/platformio.ini

+ 20
- 9
code/espurna/sensor.ino View File

@ -354,7 +354,7 @@ void _sensorInitCommands() {
DEBUG_MSG_P(PSTR("[SENSOR] PZEM004T\n")); DEBUG_MSG_P(PSTR("[SENSOR] PZEM004T\n"));
for(unsigned char dev = init; dev < limit; dev++) { for(unsigned char dev = init; dev < limit; dev++) {
float offset = pzem004t_sensor->resetEnergy(dev); float offset = pzem004t_sensor->resetEnergy(dev);
setSetting("pzEneTotal", dev, offset);
setSetting("pzemEneTotal", dev, offset);
DEBUG_MSG_P(PSTR("Device %d/%s - Offset: %s\n"), dev, pzem004t_sensor->getAddress(dev).c_str(), String(offset).c_str()); DEBUG_MSG_P(PSTR("Device %d/%s - Offset: %s\n"), dev, pzem004t_sensor->getAddress(dev).c_str(), String(offset).c_str());
} }
terminalOK(); terminalOK();
@ -718,18 +718,26 @@ void _sensorLoad() {
#if PZEM004T_SUPPORT #if PZEM004T_SUPPORT
{ {
String addresses = getSetting("pzemAddr", PZEM004T_ADDRESSES);
if (!addresses.length()) {
DEBUG_MSG_P(PSTR("[SENSOR] PZEM004T Error: no addresses are configured\n"));
return;
}
PZEM004TSensor * sensor = pzem004t_sensor = new PZEM004TSensor(); PZEM004TSensor * sensor = pzem004t_sensor = new PZEM004TSensor();
#if PZEM004T_USE_SOFT
sensor->setRX(PZEM004T_RX_PIN);
sensor->setTX(PZEM004T_TX_PIN);
#else
sensor->setAddresses(addresses.c_str());
if (getSetting("pzemSoft", PZEM004T_USE_SOFT).toInt() == 1) {
sensor->setRX(getSetting("pzemRX", PZEM004T_RX_PIN).toInt());
sensor->setTX(getSetting("pzemTX", PZEM004T_TX_PIN).toInt());
} else {
sensor->setSerial(& PZEM004T_HW_PORT); sensor->setSerial(& PZEM004T_HW_PORT);
#endif
sensor->setAddresses(PZEM004T_ADDRESSES);
}
// Read saved energy offset // Read saved energy offset
unsigned char dev_count = sensor->getAddressesCount(); unsigned char dev_count = sensor->getAddressesCount();
for(unsigned char dev = 0; dev < dev_count; dev++) { for(unsigned char dev = 0; dev < dev_count; dev++) {
float value = getSetting("pzEneTotal", dev, 0).toFloat();
float value = getSetting("pzemEneTotal", dev, 0).toFloat();
if (value > 0) sensor->resetEnergy(dev, value); if (value > 0) sensor->resetEnergy(dev, value);
} }
_sensors.push_back(sensor); _sensors.push_back(sensor);
@ -1188,7 +1196,7 @@ void _sensorConfigure() {
unsigned char dev_count = sensor->getAddressesCount(); unsigned char dev_count = sensor->getAddressesCount();
for(unsigned char dev = 0; dev < dev_count; dev++) { for(unsigned char dev = 0; dev < dev_count; dev++) {
sensor->resetEnergy(dev, 0); sensor->resetEnergy(dev, 0);
delSetting("pzEneTotal", dev);
delSetting("pzemEneTotal", dev);
} }
_sensorResetTS(); _sensorResetTS();
} }
@ -1366,6 +1374,9 @@ void sensorSetup() {
moveSetting("powerUnits", "pwrUnits"); moveSetting("powerUnits", "pwrUnits");
moveSetting("energyUnits", "eneUnits"); moveSetting("energyUnits", "eneUnits");
// Update PZEM004T energy total across multiple devices
moveSettings("pzEneTotal", "pzemEneTotal");
// Load sensors // Load sensors
_sensorLoad(); _sensorLoad();
_sensorInit(); _sensorInit();


+ 17
- 0
code/espurna/settings.ino View File

@ -103,6 +103,23 @@ void moveSetting(const char * from, const char * to) {
delSetting(from); delSetting(from);
} }
void moveSetting(const char * from, const char * to, unsigned int index) {
String value = getSetting(from, index, "");
if (value.length() > 0) setSetting(to, index, value);
delSetting(from, index);
}
void moveSettings(const char * from, const char * to) {
unsigned int index = 0;
while (index < 100) {
String value = getSetting(from, index, "");
if (value.length() == 0) break;
setSetting(to, index, value);
delSetting(from, index);
index++;
}
}
template<typename T> String getSetting(const String& key, T defaultValue) { template<typename T> String getSetting(const String& key, T defaultValue) {
String value; String value;
if (!Embedis::get(key, value)) value = String(defaultValue); if (!Embedis::get(key, value)) value = String(defaultValue);


+ 12
- 0
code/platformio.ini View File

@ -400,6 +400,18 @@ build_flags = ${common.build_flags_4m1m} -DTINKERMAN_RFM69GW -DNOWSAUTH
monitor_speed = 115200 monitor_speed = 115200
extra_scripts = ${common.extra_scripts} extra_scripts = ${common.extra_scripts}
[env:nodemcu-pzem004t]
platform = ${common.platform}
framework = ${common.framework}
board = ${common.board_4m}
board_build.flash_mode = ${common.flash_mode}
lib_deps = ${common.lib_deps}
lib_ignore = ${common.lib_ignore}
build_flags = ${common.build_flags_4m1m} -DNODEMCU_BASIC -DEBUG_SERIAL_SUPPORT=0 -DPZEM004T_SUPPORT=1 -DDISABLE_POSTMORTEM_STACKDUMP
upload_speed = ${common.upload_speed_fast}
monitor_speed = ${common.monitor_speed}
extra_scripts = ${common.extra_scripts}
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
[env:foxel-lightfox-dual] [env:foxel-lightfox-dual]


Loading…
Cancel
Save