1

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.

7
  • so, the program does something different when it deserializes successfuly and you want to know why it does something different?
    – jsotola
    May 23, 2021 at 20:16
  • Yes that too, I have a lot of questions about this weird behaviour
    – krystof18
    May 23, 2021 at 20:20
  • What exactly are you sending over serial? The first character will be echoed back over Serial. It does not get used by the deseriallzation. That might be a problem depending on what you are sending.
    – chrisl
    May 23, 2021 at 21:20
  • Do you know, what the timeout of the deserialization from serial is? That timeout is propably what takes so long.
    – chrisl
    May 23, 2021 at 21:21
  • I don't know and i couldn't find it either, but I've noticed that the lower Serial.setTimeout the 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)
    – krystof18
    May 23, 2021 at 21:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.