The JSON strings size are not a problem because they work in nominal mode. They are like 20/30 characters.
It is just that once an error occurs in the parsing, all the upcoming messages will not be read.
The JSON frequency is low, because it happens when I choose, so basically it is like this :
I tell my ESP32 to send a request message ==> The ESP32 sends a JSON with a header "request" ==> The Arduino receive the request, takes some measurements, and then send another JSON with the header "Answer" ==> done.
It works quite well, but if somehow a problem arise and the error with the json arriving, it will not work anymore and even though it received the upcoming message, will not be able to parse.
Since it work if I reboot it, the problem is not in the message, but in the treatment of it by the Arduino.
That is why I would like to know how I can "reset and empty" the communication, without reseting the whole arduino.
I will send a "resumed" version of the code
ESP32 :
void loop()
{
tryagain:// Used to send another request if no answer comes
Firebase.getInt(firebaseData, "/Request/Requestall");
int requestdata=firebaseData.intData();
Firebase.getInt(firebaseData, "/Request/UpdateLight");
int requestupdatelight=firebaseData.intData();
Firebase.getInt(firebaseData, "/Request/PumpChange");
int requestpump=firebaseData.intData();
while(requestdata == 0 && requestupdatelight==0 && requestpump==0) {// Block while there is no request from user
delay(1000);
Firebase.getInt(firebaseData, "/Request/Requestall");
requestdata=firebaseData.intData();
Firebase.getInt(firebaseData, "/Request/UpdateLight");
requestupdatelight=firebaseData.intData();
Firebase.getInt(firebaseData, "/Request/PumpChange");
requestpump=firebaseData.intData();
}
if(requestdata>0){
req["type"] = "request";
req.printTo(Serial);
boolean messageReady = false;
String message = "";
int i=0;
while(messageReady == false) { // Runs until a message is received back
if(Serial.available()) {
message = Serial.readString(); // Read the message
messageReady = true; // "Break" the while and continue
continue; //skip the delay if necessary
}
delay(10);
i++;
if(i % 1000 == 0) {goto tryagain;}// If after 10s nothing comes back, tries again to send a request
}
JsonObject& obj = jb.parseObject(message); // Translate the received message into a JsonObject for treatment
lux = obj["lux"];
dist= obj["dist"];
temp= obj["temp"];
humidity= obj["humidity"];
waterlevel= obj["waterlevel"];
tdsValue=obj["tdsvalue"];
watertemp=obj["watertemp"];
obj.prettyPrintTo(Serial);
Serial.println();
Firebase.setDouble(firebaseData,"Sensors/Light", lux);
Firebase.setDouble(firebaseData,"Sensors/TankHeight", dist);
Firebase.setDouble(firebaseData,"Sensors/AirTemp", temp);
Firebase.setDouble(firebaseData,"Sensors/Humidity", humidity);
Firebase.setDouble(firebaseData,"Sensors/WaterLevel", waterlevel);
Firebase.setDouble(firebaseData,"Sensors/EC", tdsValue);
Firebase.setDouble(firebaseData,"Sensors/WaterTemp", watertemp);
Firebase.setDouble(firebaseData,"Request/Requestall", 0); // Reset the request
requestupdatelight=0;
}
For the arduino :
tryagain:// If message bugs, retries
// measurements
uint16_t lux = lightMeter.readLightLevel();
tdsValue=getECValue();
int temp = bme.readTemperature();
int humidity = bme.readHumidity();
waterlevel = analogRead(respin);
dist=DistanceCalculation();
watertemp=sensors.getTempCByIndex(0);
// Monitor serial communication
while(Serial.available()) { // When a message becomes available, run the while and reads the message
message = Serial.readString();
messageReady = true;
}
// Only process message if there's one
if(messageReady) {
DynamicJsonBuffer jb; // Create a Json Buffer for message storage
JsonObject& obj = jb.parseObject(message); // Create a Json out of the message received
if (!obj.success()) {
//jb.clear();
// messageReady=false;
// goto tryagain;
// Serial.println("parseObject() failed"); // In case the message is not working ( Happens usualy when the ESP is reset at the first loop )
void(* resetFunc) (void) = 0;//declare reset function at address 0
resetFunc(); //call reset
}
if(obj["type"] == "request") { // If it is a request
obj["type"] = "response"; // The sent back message will be a response
obj["lux"] = lux; // Write the lux value in the message
obj["dist"]=dist;
obj["temp"]=temp;
obj["humidity"]=humidity;
obj["waterlevel"]=waterlevel;
obj["tdsvalue"]=tdsValue;
obj["watertemp"]=watertemp;
obj.printTo(Serial); // Send the message
}