Skip to content

Commit

Permalink
mathieucarbou/MycilaPZEM004Tv3 @ 5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Nov 8, 2024
1 parent 4c2b405 commit e75c945
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 75 deletions.
20 changes: 10 additions & 10 deletions lib/MycilaRouter/MycilaRouterOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/Website.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/init/Events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
104 changes: 52 additions & 52 deletions src/tasks/PZEM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
}
});

0 comments on commit e75c945

Please sign in to comment.