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):
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:Jump to 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.2 Replies
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.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! 😊