PlatformIO Community

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 10.10.10.10) 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
  http.begin(serverName);
  // Send HTTP GET request
  int httpResponseCode = http.GET();
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    http.end();
    return true;
  }
  http.end();
  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);
    delay(1000);
    counter++;
  }
  iniciarServidor();
  String serverName = "http://"+localIp.toString()+"/verificar";
  if (!testClient(serverName.c_str())){
    Serial.println("The connection failed, returning to DHCP");
    server.end();
    WiFi.disconnect(true);
    WiFi.config(0U, 0U, 0U);
    WiFi.begin(ssid, password);
    delay(1000);
    iniciarServidor();
  }
}

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
  server.begin();
}

void setup(){
Serial.begin(115200);
IPAddress localIp(10, 10, 10, 10);
IPAddress gateway(10, 10, 10, 10);
IPAddress subnet(255, 255, 255, 0);
InicioEstatico(localIp,gateway,mask);
}

 void loop(){
 }

If I read your code correctly, you are first setting yourself up with a static IP like 10.10.10.10 and then check if http://10.10.10.10/verificar is reachable. This is fundamentally flawed, as 10.10.10.10 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 -> 10.10.10.0/24 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 10.10.10.1.

There are libraries that help you execute the ping.

1 Like