How to Force HTTP Requests to Timeout on ESP32 When Router Freezes?

Hello! I've had a setup running for several months now, which makes HTTP requests to several devices on home WiFi network (the router is part of a Freebox). Everything works quite well, except for certain moments when I experience "freezes" of several seconds during a request. These freezes often disappear if I restart my Freebox (and thus the router). To avoid these freezes, I’m looking for a way to force the request to exit if it hasn’t succeeded within a second (for example). Here is the current code of one of my HTTP requests (launched every 5 seconds from the loop of my program):
void lect_ShellyEM()
{
HTTPClient http;
http.begin("http://192.168.1.12/emeter/0"); // Extracts data from the "emeter" 0 of the ShellyEM, i.e., the EDF consumption
http.setConnectTimeout(500); // Local IP >> aborts directly after 0.5 s if no connection
httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) // Checks if the transmission was successful
{
StaticJsonDocument<256> doc;
deserializeJson(doc, (http.getString())); // Extracts variables with ArduinoJson
Pedf = doc["power"]; Pedft = doc["total"];
if (top_m1 == true) // When the day changes
{
Pedf_m = Pedft; // Records the total from ShellyEM at midnight
pref.begin("memo_infos", false); // Writes the total in the "Pedf_m" section of the EEPROM
pref.putFloat("Pedf_m", Pedf_m); // in case of reset or power outage
pref.end();
top_m1 = false; // Resets top_m1
}
}
http.end();
}
void lect_ShellyEM()
{
HTTPClient http;
http.begin("http://192.168.1.12/emeter/0"); // Extracts data from the "emeter" 0 of the ShellyEM, i.e., the EDF consumption
http.setConnectTimeout(500); // Local IP >> aborts directly after 0.5 s if no connection
httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) // Checks if the transmission was successful
{
StaticJsonDocument<256> doc;
deserializeJson(doc, (http.getString())); // Extracts variables with ArduinoJson
Pedf = doc["power"]; Pedft = doc["total"];
if (top_m1 == true) // When the day changes
{
Pedf_m = Pedft; // Records the total from ShellyEM at midnight
pref.begin("memo_infos", false); // Writes the total in the "Pedf_m" section of the EEPROM
pref.putFloat("Pedf_m", Pedf_m); // in case of reset or power outage
pref.end();
top_m1 = false; // Resets top_m1
}
}
http.end();
}
I added the line http.setConnectTimeout(500);, but it doesn’t seem to be of much use... Maybe using a "watchdog" that would exit the request after some time? But I’m not really sure how to do that... So, I’m looking for all your expert advice! Thanks 🙏🏻
Solution:
Hi @Camila_99$$ in other to avoid freezes during HTTP requests, set both a connection timeout (http.setConnectTimeout(500);) and a response timeout (http.setTimeout(1000);). This will make sure the request stops if it doesn't finish within the given time. For even better performance, you might want to use asynchronous requests with ESPAsyncHTTPClient to keep your main loop running smoothly.
Jump to solution
2 Replies
Solution
Dark AI
Dark AI3mo ago
Hi @Camila_99$$ in other to avoid freezes during HTTP requests, set both a connection timeout (http.setConnectTimeout(500);) and a response timeout (http.setTimeout(1000);). This will make sure the request stops if it doesn't finish within the given time. For even better performance, you might want to use asynchronous requests with ESPAsyncHTTPClient to keep your main loop running smoothly.
Camila_99$$
Camila_99$$3mo ago
Hi! @Dark AI Okk thanks I’ll definitely try that. I haven’t explored ESPAsyncHTTPClient yet, but it sounds like a great idea to keep things running smoothly. Appreciate your help! 😊
Want results from more Discord servers?
Add your server