I'm JSON deserializing output from ESP8266 in my Arduino UNO, but when it fails (for some reason every time after it deserializes successfully) the program lags for few seconds and I don't want that to happen (at least not for a long time)
#include "SevSeg.h"
#include <ArduinoJson.h>
SevSeg sevseg;
StaticJsonDocument<500> response;
String data;
void setup(){
Serial.begin(74880);
Serial.setTimeout(10000);
while (!Serial) {
// wait for serial port to connect. Needed for native USB port only
}
byte numDigits = 4;
byte digitPins[] = {2, 3, 4, 5};
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_ANODE; // See README.md for options
bool updateWithDelays = false; // Default 'false' is Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected. Then, you only need to specify 7 segmentPins[]
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
Serial.println("running");
}
void loop(){
if (Serial.available()) {
Serial.write(Serial.read());
const auto deser_err = deserializeJson(response, Serial);
if (!deser_err) {
if(response["type"] == "num"){
sevseg.setNumber(response["data"],response["dot"]);
}else{
sevseg.setChars(response["data"]);
}
String data = response["data"];
Serial.println(data);
}else{
Serial.print(F("Failed to deserialize, reason: \""));
Serial.print(deser_err.c_str());
Serial.println('"');
}
}
sevseg.refreshDisplay();
}
The response
running //runs everytime I turn on the arduino
21:43:03.797 -> {null
21:43:03.797 -> :null
21:43:03.797 -> ,null
21:43:03.797 -> :null
21:43:03.797 -> ,null
21:43:03.797 -> :Failed to deserialize, reason: "InvalidInput" // for some reason I have to send request 2 times because for the first time it always fails
21:43:03.797 ->
Uove //correct data received
21:43:04.772 -> // this timestamp isn't right it is like 5 seconds after correct data
Failed to deserialize, reason: "EmptyInput"
What can I do for this not to happen for the first time or in a shorter duration?
I'd be really thankful for any answer.
Serial.setTimeoutthe shorter the duration of waiting, but when u set it too low, it doesn't want to deserialize anything (the current 10s is minimum for it to work correctly)