Skip to content

Commit

Permalink
PZEM004T: settings & dev board (#1712)
Browse files Browse the repository at this point in the history
* PZEM004T: runtime port and address settings

* Add nodemcu-pzem004t test env

* pzEneTotal -> pzemEneTotal
  • Loading branch information
mcspr authored May 1, 2019
1 parent b12d6fd commit 62a2b9e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
29 changes: 20 additions & 9 deletions code/espurna/sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ void _sensorInitCommands() {
DEBUG_MSG_P(PSTR("[SENSOR] PZEM004T\n"));
for(unsigned char dev = init; dev < limit; 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());
}
terminalOK();
Expand Down Expand Up @@ -718,18 +718,26 @@ void _sensorLoad() {

#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();
#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);
#endif
sensor->setAddresses(PZEM004T_ADDRESSES);
}

// Read saved energy offset
unsigned char dev_count = sensor->getAddressesCount();
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);
}
_sensors.push_back(sensor);
Expand Down Expand Up @@ -1188,7 +1196,7 @@ void _sensorConfigure() {
unsigned char dev_count = sensor->getAddressesCount();
for(unsigned char dev = 0; dev < dev_count; dev++) {
sensor->resetEnergy(dev, 0);
delSetting("pzEneTotal", dev);
delSetting("pzemEneTotal", dev);
}
_sensorResetTS();
}
Expand Down Expand Up @@ -1366,6 +1374,9 @@ void sensorSetup() {
moveSetting("powerUnits", "pwrUnits");
moveSetting("energyUnits", "eneUnits");

// Update PZEM004T energy total across multiple devices
moveSettings("pzEneTotal", "pzemEneTotal");

// Load sensors
_sensorLoad();
_sensorInit();
Expand Down
17 changes: 17 additions & 0 deletions code/espurna/settings.ino
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,23 @@ void moveSetting(const char * from, const char * to) {
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) {
String value;
if (!Embedis::get(key, value)) value = String(defaultValue);
Expand Down
12 changes: 12 additions & 0 deletions code/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,18 @@ build_flags = ${common.build_flags_4m1m} -DTINKERMAN_RFM69GW -DNOWSAUTH
monitor_speed = 115200
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]
Expand Down

0 comments on commit 62a2b9e

Please sign in to comment.