see the Example
ESP8266HTTPClient / BasicHTTPClient
this will give you the information if your resource was reachable or not:
/**
Check web sites for availability
https://forum.arduino.cc/t/multiple-webserver-pages-in-master-webserver-page-problem-with-iframe/1101573/12
by noiasca
2023-03-18
*/
#include <Arduino.h>
#ifdef ARDUINO_ARCH_ESP8266 // libraries if you compile for a ESP8266
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h> // for the webserver
//#include <ESP8266mDNS.h> // Bonjour/multicast DNS, finds the device on network by name
//#include <ArduinoOTA.h> // OTA Upload via ArduinoIDE
//#include <Preferences.h> // "preferences" for ESP8266 V2.0.0 by Volodymyr Shymanskyy https://github.com/vshymanskyy/Preferences/
#include <ESP8266HTTPClient.h>
#endif
//#include <credentials5582.h> // if you have an external file with your credentials you can use it - comment or remove it - if not available
#ifndef STASSID // either use an external .h file containing STASSID and STAPSK - or
// // add defines to your boards - or
#define STASSID "your-ssid" // ... modify these line to your SSID
#define STAPSK "your-password" // ... and set your WIFI password
#endif
// connect to the wifi
void wifiConnect() {
const byte wifiActivityPin = 2;
WiFi.mode(WIFI_STA);
WiFi.begin(STASSID, STAPSK);
// Wait for connection
int8_t i = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print('.');
if (wifiActivityPin < 255) digitalWrite(wifiActivityPin, !digitalRead(wifiActivityPin)); // blink one pin as indicator
i++;
if (i >= 80) {
Serial.println();
i = 0;
}
}
Serial.println();
}
struct Site {
String resource; // a web resource
uint32_t previousMillis; // timestamp of last check
int result; // result of last check
};
// define the sites to be checked
Site site[] {
{"http://172.18.67.66", 0, 0},
{"http://172.18.67.68", 0, 0},
{"http://172.18.67.100", 0, 0}, // in my testcase this site will fail
};
constexpr size_t noOfSites = sizeof(site) / sizeof(site[0]);
// this function checks each site one by one
// and stores the result in the global structure
void checkOneOfManySites() {
static uint32_t previousMillis = 0; // time management
static size_t currentSite = 0; //index of site to be tested
WiFiClient client;
HTTPClient http;
if (millis() - previousMillis > 5000) {
Serial.print("[HTTP] check "); Serial.println(site[currentSite].resource);
if (http.begin(client, site[currentSite].resource)) { // HTTP
http.setTimeout(1000); // must be set after http.begin! read https://github.com/espressif/arduino-esp32/issues/1433
int httpCode = http.GET();
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// httpCode will be negative on error
http.end();
site[currentSite].previousMillis = millis();
site[currentSite].result = httpCode;
currentSite++;
if (currentSite >= noOfSites) currentSite = 0;
}
previousMillis = millis(); // this
}
}
// checks all sites if they are available
void checkMySites() {
WiFiClient client;
HTTPClient http;
for (auto & i : site) {
Serial.print("[HTTP] check "); Serial.println(i.resource);
if (http.begin(client, i.resource)) { // HTTP
int httpCode = http.GET();
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// httpCode will be negative on error
http.end();
i.previousMillis = millis();
i.result = httpCode;
}
}
}
// check one site and print the result
void checkOneSite() {
WiFiClient client;
HTTPClient http;
if (http.begin(client, "http://172.18.67.66")) { // HTTP
int httpCode = http.GET();
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
http.end();
}
}
// just from the example BasicHttpClient
void checkExampleSite() {
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
if (http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html")) { // HTTP
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
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP} Unable to connect\n");
}
}
// this is just a quick debug print of the (previous) state of each site
void debugPrint() {
static uint32_t previousMillis = 0; // time management
if (millis() - previousMillis > 1000) {
previousMillis = millis();
for (auto &i : site) {
Serial.print(i.resource); Serial.print("\t");
Serial.print(i.result); Serial.print("\t");
Serial.print(i.previousMillis / 1000); Serial.print("\n");
}
}
}
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println(F("check webpages"));
wifiConnect();
checkMySites(); // optional - just to get a first result
}
void loop() {
checkOneOfManySites();
debugPrint();
// don't block code with dirty delays
}
This will print out something like:
09:28:12.305 -> [HTTP] check http://172.18.67.68
09:28:12.359 -> [HTTP] GET... code: 200
09:28:12.359 -> http://172.18.67.66 200 10
09:28:12.359 -> http://172.18.67.68 200 15
09:28:12.359 -> http://172.18.67.100 -1 10
09:28:13.361 -> http://172.18.67.66 200 10
09:28:13.361 -> http://172.18.67.68 200 15
09:28:13.361 -> http://172.18.67.100 -1 10
09:28:14.363 -> http://172.18.67.66 200 10
09:28:14.363 -> http://172.18.67.68 200 15
09:28:14.363 -> http://172.18.67.100 -1 10
09:28:15.352 -> http://172.18.67.66 200 10
09:28:15.352 -> http://172.18.67.68 200 15
09:28:15.352 -> http://172.18.67.100 -1 10
09:28:16.373 -> http://172.18.67.66 200 10
09:28:16.373 -> http://172.18.67.68 200 15
09:28:16.373 -> http://172.18.67.100 -1 10
09:28:17.328 -> [HTTP] check http://172.18.67.100
09:28:22.650 -> [HTTP] GET... code: -1
09:28:22.650 -> http://172.18.67.66 200 10
09:28:22.650 -> http://172.18.67.68 200 15
09:28:22.650 -> http://172.18.67.100 -1 25
you see either a 200 for a good resource or a negative value if the connection fails.
PS.: the default timeout of a page is 5000ms, so I reduced it to 1000ms. It could be even less for a local resource.
How I came to that code
this will describe how I came up with that code:
I started with the Example "BasicHTTPClient".
Cleaning it up a little bit and use functions for things belonging together. For example the wifiConnect() or the check function checkExampleSite()
Then play around with a single hardcoded site --> checkOneSite()
Put several sites in an array of structure and loop through that array --> checkMySites()
Get rid of the delays(). Make a function based on "Blink Without Delay" to check one site after the other. --> checkOneOfManySites()
and finally, add a simple print output. debugPrint()
all in all it took me around 90 minutes including to google how to reduce that weired http timeout and writing all that text. TL;DR 