ESP32 BASICS · LESSON 12
Building a Simple Web Server
Serving an HTML control page from the ESP32 itself so any browser on the network can flip a pin.
The same networking stack that lets the ESP32 make requests also lets it answer them. Running a small web server on the board means any browser on the network can load a page it serves — a simple way to build a control panel with no app required.
web_server.ino
#include
#include
WebServer server(80);
bool ledState = false;
void handleRoot() {
String page = "ESP32 Control
"
"Toggle LED";
server.send(200, "text/html", page);
}
void handleToggle() {
ledState = !ledState;
digitalWrite(2, ledState);
server.sendHeader("Location", "/");
server.send(303);
}
void setup() {
pinMode(2, OUTPUT);
WiFi.begin("your-network-name", "your-network-password");
while (WiFi.status() != WL_CONNECTED) delay(300);
server.on("/", handleRoot);
server.on("/toggle", handleToggle);
server.begin();
}
void loop() {
server.handleClient();
}
Why this pattern works
server.handleClient() checks for waiting requests each time through the loop and calls the matching handler function — no blocking delays, so the board stays responsive.
KEY IDEA
A page served from the ESP32 only needs a browser — no app, no cloud account, and it still works if the internet is down, as long as you're on the same Wi-Fi network.