Checking if IP, gateway and mask is valid

ello, I’m currently development a web server in ESP32-EVB Olimex Board and I’m having some trouble in order to configure the server, since the client can change the IP, gateway and mask in the interface of the web page I can’t find a proper solution in order to test the incoming input in order to configure the server connection.

I tried to make a function that connects to the new server IP and even if I send a bad IP (I.E It returns that he make the connection and obtained a response:

The problem is, if I send a wrong IP the testClient Function will return true, but if I try to access to the web server interface it doesn’t resolve the connection. What can I do?

#include <HTTPClient.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>

// Replace with your network credentials
const char* ssid = "xxx";
const char* password = "xxx";

AsyncWebServer server(80);

bool testClient(const char* serverName)
  HTTPClient http;
  // Your IP address with path or Domain name with URL path
  // Send HTTP GET request
  int httpResponseCode = http.GET();
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    return true;
  return false;

void InicioEstatico(IPAddress localIp,IPAddress gateway,IPAddress mask){
  int counter = 0;
  WiFi.config(localIp, gateway, mask);
  while (status != WL_CONNECTED && counter < 10) {
    Serial.println("Conectando al WiFi..");
    status = WiFi.begin(ssid, password);
  String serverName = "http://"+localIp.toString()+"/verificar";
  if (!testClient(serverName.c_str())){
    Serial.println("The connection failed, returning to DHCP");
    WiFi.config(0U, 0U, 0U);
    WiFi.begin(ssid, password);

void iniciarServidor(){
 // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(200, "text/plain", "MAINPAGE");

  server.on("/verificar", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", "OK");
  // Start server

void setup(){
IPAddress localIp(10, 10, 10, 10);
IPAddress gateway(10, 10, 10, 10);
IPAddress subnet(255, 255, 255, 0);

 void loop(){

If I read your code correctly, you are first setting yourself up with a static IP like and then check if is reachable. This is fundamentally flawed, as will be recognized by the network stack as its own IP and will always give you a good result…

Checking from the device itself whether it can be seen by some other device is… difficult. Maybe you should try to ping the router (router ping okay → is the correct network config). But then you are writing

here but if you are joining another WiFi, you are not the gateway. This should be something like

There are libraries that help you execute the ping.

1 Like