I have a project wherein hits detected from an electronic dart board are transmitted from an ESP32-WROOM, over WiFi, to a NodeJS server running on Windows. On a fresh boot of the Arduino everything works great for a while. After an undetermined amount of time, a dart will hit and there will be an odd delayed response. The delay prevents the transmission of the data to the server and a “-2” is bounced back from the server in response. At first, it’ll recover after a pause and I can continue, but as time goes on it gets worse and the delay gets longer to the point that I have to reboot the arduino. The function sends a JSON string with two variables, a point integer and a message string.
void sendData(int point, String msg) {
if (WiFi.status() == WL_CONNECTED) {
StaticJsonDocument<200> doc;
doc["point"] = String(point);
doc["message"] = String(msg);
// Serialize JSON to a String
String jsonString;
serializeJson(doc, jsonString);
// Send HTTP POST request with JSON data
http.beginRequest();
http.post("/data", "application/json", jsonString);
int httpResponseCode = http.responseStatusCode();
String response = http.responseBody();
http.endRequest();
} else if (WiFi.status() == WL_CONNECT_FAILED || WiFi.status() == WL_CONNECTION_LOST || WiFi.status() == WL_DISCONNECTED) {
Serial.println("lost connection");
}
}
The entire project can be found here https://github.com/ctkjedi/DigiDarts/ if more info is needed about the js app or any supporting files. I’ve been able to mitigate or at least greatly delay when it happens by faking a keep-alive in the loop function and printing a single period, every second, to the serial monitor. Eventually though, it fails the same way.