Merge branch 'gw/serial-reader' into gateway
- use private library for serial reader - optimize memory usage - reduce memory needed for json serialization/deserialization - remove the need for array commands
This commit is contained in:
commit
2a591cb28f
@ -40,7 +40,7 @@ steps:
|
|||||||
- cd gateway
|
- cd gateway
|
||||||
- service ser2net stop
|
- service ser2net stop
|
||||||
- pio run -e pro-mini
|
- 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
|
- echo '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
|
- service ser2net start
|
||||||
when:
|
when:
|
||||||
target:
|
target:
|
||||||
|
|||||||
@ -22,6 +22,7 @@ lib_deps =
|
|||||||
bblanchon/ArduinoJson@6.19.4
|
bblanchon/ArduinoJson@6.19.4
|
||||||
adafruit/Adafruit Unified Sensor@^1.1.4
|
adafruit/Adafruit Unified Sensor@^1.1.4
|
||||||
adafruit/DHT sensor library@1.3.10
|
adafruit/DHT sensor library@1.3.10
|
||||||
|
https://git.hodos.ro/arduino/lib_serial-reader.git@^1.0.0
|
||||||
build_flags = -D DHT_SENSOR=0
|
build_flags = -D DHT_SENSOR=0
|
||||||
upload_port = /dev/ttyUSB0
|
upload_port = /dev/ttyUSB0
|
||||||
check_tool = cppcheck
|
check_tool = cppcheck
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
#include "Dht.h"
|
#include "Dht.h"
|
||||||
#include "Protocol_1.h"
|
#include "Protocol_1.h"
|
||||||
#include "Protocol_2.h"
|
#include "Protocol_2.h"
|
||||||
|
#include <SerialReader.h>
|
||||||
|
|
||||||
#define RESET_PIN 10
|
#define RESET_PIN 10
|
||||||
#define SEND_PIN 11
|
#define SEND_PIN 11
|
||||||
@ -10,6 +11,7 @@
|
|||||||
|
|
||||||
|
|
||||||
RCSwitch mySwitch = RCSwitch();
|
RCSwitch mySwitch = RCSwitch();
|
||||||
|
SerialReader<200> serialReader;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
digitalWrite(RESET_PIN, HIGH);
|
digitalWrite(RESET_PIN, HIGH);
|
||||||
@ -60,47 +62,42 @@ void readRcSwitch() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void runJsonCommands(const char* cmd) {
|
void handleJsonError(DeserializationError err, const char* cmd) {
|
||||||
StaticJsonDocument<512> jsonArray;
|
StaticJsonDocument<50> jsonError;
|
||||||
DeserializationError err = deserializeJson(jsonArray, cmd);
|
JsonObject error = jsonError.createNestedObject("error");
|
||||||
|
error["msg"] = err.c_str();
|
||||||
|
error["orig_cmd"] = cmd;
|
||||||
|
serializeJson(jsonError, Serial);
|
||||||
|
Serial.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
void runJsonCommand(char* cmd) {
|
||||||
|
StaticJsonDocument<50> jsonDoc;
|
||||||
|
DeserializationError err = deserializeJson(jsonDoc, cmd);
|
||||||
if (err == DeserializationError::Ok) {
|
if (err == DeserializationError::Ok) {
|
||||||
JsonArray array = jsonArray.as<JsonArray>();
|
if (jsonDoc.containsKey("rcSwitch")) {
|
||||||
for (JsonVariant jsonDoc : array) {
|
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
||||||
if (jsonDoc.containsKey("rcSwitch")) {
|
Protocol* p = findProtocol(rcSwitch["protocol"]);
|
||||||
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
p->fromJson(rcSwitch, mySwitch);
|
||||||
Protocol* p = findProtocol(rcSwitch["protocol"]);
|
delete p;
|
||||||
p->fromJson(rcSwitch, mySwitch);
|
serializeJson(jsonDoc, Serial);
|
||||||
delete p;
|
Serial.println();
|
||||||
serializeJson(jsonDoc, Serial);
|
|
||||||
Serial.println();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
StaticJsonDocument<128> jsonError;
|
handleJsonError(err, cmd);
|
||||||
JsonObject error = jsonError.createNestedObject("error");
|
|
||||||
error["message"] = err.c_str();
|
|
||||||
error["original_cmd"] = cmd;
|
|
||||||
serializeJson(jsonError, Serial);
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void readCommand() {
|
void readCommand() {
|
||||||
if (Serial.available() > 0) {
|
if (serialReader.readLine(Serial) > 0) {
|
||||||
String cmd = Serial.readStringUntil('\n');
|
char* cmd = serialReader.getBuffer();
|
||||||
if (cmd == "reset") {
|
if (strcmp("reset", cmd) == 0) {
|
||||||
Serial.println("resetting...");
|
Serial.println("resetting...");
|
||||||
delay(200);
|
delay(1200);
|
||||||
digitalWrite(RESET_PIN, LOW);
|
digitalWrite(RESET_PIN, LOW);
|
||||||
Serial.println("resetting failed");
|
Serial.println("resetting failed");
|
||||||
}
|
}
|
||||||
if (cmd.endsWith(",")) {
|
runJsonCommand(cmd);
|
||||||
cmd = cmd.substring(0, cmd.lastIndexOf(','));
|
|
||||||
}
|
|
||||||
char buffer[cmd.length()+3];
|
|
||||||
sprintf(buffer, "[%s]", cmd.c_str());
|
|
||||||
runJsonCommands(buffer);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user