Camila_99$$
Camila_99$$
DIIDevHeads IoT Integration Server
Created by Camila_99$$ on 8/30/2024 in #edge-networking
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 🙏🏻
4 replies