Skip to content

Commit

Permalink
Several breaking changes:
Browse files Browse the repository at this point in the history
  - Device address support: `read(address)`, `resetEnergy(address)`, `setBaudRate(address)`, `setDeviceAddress()`, `setDestinationAddress()`, etc
  - Object namespace change: `Mycila::JSYData` => `Mycila::JSY::Data`, `Mycila::JSYEventType` => `Mycila::JSY::EventType`, `Mycila::JSYBaudRate` => `Mycila::JSY::BaudRate`, etc
  • Loading branch information
mathieucarbou committed Oct 21, 2024
1 parent b9a94d1 commit 73e1cfc
Show file tree
Hide file tree
Showing 17 changed files with 546 additions and 360 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
[![GitHub latest commit](https://badgen.net/github/last-commit/mathieucarbou/MycilaJSY)](https://github.com/mathieucarbou/MycilaJSY/commit/)
[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/mathieucarbou/MycilaJSY)

Arduino / ESP32 library for the JSY-MK-194T, JSY-MK-194TG single-phase two-way electric energy meters
Arduino / ESP32 library for the JSY-MK-194T, JSY-MK-194TG single-phase two-way electric energy meters from [Shenzhen Jiansiyan Technologies Co, Ltd.](https://www.jsypowermeter.com)

- Sync mode and async mode (non-blocking)
- Core, stack size and interval can be configured
- Automatically detect baud rate
- Energy reset live at runtime
- Switch bauds rate to any supported speed live at runtime
- Configurable Serial (Serial2 by default)
- Core, stack size and interval can be configured
- Device address support (for multiple devices on the same bus)
- Energy reset live at runtime
- Focus on speed and reactivity with a callback mechanism
- Switch bauds rate to any supported speed live at runtime
- Sync mode and async mode (non-blocking)
- Metrics:

```c++
Expand Down Expand Up @@ -87,10 +88,10 @@ There is a getter for each metric.
jsy.begin(Serial2, 16, 17);

// equivalent as above
jsy.begin(Serial2, 16, 17, Mycila::JSYBaudRate::UNKNOWN);
jsy.begin(Serial2, 16, 17, Mycila::JSY::BaudRate::UNKNOWN);

// Skips baud rate detection and use the given baud rate
jsy.begin(Serial2, 16, 17, Mycila::JSYBaudRate::BAUD_38400);
jsy.begin(Serial2, 16, 17, Mycila::JSY::BaudRate::BAUD_38400);
```

### Blocking mode
Expand Down Expand Up @@ -134,8 +135,8 @@ jsy.resetEnergy();
### Update Baud rate (change speed)

```c++
if (jsy.isEnabled() && jsy.getBaudRate() != Mycila::JSYBaudRate::BAUD_38400) {
if (jsy.setBaudRate(Mycila::JSYBaudRate::BAUD_38400)) {
if (jsy.isEnabled() && jsy.getBaudRate() != Mycila::JSY::BaudRate::BAUD_38400) {
if (jsy.setBaudRate(Mycila::JSY::BaudRate::BAUD_38400)) {
// speed changed and switched to new speed
} else {
// speed changed failed, keeping current speed
Expand All @@ -146,7 +147,7 @@ if (jsy.isEnabled() && jsy.getBaudRate() != Mycila::JSYBaudRate::BAUD_38400) {

### Callbacks

- `JSYCallback`: called when the JSY has read the data and when a change for any of the metric is detected by the JSY.
- `Callback`: called when the JSY has read the data and when a change for any of the metric is detected by the JSY.
This is useful to be notified exactly when required.
You must check the event type

Expand All @@ -155,14 +156,14 @@ if (jsy.isEnabled() && jsy.getBaudRate() != Mycila::JSYBaudRate::BAUD_38400) {
Reading a load for 2 second after it is turned on:

```c++
jsy.setCallback([](const Mycila::JSYEventType eventType) {
jsy.setCallback([](const Mycila::JSY::EventType eventType) {
int64_t now = esp_timer_get_time();
switch (eventType) {
case Mycila::JSYEventType::EVT_READ:
case Mycila::JSY::EventType::EVT_READ:
Serial.printf(" - %" PRId64 " EVT_READ\n", now);
break;
case Mycila::JSYEventType::EVT_CHANGE:
Serial.printf(" - %" PRId64 " EVT_CHANGE: %f W\n", now, jsy.getPower2());
case Mycila::JSY::EventType::EVT_CHANGE:
Serial.printf(" - %" PRId64 " EVT_CHANGE: %f W\n", now, jsy.getActivePower2());
break;
default:
Serial.printf(" - %" PRId64 " ERR\n", now);
Expand Down
29 changes: 15 additions & 14 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
[![GitHub latest commit](https://badgen.net/github/last-commit/mathieucarbou/MycilaJSY)](https://github.com/mathieucarbou/MycilaJSY/commit/)
[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/mathieucarbou/MycilaJSY)

Arduino / ESP32 library for the JSY-MK-194T, JSY-MK-194TG single-phase two-way electric energy meters
Arduino / ESP32 library for the JSY-MK-194T, JSY-MK-194TG single-phase two-way electric energy meters from [Shenzhen Jiansiyan Technologies Co, Ltd.](https://www.jsypowermeter.com)

- Sync mode and async mode (non-blocking)
- Core, stack size and interval can be configured
- Automatically detect baud rate
- Energy reset live at runtime
- Switch bauds rate to any supported speed live at runtime
- Configurable Serial (Serial2 by default)
- Core, stack size and interval can be configured
- Device address support (for multiple devices on the same bus)
- Energy reset live at runtime
- Focus on speed and reactivity with a callback mechanism
- Switch bauds rate to any supported speed live at runtime
- Sync mode and async mode (non-blocking)
- Metrics:

```c++
Expand Down Expand Up @@ -87,10 +88,10 @@ There is a getter for each metric.
jsy.begin(Serial2, 16, 17);

// equivalent as above
jsy.begin(Serial2, 16, 17, Mycila::JSYBaudRate::UNKNOWN);
jsy.begin(Serial2, 16, 17, Mycila::JSY::BaudRate::UNKNOWN);

// Skips baud rate detection and use the given baud rate
jsy.begin(Serial2, 16, 17, Mycila::JSYBaudRate::BAUD_38400);
jsy.begin(Serial2, 16, 17, Mycila::JSY::BaudRate::BAUD_38400);
```

### Blocking mode
Expand Down Expand Up @@ -134,8 +135,8 @@ jsy.resetEnergy();
### Update Baud rate (change speed)

```c++
if (jsy.isEnabled() && jsy.getBaudRate() != Mycila::JSYBaudRate::BAUD_38400) {
if (jsy.setBaudRate(Mycila::JSYBaudRate::BAUD_38400)) {
if (jsy.isEnabled() && jsy.getBaudRate() != Mycila::JSY::BaudRate::BAUD_38400) {
if (jsy.setBaudRate(Mycila::JSY::BaudRate::BAUD_38400)) {
// speed changed and switched to new speed
} else {
// speed changed failed, keeping current speed
Expand All @@ -146,7 +147,7 @@ if (jsy.isEnabled() && jsy.getBaudRate() != Mycila::JSYBaudRate::BAUD_38400) {

### Callbacks

- `JSYCallback`: called when the JSY has read the data and when a change for any of the metric is detected by the JSY.
- `Callback`: called when the JSY has read the data and when a change for any of the metric is detected by the JSY.
This is useful to be notified exactly when required.
You must check the event type

Expand All @@ -155,14 +156,14 @@ if (jsy.isEnabled() && jsy.getBaudRate() != Mycila::JSYBaudRate::BAUD_38400) {
Reading a load for 2 second after it is turned on:

```c++
jsy.setCallback([](const Mycila::JSYEventType eventType) {
jsy.setCallback([](const Mycila::JSY::EventType eventType) {
int64_t now = esp_timer_get_time();
switch (eventType) {
case Mycila::JSYEventType::EVT_READ:
case Mycila::JSY::EventType::EVT_READ:
Serial.printf(" - %" PRId64 " EVT_READ\n", now);
break;
case Mycila::JSYEventType::EVT_CHANGE:
Serial.printf(" - %" PRId64 " EVT_CHANGE: %f W\n", now, jsy.getPower2());
case Mycila::JSY::EventType::EVT_CHANGE:
Serial.printf(" - %" PRId64 " EVT_CHANGE: %f W\n", now, jsy.getActivePower2());
break;
default:
Serial.printf(" - %" PRId64 " ERR\n", now);
Expand Down
18 changes: 9 additions & 9 deletions examples/Callback/Callback.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@

Mycila::JSY jsy;
JsonDocument doc;
const Mycila::JSYBaudRate rates[] = {
Mycila::JSYBaudRate::BAUD_4800,
Mycila::JSYBaudRate::BAUD_9600,
Mycila::JSYBaudRate::BAUD_19200,
Mycila::JSYBaudRate::BAUD_38400,
const Mycila::JSY::BaudRate rates[] = {
Mycila::JSY::BaudRate::BAUD_4800,
Mycila::JSY::BaudRate::BAUD_9600,
Mycila::JSY::BaudRate::BAUD_19200,
Mycila::JSY::BaudRate::BAUD_38400,
};

void setup() {
Serial.begin(115200);
while (!Serial)
continue;

jsy.setCallback([](const Mycila::JSYEventType eventType) {
jsy.setCallback([](const Mycila::JSY::EventType eventType) {
int64_t now = esp_timer_get_time();
switch (eventType) {
case Mycila::JSYEventType::EVT_READ:
case Mycila::JSY::EventType::EVT_READ:
Serial.printf(" - %" PRId64 " EVT_READ\n", now);
break;
case Mycila::JSYEventType::EVT_CHANGE:
Serial.printf(" - %" PRId64 " EVT_CHANGE: %f W\n", now, jsy.getPower2());
case Mycila::JSY::EventType::EVT_CHANGE:
Serial.printf(" - %" PRId64 " EVT_CHANGE: %f W\n", now, jsy.getActivePower2());
break;
default:
Serial.printf(" - %" PRId64 " ERR\n", now);
Expand Down
10 changes: 5 additions & 5 deletions examples/CallbackAsync/CallbackAsync.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ void setup() {
while (!Serial)
continue;

jsy.setCallback([](const Mycila::JSYEventType eventType) {
if (eventType == Mycila::JSYEventType::EVT_CHANGE) {
Serial.printf(" - %" PRIu32 " EVT_CHANGE: %f V, %f A, %f W\n", (uint32_t)millis(), jsy.getVoltage2(), jsy.getCurrent2(), jsy.getPower2());
jsy.setCallback([](const Mycila::JSY::EventType eventType) {
if (eventType == Mycila::JSY::EventType::EVT_CHANGE) {
Serial.printf(" - %" PRIu32 " EVT_CHANGE: %f V, %f A, %f W\n", (uint32_t)millis(), jsy.getVoltage2(), jsy.getCurrent2(), jsy.getActivePower2());
}
});

jsy.begin(Serial2, 16, 17);
if (jsy.getBaudRate() != Mycila::JSYBaudRate::BAUD_38400) {
jsy.setBaudRate(Mycila::JSYBaudRate::BAUD_38400);
if (jsy.getBaudRate() != Mycila::JSY::BaudRate::BAUD_38400) {
jsy.setBaudRate(Mycila::JSY::BaudRate::BAUD_38400);
}
jsy.end();

Expand Down
10 changes: 5 additions & 5 deletions examples/PerfTest1/PerfTest1.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

Mycila::JSY jsy;
JsonDocument doc;
const Mycila::JSYBaudRate rates[] = {
Mycila::JSYBaudRate::BAUD_4800,
Mycila::JSYBaudRate::BAUD_9600,
Mycila::JSYBaudRate::BAUD_19200,
Mycila::JSYBaudRate::BAUD_38400,
const Mycila::JSY::BaudRate rates[] = {
Mycila::JSY::BaudRate::BAUD_4800,
Mycila::JSY::BaudRate::BAUD_9600,
Mycila::JSY::BaudRate::BAUD_19200,
Mycila::JSY::BaudRate::BAUD_38400,
};

void setup() {
Expand Down
16 changes: 8 additions & 8 deletions examples/PerfTest2/PerfTest2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

Mycila::JSY jsy;
JsonDocument doc;
const Mycila::JSYBaudRate rates[] = {
Mycila::JSYBaudRate::BAUD_4800,
Mycila::JSYBaudRate::BAUD_9600,
Mycila::JSYBaudRate::BAUD_19200,
Mycila::JSYBaudRate::BAUD_38400,
const Mycila::JSY::BaudRate rates[] = {
Mycila::JSY::BaudRate::BAUD_4800,
Mycila::JSY::BaudRate::BAUD_9600,
Mycila::JSY::BaudRate::BAUD_19200,
Mycila::JSY::BaudRate::BAUD_38400,
};

void setup() {
Expand Down Expand Up @@ -60,7 +60,7 @@ void setup() {
Serial.printf(" - ROUND: %d\n", rounds);

digitalWrite(RELAY_PIN, LOW);
while (jsy.getPower2() > 0) {
while (jsy.getActivePower2() > 0) {
jsy.read();
}

Expand All @@ -80,7 +80,7 @@ void setup() {
int64_t start = esp_timer_get_time();
while (true) {
jsy.read();
now = jsy.getPower2();
now = jsy.getActivePower2();

if (reactivityTime == 0) {
if (now > 0) {
Expand All @@ -106,7 +106,7 @@ void setup() {
start = esp_timer_get_time();
while (true) {
jsy.read();
now = jsy.getPower2();
now = jsy.getActivePower2();

if (now == 0) {
break;
Expand Down
10 changes: 5 additions & 5 deletions examples/Read/Read.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ void setup() {
// read JSY on pins 17 (JSY RX / Serial TX) and 16 (JSY TX / Serial RX)
// baud rate will be detected automatically
jsy.begin(Serial2, 16, 17);
// jsy.begin(Serial2, 16, 17, Mycila::JSYBaudRate::UNKNOWN);
// jsy.begin(Serial2, 16, 17, Mycila::JSY::BaudRate::UNKNOWN);

// if you know the bauds rate, you can set it manually
// jsy.begin(Serial2, 16, 17, Mycila::JSYBaudRate::BAUD_4800);
// jsy.begin(Serial2, 16, 17, Mycila::JSYBaudRate::BAUD_9600);
// jsy.begin(Serial2, 16, 17, Mycila::JSYBaudRate::BAUD_19200);
// jsy.begin(Serial2, 16, 17, Mycila::JSYBaudRate::BAUD_38400);
// jsy.begin(Serial2, 16, 17, Mycila::JSY::BaudRate::BAUD_4800);
// jsy.begin(Serial2, 16, 17, Mycila::JSY::BaudRate::BAUD_9600);
// jsy.begin(Serial2, 16, 17, Mycila::JSY::BaudRate::BAUD_19200);
// jsy.begin(Serial2, 16, 17, Mycila::JSY::BaudRate::BAUD_38400);
}

void loop() {
Expand Down
2 changes: 1 addition & 1 deletion examples/ReadAsync/ReadAsync.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void setup() {
jsy.begin(Serial2, 16, 17, true);
}

Mycila::JSYBaudRate target = Mycila::JSYBaudRate::BAUD_38400;
Mycila::JSY::BaudRate target = Mycila::JSY::BaudRate::BAUD_38400;

void loop() {
if (!jsy.isEnabled()) {
Expand Down
2 changes: 1 addition & 1 deletion examples/RemoteUDP/Listener/Listener.ino
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Chart power2History = Chart(&dashboard, BAR_CHART, "Channel 2 Active Power (W)")

String hostname;
String ssid;
volatile Mycila::JSYData jsyData;
volatile Mycila::JSY::Data jsyData;

// circular buffer for msg rate
Mycila::CircularBuffer<float, MYCILA_UDP_SEND_RATE_WINDOW> messageRateBuffer;
Expand Down
2 changes: 1 addition & 1 deletion examples/RemoteUDP/Listener/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ lib_deps =
mathieucarbou/AsyncTCP @ 3.2.10
mathieucarbou/ESPAsyncWebServer @ 3.3.17
mathieucarbou/MycilaESPConnect @ 6.0.3
mathieucarbou/MycilaJSY @ 9.1.10
mathieucarbou/MycilaLogger @ 3.2.0
mathieucarbou/MycilaSystem @ 3.1.0
mathieucarbou/MycilaTaskManager @ 3.1.2
Expand All @@ -32,6 +31,7 @@ lib_deps =
mathieucarbou/MycilaWebSerial @ 6.4.1
ayushsharma82/ElegantOTA @ 3.1.6
https://github.com/mathieucarbou/ayushsharma82-ESP-DASH#dev-1
MycilaJSY=file://../../..
build_flags =
-D ARDUINO_LOOP_STACK_SIZE=4096
-D CONFIG_ASYNC_TCP_MAX_ACK_TIME=3000
Expand Down
20 changes: 10 additions & 10 deletions examples/RemoteUDP/Sender/Sender.ino
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ Mycila::Task dashboardTask("Dashboard", [](void* params) {
networkWiFiSSID.set(espConnect.getWiFiSSID().c_str());
uptime.set(Mycila::Time::toDHHMMSS(Mycila::System::getUptime()).c_str());

activePower1.update(jsy.getPower1());
activePower1.update(jsy.getActivePower1());
apparentPower1.update(jsy.getApparentPower1());
current1.update(jsy.getCurrent1());
energy1.update(jsy.getEnergy1());
Expand All @@ -221,7 +221,7 @@ Mycila::Task dashboardTask("Dashboard", [](void* params) {
voltage1.update(jsy.getVoltage1());
voltageDimmed1.update(jsy.getDimmedVoltage1());

activePower2.update(jsy.getPower2());
activePower2.update(jsy.getActivePower2());
apparentPower2.update(jsy.getApparentPower2());
current2.update(jsy.getCurrent2());
energy2.update(jsy.getEnergy2());
Expand All @@ -244,8 +244,8 @@ Mycila::Task dashboardTask("Dashboard", [](void* params) {
}

// set new value
power1HistoryY[MYCILA_GRAPH_POINTS - 1] = round(jsy.getPower1());
power2HistoryY[MYCILA_GRAPH_POINTS - 1] = round(jsy.getPower2());
power1HistoryY[MYCILA_GRAPH_POINTS - 1] = round(jsy.getActivePower1());
power2HistoryY[MYCILA_GRAPH_POINTS - 1] = round(jsy.getActivePower2());

// update charts
power1History.updateY(power1HistoryY, MYCILA_GRAPH_POINTS);
Expand Down Expand Up @@ -359,14 +359,14 @@ void setup() {
dashboardTask.forceRun();

// jsy
jsy.setCallback([](const Mycila::JSYEventType eventType) {
jsy.setCallback([](const Mycila::JSY::EventType eventType) {
if (!udpSendEnabled) {
messageRate = 0;
dataRate = 0;
return;
}

if (eventType == Mycila::JSYEventType::EVT_CHANGE) {
if (eventType == Mycila::JSY::EventType::EVT_CHANGE) {

Mycila::ESPConnect::Mode mode = espConnect.getMode();

Expand All @@ -380,8 +380,8 @@ void setup() {
root["er1"] = jsy.getEnergyReturned1();
root["er2"] = jsy.getEnergyReturned2();
root["f"] = jsy.getFrequency();
root["p1"] = jsy.getPower1();
root["p2"] = jsy.getPower2();
root["p1"] = jsy.getActivePower1();
root["p2"] = jsy.getActivePower2();
root["pf1"] = jsy.getPowerFactor1();
root["pf2"] = jsy.getPowerFactor2();
root["v1"] = jsy.getVoltage1();
Expand Down Expand Up @@ -433,8 +433,8 @@ void setup() {
}
});
jsy.begin(MYCILA_JSY_SERIAL, MYCILA_JSY_RX, MYCILA_JSY_TX);
if (jsy.isEnabled() && jsy.getBaudRate() != Mycila::JSYBaudRate::BAUD_38400)
jsy.setBaudRate(Mycila::JSYBaudRate::BAUD_38400);
if (jsy.isEnabled() && jsy.getBaudRate() != Mycila::JSY::BaudRate::BAUD_38400)
jsy.setBaudRate(Mycila::JSY::BaudRate::BAUD_38400);

// Network Manager
espConnect.setAutoRestart(true);
Expand Down
Loading

0 comments on commit 73e1cfc

Please sign in to comment.