From e75c9451cf3c46797cb7ee014c79eba3c779e3ca Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Fri, 8 Nov 2024 20:09:32 +0100 Subject: [PATCH] mathieucarbou/MycilaPZEM004Tv3 @ 5.0.0 --- lib/MycilaRouter/MycilaRouterOutput.cpp | 20 ++--- platformio.ini | 5 +- src/Utils.cpp | 4 +- src/Website.cpp | 4 +- src/init/Events.cpp | 12 +-- src/main.cpp | 2 +- src/tasks/PZEM.cpp | 104 ++++++++++++------------ 7 files changed, 76 insertions(+), 75 deletions(-) diff --git a/lib/MycilaRouter/MycilaRouterOutput.cpp b/lib/MycilaRouter/MycilaRouterOutput.cpp index 99a65d80..e0918a82 100644 --- a/lib/MycilaRouter/MycilaRouterOutput.cpp +++ b/lib/MycilaRouter/MycilaRouterOutput.cpp @@ -284,7 +284,7 @@ void Mycila::RouterOutput::applyAutoBypass() { void Mycila::RouterOutput::getDimmerMetrics(Metrics& metrics, float gridVoltage) const { metrics.resistance = config.calibratedResistance; metrics.voltage = gridVoltage; - metrics.energy = _pzem->getEnergy(); + metrics.energy = _pzem->data.activeEnergy; const float dutyCycle = _dimmer->getDutyCycle(); const float maxPower = metrics.resistance == 0 ? 0 : metrics.voltage * metrics.voltage / metrics.resistance; metrics.power = dutyCycle * maxPower; @@ -298,16 +298,16 @@ void Mycila::RouterOutput::getDimmerMetrics(Metrics& metrics, float gridVoltage) bool Mycila::RouterOutput::getMeasurements(Metrics& metrics) const { if (!_pzem->isConnected()) return false; - metrics.voltage = _pzem->getVoltage(); - metrics.energy = _pzem->getEnergy(); + metrics.voltage = _pzem->data.voltage; + metrics.energy = _pzem->data.activeEnergy; if (getState() == State::OUTPUT_ROUTING) { - metrics.apparentPower = abs(_pzem->getApparentPower()); - metrics.current = abs(_pzem->getCurrent()); - metrics.dimmedVoltage = abs(_pzem->getDimmedVoltage()); - metrics.power = abs(_pzem->getPower()); - metrics.powerFactor = abs(_pzem->getPowerFactor()); - metrics.resistance = abs(_pzem->getResistance()); - metrics.thdi = abs(_pzem->getTHDi(0)); + metrics.apparentPower = abs(_pzem->data.apparentPower); + metrics.current = abs(_pzem->data.current); + metrics.dimmedVoltage = abs(_pzem->data.dimmedVoltage()); + metrics.power = abs(_pzem->data.activePower); + metrics.powerFactor = abs(_pzem->data.powerFactor); + metrics.resistance = abs(_pzem->data.resistance()); + metrics.thdi = abs(_pzem->data.thdi(0)); } return true; } diff --git a/platformio.ini b/platformio.ini index d47169b0..f3c7ea0d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -68,12 +68,13 @@ lib_deps = mathieucarbou/MycilaESPConnect @ 6.1.0 mathieucarbou/MycilaEasyDisplay @ 3.0.1 mathieucarbou/MycilaHADiscovery @ 3.0.0 - mathieucarbou/MycilaJSY @ 11.1.0 + mathieucarbou/MycilaJSY @ 11.1.1 mathieucarbou/MycilaLogger @ 3.2.0 mathieucarbou/MycilaMQTT @ 4.2.2 mathieucarbou/MycilaNTP @ 5.0.1 mathieucarbou/MycilaPulseAnalyzer @ 2.3.6 - mathieucarbou/MycilaPZEM004Tv3 @ 4.0.7 + ; https://github.com/mathieucarbou/MycilaPulseAnalyzer + mathieucarbou/MycilaPZEM004Tv3 @ 5.0.0 mathieucarbou/MycilaRelay @ 4.0.1 mathieucarbou/MycilaSystem @ 3.1.0 mathieucarbou/MycilaTaskManager @ 3.1.2 diff --git a/src/Utils.cpp b/src/Utils.cpp index 675b7d2f..54006320 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -13,11 +13,11 @@ float detectGridFrequency() { if (frequency) return frequency; - frequency = round(pzemO1.getFrequency()); + frequency = round(pzemO1.data.frequency); if (frequency) return frequency; - frequency = round(pzemO2.getFrequency()); + frequency = round(pzemO2.data.frequency); if (frequency) return frequency; diff --git a/src/Website.cpp b/src/Website.cpp index c791cef9..e507a1bf 100644 --- a/src/Website.cpp +++ b/src/Website.cpp @@ -909,10 +909,10 @@ void YaSolR::WebsiteClass::updateCards() { _status(_mqtt, KEY_ENABLE_MQTT, mqtt.isEnabled(), mqtt.isConnected(), mqtt.getLastError() ? mqtt.getLastError() : YASOLR_LBL_113); _status(_output1Dimmer, KEY_ENABLE_OUTPUT1_DIMMER, dimmerO1.isEnabled(), pulseAnalyzer.isOnline(), pulseAnalyzer.isEnabled() ? YASOLR_LBL_110 : YASOLR_LBL_179); _status(_output1DS18, KEY_ENABLE_OUTPUT1_DS18, ds18O1.isEnabled(), ds18O1.getLastTime() > 0, YASOLR_LBL_114); - _status(_output1PZEM, KEY_ENABLE_OUTPUT1_PZEM, pzemO1.isEnabled(), pzemO1.isConnected() && pzemO1.readAddress() == YASOLR_PZEM_ADDRESS_OUTPUT1, pzemO1.isConnected() ? YASOLR_LBL_180 : YASOLR_LBL_110); + _status(_output1PZEM, KEY_ENABLE_OUTPUT1_PZEM, pzemO1.isEnabled(), pzemO1.isConnected() && pzemO1.getDeviceAddress() == YASOLR_PZEM_ADDRESS_OUTPUT1, pzemO1.isConnected() ? YASOLR_LBL_180 : YASOLR_LBL_110); _status(_output2Dimmer, KEY_ENABLE_OUTPUT2_DIMMER, dimmerO2.isEnabled(), pulseAnalyzer.isOnline(), pulseAnalyzer.isEnabled() ? YASOLR_LBL_110 : YASOLR_LBL_179); _status(_output2DS18, KEY_ENABLE_OUTPUT2_DS18, ds18O2.isEnabled(), ds18O2.getLastTime() > 0, YASOLR_LBL_114); - _status(_output2PZEM, KEY_ENABLE_OUTPUT2_PZEM, pzemO2.isEnabled(), pzemO2.isConnected() && pzemO2.readAddress() == YASOLR_PZEM_ADDRESS_OUTPUT2, pzemO2.isConnected() ? YASOLR_LBL_180 : YASOLR_LBL_110); + _status(_output2PZEM, KEY_ENABLE_OUTPUT2_PZEM, pzemO2.isEnabled(), pzemO2.isConnected() && pzemO2.getDeviceAddress() == YASOLR_PZEM_ADDRESS_OUTPUT2, pzemO2.isConnected() ? YASOLR_LBL_180 : YASOLR_LBL_110); _status(_routerDS18, KEY_ENABLE_DS18_SYSTEM, ds18Sys.isEnabled(), ds18Sys.getLastTime() > 0, YASOLR_LBL_114); _status(_zcd, KEY_ENABLE_ZCD, pulseAnalyzer.isEnabled(), pulseAnalyzer.isOnline(), YASOLR_LBL_110); #endif diff --git a/src/init/Events.cpp b/src/init/Events.cpp index 1929b869..4a3f80e5 100644 --- a/src/init/Events.cpp +++ b/src/init/Events.cpp @@ -322,15 +322,15 @@ Mycila::Task initEventsTask("Init Events", [](void* params) { } }); - pzemO1.setCallback([](const Mycila::PZEMEventType eventType) { - if (eventType == Mycila::PZEMEventType::EVT_READ) { - grid.pzemVoltage().update(pzemO1.getVoltage()); + pzemO1.setCallback([](const Mycila::PZEM::EventType eventType) { + if (eventType == Mycila::PZEM::EventType::EVT_READ) { + grid.pzemVoltage().update(pzemO1.data.voltage); } }); - pzemO2.setCallback([](const Mycila::PZEMEventType eventType) { - if (eventType == Mycila::PZEMEventType::EVT_READ) { - grid.pzemVoltage().update(pzemO2.getVoltage()); + pzemO2.setCallback([](const Mycila::PZEM::EventType eventType) { + if (eventType == Mycila::PZEM::EventType::EVT_READ) { + grid.pzemVoltage().update(pzemO2.data.voltage); } }); diff --git a/src/main.cpp b/src/main.cpp index c0473cbc..782bae6c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -67,7 +67,7 @@ void setup() { assert( pioTaskManager.asyncStart(512 * 7, 1, 1, 100, true)); // NOLINT assert( coreTaskManager.asyncStart(512 * 7, 1, 1, 100, true)); // NOLINT assert( mqttTaskManager.asyncStart(512 * 7, 1, 1, 100, false)); // NOLINT - assert( pzemTaskManager.asyncStart(512 * 4, 5, 0, 100, true)); // NOLINT + assert( pzemTaskManager.asyncStart(512 * 6, 5, 0, 100, true)); // NOLINT assert(routingTaskManager.asyncStart(512 * 3, 5, 0, 100, true)); // NOLINT // STARTUP READY! diff --git a/src/tasks/PZEM.cpp b/src/tasks/PZEM.cpp index 8ce574bb..ef30133f 100644 --- a/src/tasks/PZEM.cpp +++ b/src/tasks/PZEM.cpp @@ -12,67 +12,67 @@ Mycila::Task pzemTask("PZEM", [](void* params) { }); Mycila::Task pzemO1PairingTask("PZEM Pairing 0x01", Mycila::TaskType::ONCE, [](void* params) { - logger.info(TAG, "Pairing connected PZEM to Output 1"); - if (pzemO1.isEnabled() && pzemO1.readAddress() == YASOLR_PZEM_ADDRESS_OUTPUT1) { - logger.warn(TAG, "PZEM already paired to Output 1"); - } else { - pzemO1.end(); - pzemO1.begin(YASOLR_PZEM_SERIAL, config.get(KEY_PIN_PZEM_RX).toInt(), config.get(KEY_PIN_PZEM_TX).toInt()); - switch (pzemO1.readAddress()) { - case MYCILA_PZEM_INVALID_ADDRESS: - logger.error(TAG, "Failed to pair PZEM to Output 1: make sure only PZEM of Output 1 is powered and connected to Serial RX/TX!"); - pzemO1.end(); - break; - case YASOLR_PZEM_ADDRESS_OUTPUT1: - logger.warn(TAG, "PZEM already paired to Output 1"); + logger.info(TAG, "Pairing connected PZEM to Output 1..."); + pzemO1.end(); + pzemO1.begin(YASOLR_PZEM_SERIAL, config.get(KEY_PIN_PZEM_RX).toInt(), config.get(KEY_PIN_PZEM_TX).toInt(), MYCILA_PZEM_ADDRESS_GENERAL); + switch (pzemO1.getDeviceAddress()) { + case YASOLR_PZEM_ADDRESS_OUTPUT1: + // already paired + if (!config.getBool(KEY_ENABLE_OUTPUT1_PZEM)) { + // stop PZEM if it was not enabled pzemO1.end(); - if (config.getBool(KEY_ENABLE_OUTPUT1_PZEM)) - pzemO1.begin(YASOLR_PZEM_SERIAL, config.get(KEY_PIN_PZEM_RX).toInt(), config.get(KEY_PIN_PZEM_TX).toInt(), YASOLR_PZEM_ADDRESS_OUTPUT1); - break; - default: - if (pzemO1.setAddress(YASOLR_PZEM_ADDRESS_OUTPUT1)) { - logger.info(TAG, "PZEM has been paired to Output 1"); - pzemO1.end(); - if (config.getBool(KEY_ENABLE_OUTPUT1_PZEM)) - pzemO1.begin(YASOLR_PZEM_SERIAL, config.get(KEY_PIN_PZEM_RX).toInt(), config.get(KEY_PIN_PZEM_TX).toInt(), YASOLR_PZEM_ADDRESS_OUTPUT1); - } else { - logger.error(TAG, "Failed to pair PZEM to Output 1: make sure only PZEM of Output 1 is powered and connected to Serial RX/TX!"); + } + logger.warn(TAG, "PZEM already paired to Output 1"); + break; + case MYCILA_PZEM_ADDRESS_UNKNOWN: + // no device found + pzemO1.end(); + logger.error(TAG, "Failed to pair PZEM to Output 1: make sure only PZEM of Output 1 is powered and connected to Serial RX/TX!"); + break; + default: + // found a device + if (pzemO1.setDeviceAddress(YASOLR_PZEM_ADDRESS_OUTPUT1)) { + if (!config.getBool(KEY_ENABLE_OUTPUT1_PZEM)) { + // stop PZEM if it was not enabled pzemO1.end(); } - break; - } + logger.info(TAG, "PZEM has been paired to Output 1"); + } else { + pzemO1.end(); + logger.error(TAG, "Failed to pair PZEM to Output 1: make sure only PZEM of Output 1 is powered and connected to Serial RX/TX!"); + } } }); Mycila::Task pzemO2PairingTask("PZEM Pairing 0x02", Mycila::TaskType::ONCE, [](void* params) { - logger.info(TAG, "Pairing connected PZEM to Output 2"); - if (pzemO2.isEnabled() && pzemO2.readAddress() == YASOLR_PZEM_ADDRESS_OUTPUT2) { - logger.warn(TAG, "PZEM already paired to Output 2"); - } else { - pzemO2.end(); - pzemO2.begin(YASOLR_PZEM_SERIAL, config.get(KEY_PIN_PZEM_RX).toInt(), config.get(KEY_PIN_PZEM_TX).toInt()); - switch (pzemO2.readAddress()) { - case MYCILA_PZEM_INVALID_ADDRESS: - logger.error(TAG, "Failed to pair PZEM to Output 2: make sure only PZEM of Output 2 is powered and connected to Serial RX/TX!"); - pzemO2.end(); - break; - case YASOLR_PZEM_ADDRESS_OUTPUT2: - logger.warn(TAG, "PZEM already paired to Output 2"); + logger.info(TAG, "Pairing connected PZEM to Output 2..."); + pzemO2.end(); + pzemO2.begin(YASOLR_PZEM_SERIAL, config.get(KEY_PIN_PZEM_RX).toInt(), config.get(KEY_PIN_PZEM_TX).toInt(), MYCILA_PZEM_ADDRESS_GENERAL); + switch (pzemO2.getDeviceAddress()) { + case YASOLR_PZEM_ADDRESS_OUTPUT2: + // already paired + if (!config.getBool(KEY_ENABLE_OUTPUT2_PZEM)) { + // stop PZEM if it was not enabled pzemO2.end(); - if (config.getBool(KEY_ENABLE_OUTPUT2_PZEM)) - pzemO2.begin(YASOLR_PZEM_SERIAL, config.get(KEY_PIN_PZEM_RX).toInt(), config.get(KEY_PIN_PZEM_TX).toInt(), YASOLR_PZEM_ADDRESS_OUTPUT2); - break; - default: - if (pzemO2.setAddress(YASOLR_PZEM_ADDRESS_OUTPUT2)) { - logger.info(TAG, "PZEM has been paired to Output 2"); - pzemO2.end(); - if (config.getBool(KEY_ENABLE_OUTPUT2_PZEM)) - pzemO2.begin(YASOLR_PZEM_SERIAL, config.get(KEY_PIN_PZEM_RX).toInt(), config.get(KEY_PIN_PZEM_TX).toInt(), YASOLR_PZEM_ADDRESS_OUTPUT2); - } else { - logger.error(TAG, "Failed to pair PZEM to Output 2: make sure only PZEM of Output 2 is powered and connected to Serial RX/TX!"); + } + logger.warn(TAG, "PZEM already paired to Output 2"); + break; + case MYCILA_PZEM_ADDRESS_UNKNOWN: + // no device found + pzemO2.end(); + logger.error(TAG, "Failed to pair PZEM to Output 2: make sure only PZEM of Output 2 is powered and connected to Serial RX/TX!"); + break; + default: + // found a device + if (pzemO2.setDeviceAddress(YASOLR_PZEM_ADDRESS_OUTPUT2)) { + if (!config.getBool(KEY_ENABLE_OUTPUT2_PZEM)) { + // stop PZEM if it was not enabled pzemO2.end(); } - break; - } + logger.info(TAG, "PZEM has been paired to Output 2"); + } else { + pzemO2.end(); + logger.error(TAG, "Failed to pair PZEM to Output 2: make sure only PZEM of Output 2 is powered and connected to Serial RX/TX!"); + } } });