esp32_webserver | Last modified: 1749060114 | Edit | Home

Client Wifi / Server HTTP

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

const char *ssid = "Your SSID";
const char *password = "P4sswOrd";

WebServer server(80);

void handleRoot() {
  String html = "<!DOCTYPE html>";
  html += "<html>\n";
  html += "<head>\n";
  html += "<title>Server ESP32</title>\n";
  html += "</head>\n";
  html += "<body>\n";
  html += "<h1>Hello world</h1>\n";
  html += "</body>\n";
  html += "</html>";
  server.send(200, "text/html", html);
}

void setup(void) {

  Serial.begin(115200);
  while (!Serial) continue;

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  // URL esp32.local
  //if (MDNS.begin("esp32")) {
  //  Serial.println("MDNS responder started");
  //}

  server.on("/", handleRoot);

  server.onNotFound([]() {
    server.send(404, "text/plain", "Not Found");
  });

  server.begin();
  Serial.println("HTTP server started");
}

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

Hotspot

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

/* Put your SSID & Password */
const char* ssid = "ESP32";  // Enter SSID here
const char* password = "12345678";  //Enter Password here

/* Put IP Address details */
IPAddress local_ip(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

WebServer server(80);

void handleRoot() {
  String html = "<!DOCTYPE html>";
  html += "<html>\n";
  html += "<head>\n";
  html += "<title>Server ESP32</title>\n";
  html += "</head>\n";
  html += "<body>\n";
  html += "<h1>Hello world</h1>\n";
  html += "</body>\n";
  html += "</html>";
  server.send(200, "text/html", html);
}

void handle_NotFound(){
  server.send(404, "text/plain", "Not found");
}

void setup() {
  Serial.begin(115200);

  WiFi.softAP(ssid, password);
  WiFi.softAPConfig(local_ip, gateway, subnet);
  delay(100);

  // URL esp32.local
  if (MDNS.begin("esp32")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);
  server.onNotFound(handle_NotFound);

  server.begin();
  Serial.println("HTTP server started");
}
void loop() {
  server.handleClient();
}