ESP32 Web Server and Web Control
Now we reach one of the most useful ESP32 topics.
With a web server, you can control ESP32 hardware directly from a browser.
For example:
📱 Phone / 💻 Laptop │ Wi-Fi │ ▼ ┌──────────────┐ │ ESP32 │ │ Web Server │ └──────┬───────┘ │ ┌───────┼────────┐ ▼ ▼ ▼ 💡 🌀 🚪 Light Fan Door
You can open a webpage such as:
http://192.168.1.105
and control the ESP32.
1. What is a Web Server?
A web server is software that listens for requests from clients such as browsers and sends back a response.
In our project:
Browser = Client ESP32 = Server
Example:
Phone │ │ GET / ▼ ESP32 Web Server │ │ HTML ▼ Phone Browser
The browser displays the HTML page returned by the ESP32.
2. Basic Architecture
Wi-Fi Router / \ / \ Phone ESP32 │ │ │ HTTP Request │ └─────────────────► │ Web Server │ ▼ GPIO Control
For local control, your phone and ESP32 generally need to be reachable on the same network.
3. HTTP
Web browsers communicate with web servers using HTTP/HTTPS.
For example:
GET /
means:
Give me the main webpage.
Another request could be:
GET /led/on
meaning:
Turn the LED on.
Conceptually:
Browser │ │ GET /led/on ▼ ESP32 │ ▼ GPIO HIGH │ ▼ LED ON
4. ESP32 Web Server Library
With Arduino-ESP32, you can use:
#include <WiFi.h> #include <WebServer.h>
Then create:
WebServer server(80);
Here:
80
is the standard HTTP port.
5. Basic Web Server
Let's start with the simplest example.
#include <WiFi.h> #include <WebServer.h> const char* ssid = "YourWiFi"; const char* password = "YourPassword"; WebServer server(80); void handleRoot() { server.send( 200, "text/html", "<h1>Hello from ESP32!</h1>" ); } void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi Connected"); Serial.println(WiFi.localIP()); server.on("/", handleRoot); server.begin(); Serial.println("Web Server Started"); } void loop() { server.handleClient(); }
6. What Happens?
After uploading:
ESP32 │ └── Connects to Wi-Fi │ ▼ IP Address │ ▼ Web Server starts
Serial Monitor might show:
WiFi Connected 192.168.1.105 Web Server Started
Open your browser:
http://192.168.1.105
You'll see:
Hello from ESP32!
7. Understanding server.on()
This line:
server.on("/", handleRoot);
means:
URL: / When requested: call handleRoot()
For example:
server.on("/about", handleAbout);
means:
http://ESP32-IP/about
will call:
handleAbout();
8. Understanding server.send()
Example:
server.send( 200, "text/html", "<h1>Hello</h1>" );
There are three important parts:
200
HTTP status code:
200 = OK
text/html
The response is HTML.
HTML content
<h1>Hello</h1>
9. server.handleClient()
This is very important:
server.handleClient();
It allows the ESP32 web server to process incoming client requests.
Without proper request handling, your server won't behave as expected.
10. Control an LED
Now let's make something useful.
Browser │ ├── ON │ └── OFF │ ▼ ESP32 │ ▼ LED
Complete Example
#include <WiFi.h> #include <WebServer.h> const char* ssid = "YourWiFi"; const char* password = "YourPassword"; WebServer server(80); #define LED_PIN 2 void handleRoot() { String html = R"rawliteral( <!DOCTYPE html> <html> <head> <title>ESP32 Control</title> </head> <body> <h1>ESP32 LED Control</h1> <a href="/led/on"> <button>LED ON</button> </a> <a href="/led/off"> <button>LED OFF</button> </a> </body> </html> )rawliteral"; server.send(200, "text/html", html); } void ledOn() { digitalWrite(LED_PIN, HIGH); server.send( 200, "text/html", "<h1>LED ON</h1><a href='/'>Back</a>" ); } void ledOff() { digitalWrite(LED_PIN, LOW); server.send( 200, "text/html", "<h1>LED OFF</h1><a href='/'>Back</a>" ); } void setup() { Serial.begin(115200); pinMode(LED_PIN, OUTPUT); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.print("ESP32 IP: "); Serial.println(WiFi.localIP()); server.on("/", handleRoot); server.on("/led/on", ledOn); server.on("/led/off", ledOff); server.begin(); Serial.println("Web Server Started"); } void loop() { server.handleClient(); }
11. How the Button Works
The HTML contains:
<a href="/led/on"> <button>LED ON</button> </a>
When you click it:
Browser │ │ GET /led/on ▼ ESP32 │ ▼ ledOn() │ ▼ GPIO HIGH │ ▼ LED ON
Similarly:
/led/off
turns it off.
12. Building a Smart-Home Dashboard
Now you can expand the idea.
╔══════════════════════════════╗ ║ ESP32 SMART HOME ║ ╠══════════════════════════════╣ ║ ║ ║ 💡 LIGHT ║ ║ [ ON ] [ OFF ] ║ ║ ║ ║ 🌀 FAN ║ ║ [ ON ] [ OFF ] ║ ║ ║ ║ 🚪 DOOR ║ ║ [ OPEN ] [ CLOSE ] ║ ║ ║ ║ 🔒 SECURITY ║ ║ [ ARM ] [ DISARM ] ║ ║ ║ ╚══════════════════════════════╝
This fits perfectly with your IoT ZONE Smart Home project.
13. Multiple GPIO Controls
For example:
#define LIGHT_PIN 25 #define FAN_PIN 26 #define RELAY_PIN 27
Routes:
server.on("/light/on", lightOn); server.on("/light/off", lightOff); server.on("/fan/on", fanOn); server.on("/fan/off", fanOff);
Then:
/light/on /fan/on
control different devices.
14. Reading Sensors
The web server doesn't only control hardware.
It can also display sensor data.
For example:
DHT22 │ ├── Temperature = 29.5°C └── Humidity = 61% │ ▼ ESP32 │ ▼ Web Page
Browser:
╔══════════════════════════╗ ║ SENSOR DATA ║ ╠══════════════════════════╣ ║ Temperature: 29.5 °C ║ ║ Humidity: 61 % ║ ║ Motion: Detected ║ ╚══════════════════════════╝
15. Dynamic Web Pages
Instead of sending fixed HTML:
Temperature: 25°C
you can generate HTML using the current sensor value.
Conceptually:
String html = "<h1>Temperature: "; html += temperature; html += " °C</h1>";
Then the ESP32 sends the current value.
16. HTML + CSS
You can make the ESP32 dashboard attractive using CSS.
Example:
<style> body { font-family: Arial; text-align: center; } button { padding: 15px; margin: 10px; font-size: 18px; } </style>
Then:
ESP32 │ └── HTML + CSS │ ▼ Browser
You can create surprisingly attractive dashboards directly from the ESP32.
17. ESP32 as an Access Point
You don't necessarily need a router.
You can combine:
ESP32 │ └── Creates Wi-Fi │ ▼ Phone │ ▼ http://192.168.4.1
Then the phone directly controls the ESP32.
This is useful for:
- Robots
- RC cars
- Portable devices
- Field projects
- Configuration pages
18. Web Control of Your Robot
This is especially relevant to your ESP32 car project.
Phone │ Wi-Fi │ ▼ ESP32 Car │ L298N │ ┌───────┴───────┐ ▼ ▼ Left Motors Right Motors
Webpage:
🚗 ESP32 CAR [ ↑ ] [ ← ] [STOP] [ → ] [ ↓ ]
Requests could be:
/forward /backward /left /right /stop
19. Web Server + Servo
You can also control a servo:
Browser │ │ /door/open ▼ ESP32 │ ▼ Servo │ ▼ Door
For your smart-home model:
DOOR → OPEN
could rotate the servo.
20. Web Server + Relay
Example:
Browser │ ▼ ESP32 │ ▼ Relay │ ▼ Fan / Light
The relay should be appropriately rated for the load and wired safely, especially for mains electricity.
21. GET and POST
Two important HTTP methods are:
GET
Usually used to request a resource or trigger simple URL-based actions.
GET /led/on
POST
Commonly used to submit data.
For example:
POST /login
with:
username password
For larger or more structured applications, POST is generally preferable to putting sensitive data into a URL.
22. URL Parameters
You can also send values through a URL.
For example:
/set?brightness=80
Conceptually:
Browser │ │ brightness=80 ▼ ESP32 │ ▼ PWM = 80
This becomes useful for:
- LED brightness
- Servo angle
- Motor speed
- Temperature thresholds
23. Web Server Security
A local ESP32 web server is not automatically secure.
Avoid exposing an unauthenticated control interface directly to the public Internet.
Bad architecture:
Internet │ ▼ ESP32 │ Relay
Anyone who can access the control interface could potentially control the hardware.
A safer approach is to use appropriate authentication, encryption, network segmentation, and a secure cloud/backend architecture when remote access is required.
24. Common Problems
❌ Browser cannot open ESP32
Check:
1. ESP32 connected to Wi-Fi? 2. Correct IP? 3. Phone on same network? 4. Server started? 5. Correct port?
❌ IP keeps changing
Use a suitable static-IP strategy or DHCP reservation.
❌ Buttons work but page doesn't update
Your server may be returning a static page.
For live updates, later we'll use:
JavaScript AJAX / Fetch WebSocket
❌ Page is too large
Remember that ESP32 has limited RAM and flash.
A huge HTML/CSS/JavaScript application may consume significant resources.
For larger applications, a better architecture can be:
ESP32 │ API / MQTT │ ▼ External Web Application
25. Web Server Architecture
The complete concept is:
Browser │ HTTP Request │ ▼ ┌──────────┐ │ ESP32 │ │ Web │ │ Server │ └────┬─────┘ │ ┌───────┼────────┐ │ │ │ ▼ ▼ ▼ GPIO Sensors Servo │ │ │ ▼ ▼ ▼ Relay DHT22 Door
🧠 Quick Summary
Remember:
ESP32 Web Server │ ├── Wi-Fi │ ├── HTTP │ ├── Routes │ ├── / │ ├── /led/on │ └── /led/off │ ├── HTML ├── CSS └── GPIO Control
The most important functions from today's example are:
WiFi.begin()
Connect to Wi-Fi.
WebServer server(80);
Create HTTP server.
server.on()
Create URL routes.
server.send()
Send a response.
server.handleClient()
Process incoming requests.