Web server on esp32

Hello

I made a very simple sketch to test a web server on ESP32:

#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>

const char* ssid = "Net5231";
const char* password = "ckl8796";
WebServer server(80);

// Define routing
void restServerRouting() {
  server.on("/", HTTP_GET, []() {
    server.send(200, F("text/html"),
                F("Welcome to the REST Web Server"));
  });
}

// Manage not found URL
void handleNotFound() {
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  if (MDNS.begin("esp32test")) {
    Serial.println("MDNS responder started");
  }
  Serial.print(WiFi.localIP());
  restServerRouting();
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
server.handleClient();
}

Turns out it works fine on notebook/windows10 when I try to open with "esp32test.local"

However, on Android, the same does not happen:

But if I type the IP, it works:

I'm quite new to this issue, the first time I've worked with this, but is there a chance that the problem is in the ESP32 code and MDNS.begin()?

Thanks

I understand that Android only recently (this year) provided supported mDNS.

What version of Android are you running? Older versions may still not work.

Android version 12.
Is there another solution to the problem other than using mDNS in the Arduino sketch? The intention was to create POST functions, simulating an API.

You could tell your DHCP server (usually your WiFi router) to always assign a specific IP address to that MAC address. Then it would be safe to use the IP address.

Would it be assigning a fixed IP?
Like this: ESP8266 NodeMCU Static/Fixed IP Address (Arduino IDE) | Random Nerd Tutorials

Yes... that shows both ways to do it... either from the client (ESP8266) or from the router side.

A fixed IP on the LAN side of the router (NAT).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.