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
|
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<100> serialReader;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
digitalWrite(RESET_PIN, HIGH);
|
digitalWrite(RESET_PIN, HIGH);
|
||||||
@ -60,12 +62,10 @@ void readRcSwitch() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void runJsonCommands(const char* cmd) {
|
void runJsonCommand(const char* cmd) {
|
||||||
StaticJsonDocument<512> jsonArray;
|
StaticJsonDocument<100> jsonDoc;
|
||||||
DeserializationError err = deserializeJson(jsonArray, cmd);
|
DeserializationError err = deserializeJson(jsonDoc, cmd);
|
||||||
if (err == DeserializationError::Ok) {
|
if (err == DeserializationError::Ok) {
|
||||||
JsonArray array = jsonArray.as<JsonArray>();
|
|
||||||
for (JsonVariant jsonDoc : array) {
|
|
||||||
if (jsonDoc.containsKey("rcSwitch")) {
|
if (jsonDoc.containsKey("rcSwitch")) {
|
||||||
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
||||||
Protocol* p = findProtocol(rcSwitch["protocol"]);
|
Protocol* p = findProtocol(rcSwitch["protocol"]);
|
||||||
@ -74,33 +74,26 @@ void runJsonCommands(const char* cmd) {
|
|||||||
serializeJson(jsonDoc, Serial);
|
serializeJson(jsonDoc, Serial);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
StaticJsonDocument<128> jsonError;
|
StaticJsonDocument<150> jsonError;
|
||||||
JsonObject error = jsonError.createNestedObject("error");
|
JsonObject error = jsonError.createNestedObject("error");
|
||||||
error["message"] = err.c_str();
|
error["msg"] = err.c_str();
|
||||||
error["original_cmd"] = cmd;
|
error["orig_cmd"] = cmd;
|
||||||
serializeJson(jsonError, Serial);
|
serializeJson(jsonError, Serial);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void readCommand() {
|
void readCommand() {
|
||||||
if (Serial.available() > 0) {
|
if (serialReader.readLine(Serial) > 0) {
|
||||||
String cmd = Serial.readStringUntil('\n');
|
const char* cmd = serialReader.getBuffer();
|
||||||
if (cmd == "reset") {
|
if (strcmp("reset", cmd) == 0) {
|
||||||
Serial.println("resetting...");
|
Serial.println("resetting...");
|
||||||
delay(200);
|
delay(200);
|
||||||
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