Seeed_Arduino_rpcWiFi icon indicating copy to clipboard operation
Seeed_Arduino_rpcWiFi copied to clipboard

HTTPClient http.GET hangs when host is down

Open vossnick opened this issue 2 years ago • 0 comments

When attempting a connection to a webserver that happens to not respond, i.e. it is down. The http.GET() seems to hang. I attempted adding setTimeout and setConnectTimeout with no success. It doesn’t seem to matter what I do. If the host is down it simply hangs on the http.GET(). Any ideas? I need to be able to gracefully handle the eventuality that any particular host may be down, but that life should move on? Please note this code does work with different hardware (wESP32) and different version of (esp32/2.0.14/libraries/HTTPClient/src/HTTPClient.h), but not with the wio terminal and the libs included in the following code:

`#include <Arduino.h> #include <rpcWiFi.h> #include <HTTPClient.h>

const char ssid[] = "x"; const char password[] = "x;

void setup() { Serial.begin(9600); while (!Serial); Serial.println("Serial Started.");

int CA = 0; WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); delay(1000); while (WiFi.status() != WL_CONNECTED) { CA++; if (CA > 2) { WiFi.disconnect(); delay(2000); WiFi.begin(ssid, password); CA = 0; } delay(500); } Serial.println("Connected."); }

void loop() { // wait for WiFi connection if((WiFi.status() == WL_CONNECTED)) { HTTPClient http; delay(1000); Serial.print("[HTTP] begin...\n"); http.begin("http://192.168.226.205/dt.php"); //HTTP delay(1000); Serial.println("setTimeout"); http.setTimeout(10); delay(1000); Serial.println("setConnectTimeout"); http.setConnectTimeout(5); delay(1000); Serial.print("[HTTP] GET...\n"); int httpCode = http.GET(); Serial.print("[HTTP] END GET...\n"); if(httpCode > 0) { Serial.printf("[HTTP] GET... code: %d\n", httpCode);

if(httpCode == HTTP_CODE_OK) {
  String payload = http.getString();
  Serial.println(payload);
}
} else {
  Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();

} delay(5000); } ` To Reproduce If you run this code on the Wio Terminal, adding your wifi creds, you should immediately experience the same issue (it is unlikely the ip it is trying exists on your network). I have tested values as second and millis of a wide range for setTimeout and setConnectTimeout.

Expected behavior I would expect this to time out when the host is down. This code DOES WORK with my wESP32, but that doesn't use the same rpcWiFi.h/HTTPClient.h.

Linux Mint 21.3 Cinnamon Arduino IDE 2.2.1 Wio Terminal with up to date firmware

vossnick avatar Feb 08 '24 15:02 vossnick