Problem Description:
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?
Linux Mint 21.3 Cinnamon
Arduino IDE 2.2.1
I have removed any possible conflicting HTTPClient directories and only left /home/$user/Arduino/libraries/Seeed_Arduino_rpcWiFi/src/HTTPClient.h
This code is based on: /home/$user/Arduino/libraries/Seeed_Arduino_rpcWiFi/examples/BasicHttpClient
I have tried with and without setTimeout, setConnectTimeout and with various values treating it as seconds and milliseconds.
Please note, this code works as expected when the host is up.
Current code:
#include <Arduino.h>
#include <rpcWiFi.h>
#include <HTTPClient.h>
#define USE_SERIAL Serial
const char ssid[] = “MySSID”;
const char password[] = “MyPass”;
void setup()
{
USE_SERIAL.begin(9600);
for(uint8_t t = 4; t > 0; t–) {
USE_SERIAL.printf("[SETUP] WAIT %d…\n", t);
USE_SERIAL.flush();
delay(1000);
}
int CA = 0;
// Serial.println("Connecting to WiFi network: " + String(ssid));
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.print(".");
}
Serial.println(“Connected.”);
}
void loop() {
// wait for WiFi connection
if((WiFi.status() == WL_CONNECTED)) {
HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
http.setTimeout(5);
http.setConnectTimeout(5);
http.begin("http://192.168.226.205/dt.php"); //HTTP
USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
delay(5000);
}