Skip to content

Commit

Permalink
mathieucarbou/MycilaJSY @ 11.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Oct 26, 2024
1 parent eee4548 commit b0cb919
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 47 deletions.
3 changes: 2 additions & 1 deletion include/YaSolRDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
// UDP communication

#define YASOLR_UDP_PORT 53964
#define YASOLR_UDP_MSG_TYPE_JSY_DATA 0x01
// #define YASOLR_UDP_MSG_TYPE_JSY_DATA 0x01 // old json
#define YASOLR_UDP_MSG_TYPE_JSY_DATA 0x02 // new json

// password configuration keys

Expand Down
47 changes: 36 additions & 11 deletions lib/MycilaRouter/MycilaRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,21 @@ void Mycila::Router::getMetrics(Metrics& metrics, float voltage) const {
metrics.resistance = metrics.current == 0 ? 0 : metrics.power / (metrics.current * metrics.current);
metrics.thdi = metrics.powerFactor == 0 ? 0 : sqrt(1 / pow(metrics.powerFactor, 2) - 1);

if (!metrics.energy)
metrics.energy = _jsy->getEnergy1() + _jsy->getEnergyReturned1();
if (!metrics.energy) {
switch (_jsy->data.model) {
case MYCILA_JSY_MK_163:
// JSY MK-163 only has 1 clamp and if connected is used to measure the grid so there is no way to measure the output
break;
case MYCILA_JSY_MK_194:
metrics.energy = _jsy->data.channel1().activeEnergy; // imported + exported
break;
case MYCILA_JSY_MK_333:
// JSY MK-333 only has 3 clamps to measure a 3-phase system (grid) so there is no way to measure the output
break;
default:
break;
}
}
}

void Mycila::Router::getMeasurements(Metrics& metrics) const {
Expand All @@ -88,15 +101,27 @@ void Mycila::Router::getMeasurements(Metrics& metrics) const {
metrics.thdi = metrics.powerFactor == 0 ? 0 : sqrt(1 / pow(metrics.powerFactor, 2) - 1);

} else if (_jsy->isConnected()) {
metrics.voltage = _jsy->getVoltage1();
metrics.energy = _jsy->getEnergy1() + _jsy->getEnergyReturned1();
if (routing) {
metrics.apparentPower = abs(_jsy->getApparentPower1());
metrics.current = abs(_jsy->getCurrent1());
metrics.power = abs(_jsy->getActivePower1());
metrics.powerFactor = abs(_jsy->getPowerFactor1());
metrics.resistance = abs(_jsy->getResistance1());
metrics.thdi = abs(_jsy->getTHDi1(0));
switch (_jsy->data.model) {
case MYCILA_JSY_MK_163:
// JSY MK-163 only has 1 clamp and if connected is used to measure the grid so there is no way to measure the output
break;
case MYCILA_JSY_MK_194:
metrics.voltage = _jsy->data.channel1().voltage;
metrics.energy = _jsy->data.channel1().activeEnergy; // imported + exported
if (routing) {
metrics.apparentPower = _jsy->data.channel1().apparentPower;
metrics.current = _jsy->data.channel1().current;
metrics.power = abs(_jsy->data.channel1().activePower);
metrics.powerFactor = _jsy->data.channel1().powerFactor;
metrics.resistance = _jsy->data.channel1().resistance();
metrics.thdi = _jsy->data.channel1().thdi();
}
break;
case MYCILA_JSY_MK_333:
// JSY MK-333 only has 3 clamps to measure a 3-phase system (grid) so there is no way to measure the output
break;
default:
break;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ lib_deps =
mathieucarbou/MycilaESPConnect @ 6.0.3
mathieucarbou/MycilaEasyDisplay @ 3.0.1
mathieucarbou/MycilaHADiscovery @ 3.0.0
mathieucarbou/MycilaJSY @ 10.1.0
; mathieucarbou/MycilaJSY @ 11.0.4
https://github.com/mathieucarbou/MycilaJSY/archive/refs/tags/v11.0.4.zip
mathieucarbou/MycilaLogger @ 3.2.0
mathieucarbou/MycilaMQTT @ 4.2.2
mathieucarbou/MycilaNTP @ 5.0.1
Expand Down
94 changes: 60 additions & 34 deletions src/init/Events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,30 +335,43 @@ Mycila::Task initEventsTask("Init Events", [](void* params) {

jsy.setCallback([](const Mycila::JSY::EventType eventType) {
if (eventType == Mycila::JSY::EventType::EVT_CHANGE) {
switch (jsy.getModel()) {
switch (jsy.data.model) {
case MYCILA_JSY_MK_163:
grid.localMetrics().update({
.apparentPower = jsy.getApparentPower1(),
.current = jsy.getCurrent1(),
.energy = jsy.getEnergy1(),
.energyReturned = jsy.getEnergyReturned1(),
.frequency = jsy.getFrequency(),
.power = jsy.getActivePower1(),
.powerFactor = jsy.getPowerFactor1(),
.voltage = jsy.getVoltage1(),
.apparentPower = jsy.data.single().apparentPower,
.current = jsy.data.single().current,
.energy = jsy.data.single().activeEnergyImported,
.energyReturned = jsy.data.single().activeEnergyReturned,
.frequency = jsy.data.frequency,
.power = jsy.data.single().activePower,
.powerFactor = jsy.data.single().powerFactor,
.voltage = jsy.data.single().voltage,
});
break;

case MYCILA_JSY_MK_194:
grid.localMetrics().update({
.apparentPower = jsy.getApparentPower2(),
.current = jsy.getCurrent2(),
.energy = jsy.getEnergy2(),
.energyReturned = jsy.getEnergyReturned2(),
.frequency = jsy.getFrequency(),
.power = jsy.getActivePower2(),
.powerFactor = jsy.getPowerFactor2(),
.voltage = jsy.getVoltage2(),
.apparentPower = jsy.data.channel2().apparentPower,
.current = jsy.data.channel2().current,
.energy = jsy.data.channel2().activeEnergyImported,
.energyReturned = jsy.data.channel2().activeEnergyReturned,
.frequency = jsy.data.frequency,
.power = jsy.data.channel2().activePower,
.powerFactor = jsy.data.channel2().powerFactor,
.voltage = jsy.data.channel2().voltage,
});
break;

case MYCILA_JSY_MK_333:
grid.localMetrics().update({
.apparentPower = jsy.data.aggregate.apparentPower,
.current = jsy.data.aggregate.current,
.energy = jsy.data.aggregate.activeEnergyImported,
.energyReturned = jsy.data.aggregate.activeEnergyReturned,
.frequency = jsy.data.frequency,
.power = jsy.data.aggregate.activePower,
.powerFactor = jsy.data.aggregate.powerFactor,
.voltage = jsy.data.aggregate.voltage,
});
break;

Expand Down Expand Up @@ -402,30 +415,43 @@ Mycila::Task initEventsTask("Init Events", [](void* params) {
JsonDocument doc;
deserializeMsgPack(doc, buffer + 5, size);

switch (doc["m"].as<uint16_t>()) {
switch (doc["model"].as<uint16_t>()) {
case MYCILA_JSY_MK_163:
grid.remoteMetrics().update({
.apparentPower = doc["pf1"].as<float>() == 0 ? 0 : doc["p1"].as<float>() / doc["pf1"].as<float>(),
.current = doc["c1"].as<float>(),
.energy = doc["e1"].as<float>(),
.energyReturned = doc["er1"].as<float>(),
.frequency = doc["f"].as<float>(),
.power = doc["p1"].as<float>(),
.powerFactor = doc["pf1"].as<float>(),
.voltage = doc["v1"].as<float>(),
.apparentPower = doc["apparent_power"].as<float>(),
.current = doc["current"].as<float>(),
.energy = doc["active_energy_imported"].as<float>(),
.energyReturned = doc["active_energy_returned"].as<float>(),
.frequency = doc["frequency"].as<float>(),
.power = doc["active_power"].as<float>(),
.powerFactor = doc["power_factor"].as<float>(),
.voltage = doc["voltage"].as<float>(),
});
break;

case MYCILA_JSY_MK_194:
grid.remoteMetrics().update({
.apparentPower = doc["pf2"].as<float>() == 0 ? 0 : doc["p2"].as<float>() / doc["pf2"].as<float>(),
.current = doc["c2"].as<float>(),
.energy = doc["e2"].as<float>(),
.energyReturned = doc["er2"].as<float>(),
.frequency = doc["f"].as<float>(),
.power = doc["p2"].as<float>(),
.powerFactor = doc["pf2"].as<float>(),
.voltage = doc["v2"].as<float>(),
.apparentPower = doc["channel2"]["apparent_power"].as<float>(),
.current = doc["channel2"]["current"].as<float>(),
.energy = doc["channel2"]["active_energy_imported"].as<float>(),
.energyReturned = doc["channel2"]["active_energy_returned"].as<float>(),
.frequency = doc["channel2"]["frequency"].as<float>(),
.power = doc["channel2"]["active_power"].as<float>(),
.powerFactor = doc["channel2"]["power_factor"].as<float>(),
.voltage = doc["channel2"]["voltage"].as<float>(),
});
break;

case MYCILA_JSY_MK_333:
grid.remoteMetrics().update({
.apparentPower = doc["aggregate"]["apparent_power"].as<float>(),
.current = doc["aggregate"]["current"].as<float>(),
.energy = doc["aggregate"]["active_energy_imported"].as<float>(),
.energyReturned = doc["aggregate"]["active_energy_returned"].as<float>(),
.frequency = doc["aggregate"]["frequency"].as<float>(),
.power = doc["aggregate"]["active_power"].as<float>(),
.powerFactor = doc["aggregate"]["power_factor"].as<float>(),
.voltage = doc["aggregate"]["voltage"].as<float>(),
});
break;

Expand Down

0 comments on commit b0cb919

Please sign in to comment.