use private serial reader library
array is no longer needed: - reduce json from 512 to 100 - no need for trailing comma - no need to use []
This commit is contained in:
parent
a588f56e38
commit
69554f441a
@ -22,6 +22,7 @@ lib_deps =
|
||||
bblanchon/ArduinoJson@6.19.4
|
||||
adafruit/Adafruit Unified Sensor@^1.1.4
|
||||
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
|
||||
upload_port = /dev/ttyUSB0
|
||||
check_tool = cppcheck
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include "Dht.h"
|
||||
#include "Protocol_1.h"
|
||||
#include "Protocol_2.h"
|
||||
#include <SerialReader.h>
|
||||
|
||||
#define RESET_PIN 10
|
||||
#define SEND_PIN 11
|
||||
@ -10,6 +11,7 @@
|
||||
|
||||
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
SerialReader<100> serialReader;
|
||||
|
||||
void setup() {
|
||||
digitalWrite(RESET_PIN, HIGH);
|
||||
@ -60,47 +62,38 @@ void readRcSwitch() {
|
||||
}
|
||||
}
|
||||
|
||||
void runJsonCommands(const char* cmd) {
|
||||
StaticJsonDocument<512> jsonArray;
|
||||
DeserializationError err = deserializeJson(jsonArray, cmd);
|
||||
void runJsonCommand(const char* cmd) {
|
||||
StaticJsonDocument<100> jsonDoc;
|
||||
DeserializationError err = deserializeJson(jsonDoc, cmd);
|
||||
if (err == DeserializationError::Ok) {
|
||||
JsonArray array = jsonArray.as<JsonArray>();
|
||||
for (JsonVariant jsonDoc : array) {
|
||||
if (jsonDoc.containsKey("rcSwitch")) {
|
||||
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
||||
Protocol* p = findProtocol(rcSwitch["protocol"]);
|
||||
p->fromJson(rcSwitch, mySwitch);
|
||||
delete p;
|
||||
serializeJson(jsonDoc, Serial);
|
||||
Serial.println();
|
||||
}
|
||||
if (jsonDoc.containsKey("rcSwitch")) {
|
||||
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
||||
Protocol* p = findProtocol(rcSwitch["protocol"]);
|
||||
p->fromJson(rcSwitch, mySwitch);
|
||||
delete p;
|
||||
serializeJson(jsonDoc, Serial);
|
||||
Serial.println();
|
||||
}
|
||||
} else {
|
||||
StaticJsonDocument<128> jsonError;
|
||||
StaticJsonDocument<150> jsonError;
|
||||
JsonObject error = jsonError.createNestedObject("error");
|
||||
error["message"] = err.c_str();
|
||||
error["original_cmd"] = cmd;
|
||||
error["msg"] = err.c_str();
|
||||
error["orig_cmd"] = cmd;
|
||||
serializeJson(jsonError, Serial);
|
||||
Serial.println();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void readCommand() {
|
||||
if (Serial.available() > 0) {
|
||||
String cmd = Serial.readStringUntil('\n');
|
||||
if (cmd == "reset") {
|
||||
if (serialReader.readLine(Serial) > 0) {
|
||||
const char* cmd = serialReader.getBuffer();
|
||||
if (strcmp("reset", cmd) == 0) {
|
||||
Serial.println("resetting...");
|
||||
delay(200);
|
||||
digitalWrite(RESET_PIN, LOW);
|
||||
Serial.println("resetting failed");
|
||||
}
|
||||
if (cmd.endsWith(",")) {
|
||||
cmd = cmd.substring(0, cmd.lastIndexOf(','));
|
||||
}
|
||||
char buffer[cmd.length()+3];
|
||||
sprintf(buffer, "[%s]", cmd.c_str());
|
||||
runJsonCommands(buffer);
|
||||
runJsonCommand(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user