Wio Terminal HTTPClient http.GET hangs when host is down

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);
}

Hi there]
The Http: Begin is after the Timeouts?

http.begin("http://192.168.226.205/dt.php"); //HTTP
http.setTimeout(5);
http.setConnectTimeout(5);

Change that.
should work.
HTH
GL :slight_smile: PJ

Sadly that did not seem to work. It still hangs. I even tried:
http.begin(“http://192.168.226.205/dt.php”); //HTTP
http.setTimeout(1);
http.setConnectTimeout(1);

Emphasis on the “1”, for a short duration. I have again tried many values between 1 and 5000. I also added a USE_SERIAL.print("[HTTP] END GET…\n"); after the http.GET to immidiately see that it has gotten past the GET. It just does not to work?

Hi there,
Hmmm, That is a pita for sure.
Perhaps put some delay between the steps to give the Http time to respond or process the requests,
You are correct though , it should time-out when the host is down.Strange behavior
Try some delay’s and LMK
GL :slight_smile: PJ
:v:

It still hangs on the http.GET. I have tried many different timings and delays.
Here is the tightened up the code:
#include <Arduino.h>
#include <rpcWiFi.h>
#include <HTTPClient.h>

const char ssid[] = “”;
const char password[] = “”;

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);
}

Hi there, So After thinking about this issue ,
You sure it’s shouldn’t be a Request Timeout Var being set?, not a GET?
Have you tried to change it to request?
LMK
HTH
GL :slight_smile: PJ

So I pulled out my old trusty wESP32 and the code works as expected. So the problem I would say is somewhere within the wio library.

I am going to pull out some other boards I have, as this comparison is not apples to apples because the wESP32 is a POE ethernet ESP32, not WiFi.

Hi there,
Interesting, so the only dif is one is directly on network and the other is WiFi?
if so , Likely DNS issue or DHCP not assigning IP correctly on AP…could be?
HTH
GL :slight_smile: PJ :v:

It goes directly to an ip, so no DNS to be had. The DHCP is supplying an address which is easily confirmed by ETH.localIP() on wESP and you can use the same function portion on wifi. It has to be in the library. I know there were bug reports some years ago in these same functions, perhaps they forked it before it was fixed or something. Do know where you report something like this?

1 Like

Hi there,
So what would happen if you do something…Like?

Char* msg;
msg =GET / HTTP/1.1\r\nHost; www.google.com\r\n\r\n;

Straw grasping , LOL
HTH
GL :slight_smile: PJ

But the current code works on other platforms. Pretty sure that means it’s the wio library. Do you know where you report something like this? I’m going to give it another google.

I opened a ticket on github, that seems to be where this should go. If you have any other thoughts, let me know.

1 Like

Hi There,
:smiling_face: GREAT move, and Yes indeed, If it does work on other hardware, then must be the LIB, kinda surprising something so small, wrecking the whole thing.
GL :slight_smile: PJ