From 92bcbdbe6b5a1a88c826fe6302ce806103130025 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sun, 23 Oct 2022 09:05:18 +0200 Subject: [PATCH 1/4] echo every command in array --- .drone.yml | 3 --- gateway/src/gateway.cpp | 7 ++++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.drone.yml b/.drone.yml index 7c238b1..8fe9ac6 100644 --- a/.drone.yml +++ b/.drone.yml @@ -15,9 +15,6 @@ steps: - pio run -e pro-mini - echo -n 'reset' > /dev/ttyUSB0; sleep 1s; avrdude -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:.pio/build/pro-mini/firmware.hex:i -v - service ser2net start - when: - target: - - production trigger: branch: diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index d819824..b291c5a 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -128,6 +128,9 @@ void runJsonCommand(String cmd) { mySwitch.send(rcSwitch["value"]); // Serial.println((const char*)rcSwitch["value"]); } + char output[256]; + serializeJson(jsonDoc, output); + Serial.println(output); blink(); } else { Serial.println(err.c_str()); @@ -148,8 +151,6 @@ void readCommand() { if (cmd.endsWith(",")) { cmd = cmd.substring(0, cmd.lastIndexOf(',')); } - cmd = "[" + cmd + "]"; - Serial.println(cmd); - runJsonCommand(cmd); + runJsonCommand("[" + cmd + "]"); } } From 20feae05886f099e7dc21f9c79635c29f0823873 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 24 Oct 2022 14:03:16 +0200 Subject: [PATCH 2/4] refactor into smaller functions --- gateway/src/gateway.cpp | 52 +++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index b291c5a..c851470 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -105,37 +105,38 @@ void blink() { digitalWrite(LED_BUILTIN, LOW); } -void runJsonCommand(String cmd) { - StaticJsonDocument<256> jsonArray; +void runRcSwitchCommand(JsonVariant jsonDoc) { + JsonObject rcSwitch = jsonDoc["rcSwitch"]; + unsigned int protocol = rcSwitch["protocol"]; + if (protocol == 1) { + mySwitch.setProtocol(protocol); + char* group = rcSwitch["group"]; + int channel = rcSwitch["channel"]; + rcSwitch["state"] ? mySwitch.switchOn(group, channel) : mySwitch.switchOff(group, channel); + } else { + mySwitch.setProtocol(protocol); + mySwitch.send(rcSwitch["value"]); + // Serial.println((const char*)rcSwitch["value"]); + } + char output[256]; + serializeJson(jsonDoc, output); + Serial.println(output); + // blink(); + // delay(1000); +} + +void runJsonCommands(String cmd) { + StaticJsonDocument<512> jsonArray; DeserializationError err = deserializeJson(jsonArray, cmd); if (err == DeserializationError::Ok) { JsonArray array = jsonArray.as(); for (JsonVariant jsonDoc : array) { if (jsonDoc.containsKey("rcSwitch")) { - JsonObject rcSwitch = jsonDoc["rcSwitch"]; - unsigned int protocol = rcSwitch["protocol"]; - if (protocol == 1) { - mySwitch.setProtocol(protocol); - char* group = rcSwitch["group"]; - int channel = rcSwitch["channel"]; - if (rcSwitch["state"]) { - mySwitch.switchOn(group, channel); - } else { - mySwitch.switchOff(group, channel); - } - } else { - mySwitch.setProtocol(protocol); - mySwitch.send(rcSwitch["value"]); - // Serial.println((const char*)rcSwitch["value"]); - } - char output[256]; - serializeJson(jsonDoc, output); - Serial.println(output); - blink(); - } else { - Serial.println(err.c_str()); + runRcSwitchCommand(jsonDoc); } } + } else { + Serial.println(String(err.c_str()) + ": " + cmd); } } @@ -151,6 +152,7 @@ void readCommand() { if (cmd.endsWith(",")) { cmd = cmd.substring(0, cmd.lastIndexOf(',')); } - runJsonCommand("[" + cmd + "]"); + cmd = "[" + cmd + "]"; + runJsonCommands(cmd); } } From 2432e631300e85677afe8d3807576f2b4d5d953d Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 24 Oct 2022 16:02:05 +0200 Subject: [PATCH 3/4] write json directly into Serial --- gateway/src/gateway.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index c851470..cc6368d 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -33,8 +33,8 @@ void setup() { } void loop() { - StaticJsonDocument<200> jsonDoc; readCommand(); + StaticJsonDocument<200> jsonDoc; readRcSwitch(jsonDoc); Dht::read(jsonDoc); if (!jsonDoc.isNull()) { @@ -116,13 +116,10 @@ void runRcSwitchCommand(JsonVariant jsonDoc) { } else { mySwitch.setProtocol(protocol); mySwitch.send(rcSwitch["value"]); - // Serial.println((const char*)rcSwitch["value"]); } - char output[256]; - serializeJson(jsonDoc, output); - Serial.println(output); + serializeJson(jsonDoc, Serial); + Serial.println(); // blink(); - // delay(1000); } void runJsonCommands(String cmd) { From f3141df04ed436fe69045bcbd1843d9127218202 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 24 Oct 2022 15:48:34 +0200 Subject: [PATCH 4/4] make a copy of cmd, ArudinoJson alters original one --- gateway/src/gateway.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index cc6368d..876cdef 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -122,7 +122,8 @@ void runRcSwitchCommand(JsonVariant jsonDoc) { // blink(); } -void runJsonCommands(String cmd) { +void runJsonCommands(const char* cmd) { + String origCmd = String(cmd); StaticJsonDocument<512> jsonArray; DeserializationError err = deserializeJson(jsonArray, cmd); if (err == DeserializationError::Ok) { @@ -133,7 +134,9 @@ void runJsonCommands(String cmd) { } } } else { - Serial.println(String(err.c_str()) + ": " + cmd); + Serial.print(err.c_str()); + Serial.print(": "); + Serial.println(origCmd); } } @@ -150,6 +153,6 @@ void readCommand() { cmd = cmd.substring(0, cmd.lastIndexOf(',')); } cmd = "[" + cmd + "]"; - runJsonCommands(cmd); + runJsonCommands(cmd.c_str()); } }