What You Will Build
In this tutorial you will turn an ESP32 into a tiny web server that creates its own Wi-Fi network. Join that network from a phone or laptop, open one address in the browser, and a page with two buttons appears. Tap a button and a real LED on your breadboard switches on or off. There is no router, no internet connection, no app and no cloud account involved: the ESP32 is the access point, the web server and the LED controller in a single board.
It is the best first step into ESP32 networking because every later project builds on the same idea. A browser sends a short text request, the ESP32 reads it, does something with its pins, and sends a page back. Relay control, sensor dashboards and smart-home panels all work this way.
By the end you will know how to:
- Install the ESP32 board package and upload a sketch from the Arduino IDE
- Wire LEDs safely to 3.3 V GPIO pins with the right resistor
- Start the ESP32 as a Wi-Fi access point and find its IP address
- Read an HTTP GET request and use its path (such as
/16/on) as a command - Generate an HTML page from the sketch that reflects the current LED state
- Diagnose the most common upload, Wi-Fi and wiring problems
Time: about 45 minutes. Skill level: beginner. No soldering is needed.
Components
8 itemsNeed these parts? Get this build sheet by email
Parts list with buy links, plus new builds as they're published. By subscribing you agree to our Privacy Policy. Unsubscribe anytime.
🚨 Danger
ESP32 GPIO pins are 3.3 V only, and the maximum safe input is about 3.6 V. Connecting a 5 V signal, even briefly, can permanently damage the chip. This build only drives LEDs from the pins, so it is safe, but remember the rule before you add 5 V modules later: use a level shifter or a voltage divider on every signal line that comes from a 5 V device.
How a Web Server on a Microcontroller Works
A web page is a conversation between two programs. Your browser is the client: it opens a connection and sends a request. The ESP32 is the server: it listens on port 80, reads whatever arrives, and answers. The language they speak, HTTP, is plain text.
When you type 192.168.4.1/16/on into a browser, it sends something like this to the ESP32:
GET /16/on HTTP/1.1
Host: 192.168.4.1
User-Agent: Mozilla/5.0 (...)
Accept: text/html,application/xhtml+xml
Connection: keep-aliveThe first line is the one that matters. GET means "give me this resource", and /16/on is the path. The sketch does not own a file called 16/on. Instead it treats the path as a command: if the request contains GET /16/on, switch GPIO16 on. Whatever the path, the ESP32 then replies with a status line (HTTP/1.1 200 OK), a few headers, one blank line and the HTML of the page. The blank line matters. HTTP uses it to mark the end of the headers, and the sketch waits for it before answering.
The whole project is therefore one loop that never changes: request in, pin change, page out.
Access Point or Station?
An ESP32 can join an existing Wi-Fi network (station mode, STA) or create its own (access point mode, AP). This tutorial uses AP mode because it works anywhere, even in a workshop or a field with no router, and because the address is always the same.
| Access point (this tutorial) | Station (joins your router) | |
|---|---|---|
| Needs a router | No | Yes |
| Address of the ESP32 | 192.168.4.1 by default | Assigned by the router, can change |
| Phone keeps its internet | No, while joined to the ESP32 | Yes |
| Typical range | One room to a few rooms | Wherever your router reaches |
| Best for | First tests, demos, projects on the move | Home automation and dashboards |
Later in the tutorial you will see the five lines that switch the same sketch to station mode.
Choosing Safe GPIO Pins on the WROOM-32
The ESP32 has many pins, but they are not all equal. Before you wire anything, know which ones to avoid for a simple output:
| Pins | What to know |
|---|---|
| GPIO6 to GPIO11 | Connected to the on-board flash memory. Never use them. |
| GPIO34, 35, 36, 39 | Input only. They cannot drive an LED and have no internal pull-up or pull-down. |
| GPIO0, 2, 5, 12, 15 | Strapping pins. Their level at reset selects boot options, so something pulling them the wrong way can stop the board from booting or uploading. |
| GPIO1, 3 | UART0, used by USB serial and uploads. Leave them alone. |
| GPIO16, 17 | Free on WROOM-32 modules. On WROVER modules they are used by the external PSRAM. |
| GPIO13, 14, 18, 19, 21, 22, 23, 25, 26, 27, 32, 33 | General purpose and well behaved. |
This project uses GPIO16 and GPIO17. They are not strapping pins, they are free on a WROOM-32, and many boards label them RX2 and TX2. If your module is a WROVER, pick two pins from the last row instead and change the two ledPin constants in the sketch.
Wiring the Two LEDs
An LED is not a light bulb. It passes as much current as the circuit allows, so it always needs a series resistor. The ESP32 outputs 3.3 V, and a red LED drops roughly 2.0 V when lit, so the resistor has to absorb the remaining 1.3 V. For a target of about 6 mA:
R = (3.3 V - 2.0 V) / 0.006 A = about 217 ohms -> use 220 ohmsThat gives a clearly visible glow while staying far below what a GPIO pin can safely supply. Green, yellow and orange LEDs behave similarly. Blue and white LEDs drop about 3.0 V, which leaves almost no headroom at 3.3 V, so stay with red or green for this first build.
| From | To |
|---|---|
| ESP32 GPIO16 | 220 Ω resistor, then LED 1 anode (the long leg) |
| LED 1 cathode (short leg, flat edge) | GND rail of the breadboard |
| ESP32 GPIO17 | 220 Ω resistor, then LED 2 anode |
| LED 2 cathode | GND rail of the breadboard |
| ESP32 GND | GND rail of the breadboard |
The resistor can sit on either side of the LED, as long as it is in series. If you reverse an LED nothing breaks, it simply stays dark, so flip it around if it does not light.
Setting Up the Arduino IDE for ESP32
The ESP32 is not built into the Arduino IDE, so you add Espressif's board package once:
- Install Arduino IDE 2.x from arduino.cc.
- Open File → Preferences and paste this into Additional boards manager URLs:
https://espressif.github.io/arduino-esp32/package_esp32_index.json - Open Tools → Board → Boards Manager, search for esp32 and install esp32 by Espressif Systems.
- Choose Tools → Board → esp32 → ESP32 Dev Module.
- Plug in the board, pick its port under Tools → Port, and leave Upload Speed at 921600. If uploads fail, drop it to 115200.
The sketch uses only the built-in WiFi.h library, so there is nothing else to install. It follows the same pattern on the current 2.x and 3.x board packages. Newer packages add accept() as the preferred name for the call this sketch makes with available(); both do the same job.
💡 Tip
If no port appears after plugging in the board, try a different USB cable first. Many cables are charge-only and carry no data. If that does not help, install the driver for the USB-serial chip on your board (CP2102 or CH340, the chip is printed next to the USB connector), then replug the board.
The Complete Sketch
Copy the sketch below into a new Arduino IDE window. Change the network name and password if you like. The password must be at least 8 characters; with a shorter one softAP() fails and no network appears.
#include <WiFi.h>
// The Wi-Fi network the ESP32 will CREATE (password: 8+ characters)
const char* networkName = "ESP32-Network";
const char* networkPassword = "Esp32-Password";
WiFiServer webServer(80); // HTTP server on port 80
String httpRequest; // collects the text of one request
String ledStatePin16 = "off";
String ledStatePin17 = "off";
const int ledPin16 = 16;
const int ledPin17 = 17;
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeout = 2000; // give up on a stalled client after 2 s
void setup() {
Serial.begin(115200);
pinMode(ledPin16, OUTPUT);
digitalWrite(ledPin16, LOW);
pinMode(ledPin17, OUTPUT);
digitalWrite(ledPin17, LOW);
WiFi.softAP(networkName, networkPassword);
Serial.println("");
Serial.println("IP address: ");
Serial.println(WiFi.softAPIP());
webServer.begin();
}
void loop() {
WiFiClient client = webServer.available();
if (client) {
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client.");
String currentLine = "";
while (client.connected() && currentTime - previousTime <= timeout) {
currentTime = millis();
if (client.available()) {
char c = client.read();
Serial.write(c);
httpRequest += c;
if (c == '\n') {
if (currentLine.length() == 0) {
// A blank line means the request headers have ended: reply now
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// Treat the path of the request as a command
if (httpRequest.indexOf("GET /16/on") >= 0) {
ledStatePin16 = "on";
digitalWrite(ledPin16, HIGH);
} else if (httpRequest.indexOf("GET /16/off") >= 0) {
ledStatePin16 = "off";
digitalWrite(ledPin16, LOW);
}
if (httpRequest.indexOf("GET /17/on") >= 0) {
ledStatePin17 = "on";
digitalWrite(ledPin17, HIGH);
} else if (httpRequest.indexOf("GET /17/off") >= 0) {
ledStatePin17 = "off";
digitalWrite(ledPin17, LOW);
}
// Build the web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<style>html { font-family: monospace; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: yellowgreen; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 32px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: gray;}</style></head>");
client.println("<body><h1>ESP32 Web Server</h1>");
client.println("<p>Control LED State</p>");
// Each button shows the CURRENT state and links to the OPPOSITE command
if (ledStatePin16 == "off") {
client.println("<p><a href=\"/16/on\"><button class=\"button\">OFF</button></a></p>");
} else {
client.println("<p><a href=\"/16/off\"><button class=\"button button2\">ON</button></a></p>");
}
if (ledStatePin17 == "off") {
client.println("<p><a href=\"/17/on\"><button class=\"button\">OFF</button></a></p>");
} else {
client.println("<p><a href=\"/17/off\"><button class=\"button button2\">ON</button></a></p>");
}
client.println("</body></html>");
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
httpRequest = "";
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}#include <WiFi.h>
// The Wi-Fi network the ESP32 will CREATE (password: 8+ characters)
const char* networkName = "ESP32-Network";
const char* networkPassword = "Esp32-Password";
WiFiServer webServer(80); // HTTP server on port 80
String httpRequest; // collects the text of one request
String ledStatePin16 = "off";
String ledStatePin17 = "off";
const int ledPin16 = 16;
const int ledPin17 = 17;
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeout = 2000; // give up on a stalled client after 2 s
void setup() {
Serial.begin(115200);
pinMode(ledPin16, OUTPUT);
digitalWrite(ledPin16, LOW);
pinMode(ledPin17, OUTPUT);
digitalWrite(ledPin17, LOW);
WiFi.softAP(networkName, networkPassword);
Serial.println("");
Serial.println("IP address: ");
Serial.println(WiFi.softAPIP());
webServer.begin();
}
void loop() {
WiFiClient client = webServer.available();
if (client) {
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client.");
String currentLine = "";
while (client.connected() && currentTime - previousTime <= timeout) {
currentTime = millis();
if (client.available()) {
char c = client.read();
Serial.write(c);
httpRequest += c;
if (c == '\n') {
if (currentLine.length() == 0) {
// A blank line means the request headers have ended: reply now
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// Treat the path of the request as a command
if (httpRequest.indexOf("GET /16/on") >= 0) {
ledStatePin16 = "on";
digitalWrite(ledPin16, HIGH);
} else if (httpRequest.indexOf("GET /16/off") >= 0) {
ledStatePin16 = "off";
digitalWrite(ledPin16, LOW);
}
if (httpRequest.indexOf("GET /17/on") >= 0) {
ledStatePin17 = "on";
digitalWrite(ledPin17, HIGH);
} else if (httpRequest.indexOf("GET /17/off") >= 0) {
ledStatePin17 = "off";
digitalWrite(ledPin17, LOW);
}
// Build the web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<style>html { font-family: monospace; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: yellowgreen; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 32px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: gray;}</style></head>");
client.println("<body><h1>ESP32 Web Server</h1>");
client.println("<p>Control LED State</p>");
// Each button shows the CURRENT state and links to the OPPOSITE command
if (ledStatePin16 == "off") {
client.println("<p><a href=\"/16/on\"><button class=\"button\">OFF</button></a></p>");
} else {
client.println("<p><a href=\"/16/off\"><button class=\"button button2\">ON</button></a></p>");
}
if (ledStatePin17 == "off") {
client.println("<p><a href=\"/17/on\"><button class=\"button\">OFF</button></a></p>");
} else {
client.println("<p><a href=\"/17/off\"><button class=\"button button2\">ON</button></a></p>");
}
client.println("</body></html>");
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
httpRequest = "";
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}How the Sketch Works, Piece by Piece
Creating the network
WiFi.softAP(networkName, networkPassword) turns the ESP32 into an access point. WiFi.softAPIP() returns its address, which is 192.168.4.1 unless you change it. By default the access point accepts up to four connected devices, which is plenty for a control panel. webServer.begin() starts listening on port 80, the standard web port, so you never need to type a port number in the browser.
The state variables
The sketch remembers each LED as a text string ("on" or "off") alongside the real pin level. That string is only used to decide which button to draw. The httpRequest string collects the text of the request that is currently being read. It is emptied at the end of every request so that an old GET /16/on can never leak into the next one.
The loop: one client at a time
webServer.available() returns a client object only when a browser has connected. If nobody has, loop() falls through immediately, so the board is free to do other work. When a client does connect, the sketch reads it one character at a time. Every character is echoed to the Serial Monitor, appended to httpRequest, and also added to currentLine. When a newline arrives and currentLine is empty, the sketch has seen a blank line, which is the end of the headers, and it is time to reply.
The 2-second timeout protects you from a client that connects and then goes quiet. Without it, one stalled phone could freeze the whole server.
Using the path as a command
httpRequest.indexOf("GET /16/on") searches the request text and returns the position of the match, or -1 if it is not there. Any value of zero or more means the command was present. Each LED has its own pair of checks, so a request for /16/on never touches GPIO17.
One subtle point: the sketch sends the response headers first and parses the command afterwards. That works because the whole request has already been read at that point, but it is the reason the order of statements inside the if block matters if you rearrange the code.
Drawing the page
The client.println() lines build an HTML page one line at a time. The two if blocks are the clever part: when LED 1 is off, the page shows a green button labelled OFF whose link is /16/on. When it is on, the page shows a grey button labelled ON whose link is /16/off. So every tap asks for the opposite of what you see, and the freshly generated page always shows the true state.
Two details are worth knowing:
<meta name="viewport" ...>makes the page scale properly on a phone screen.<link rel="icon" href="data:,">gives the page an empty icon. Without it, browsers send an extra request for/favicon.icoafter every page load, which the sketch would have to answer pointlessly.
Closing the connection
Connection: close tells the browser the server will hang up after one reply, and client.stop() does exactly that. This keeps the code simple and means the server handles one request at a time, which is perfectly fine for a button panel.
Steps
- 1Build the circuit from the wiring table and double-check each LED’s direction.
- 2In the Arduino IDE choose ESP32 Dev Module, select the port and click Upload. If it stops at "Connecting....", hold the BOOT button until the upload starts, then release it.
- 3Open the Serial Monitor at 115200 baud and press the EN (reset) button on the board. You should see "IP address:" followed by 192.168.4.1.
- 4On your phone or laptop, open the Wi-Fi list and join ESP32-Network with the password Esp32-Password.
- 5If your phone warns that the network has no internet, choose to stay connected. Turn mobile data off if the page will not load.
- 6Open a browser and go to http://192.168.4.1 (type http, not https).
- 7Tap the buttons. The matching LED should switch on and off, and the button label and colour should change.
- 8Look at the Serial Monitor: every tap prints the incoming GET request.
What the Serial Monitor Should Show
After you tap a button, the monitor prints the raw request the ESP32 received. It will look similar to this (the browser headers vary from device to device):
IP address:
192.168.4.1
New Client.
GET /16/on HTTP/1.1
Host: 192.168.4.1
Connection: keep-alive
Accept: text/html,application/xhtml+xml
...
Client disconnected.Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
ESP32-Network does not appear | Sketch not running, or password shorter than 8 characters | Check the Serial Monitor for the IP line, press EN, and use a password of 8+ characters. |
| Phone joins but the page will not load | Mobile data is taking over, or you typed https:// | Turn mobile data off and type http://192.168.4.1. |
| Upload fails with "Failed to connect to ESP32" | Board not in download mode or a charge-only cable | Hold BOOT while uploading, release when "Connecting" ends, and use a data cable. |
| Board resets when Wi-Fi starts | Weak USB port or cable causing a brownout | Plug into a different port or a powered hub, and use a shorter cable. |
| Page loads but an LED never lights | LED reversed, wrong pin, or no resistor path | Flip the LED, confirm GPIO16/17 in the wiring, and test the LED with a 3.3 V pin. |
| Buttons work but the LED is dim | Resistor too large or a blue/white LED | Use 220 Ω with red or green; try 100 Ω for blue or white. |
| A different device cannot connect | Access point is full | The default limit is four clients. Disconnect one device. |
Joining Your Home Wi-Fi Instead
Access point mode is ideal for demos, but for a home project you will usually want the ESP32 on your normal network, so your phone keeps its internet. Replace the WiFi.softAP call and the IP printout in setup() with this, and add the two credential lines at the top:
const char* homeSsid = "YOUR_WIFI_NAME";
const char* homePassword = "YOUR_WIFI_PASSWORD";
// inside setup(), instead of WiFi.softAP(...):
WiFi.mode(WIFI_STA);
WiFi.begin(homeSsid, homePassword);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); // open this address in your browserThree things change. The ESP32 can only join 2.4 GHz networks, so it will never see a 5 GHz-only network. The address is chosen by your router, so read it from the Serial Monitor. And because the router may hand out a different address after a restart, set a DHCP reservation for the ESP32 in your router settings if you want it to stay put.
Limits of This Design, and How to Go Further
This sketch is deliberately simple, and it helps to know where it stops:
- GET requests that change things. Using a link to switch an LED is fine for a demo, but a browser or a link-previewing app may follow a link on its own. For anything that matters, send commands with POST requests or small JavaScript
fetch()calls. - One client at a time. The loop is blocking. For a page with live updates, use the
WebServerlibrary included with the ESP32 package, or an asynchronous web server library. - Security. The only protection is the Wi-Fi password. Do not expose this kind of server to the internet.
- Page refresh. Each tap reloads the whole page. JavaScript can update the button without a reload.
Ideas to try next:
- Add a third LED on GPIO18 by copying one block of state, one pair of
indexOfchecks and one button. - Dim an LED with PWM instead of switching it fully on or off.
- Return a small status line showing how long the board has been running with
millis(). - Swap the LEDs for a 3.3 V logic relay module so the same page controls a low-voltage device.
🚨 Danger
Never connect mains voltage to anything on a breadboard. If you later switch a lamp or appliance, use a properly rated relay or SSR module inside an insulated enclosure, and have the mains wiring done by someone qualified. Practise with low-voltage loads first.
Frequently Asked Questions
Can I use this on an ESP32-S3, C3 or other ESP32 boards? Yes, the code is portable. Only the pin numbers change, so check your board's pinout and avoid its strapping and flash pins.
Why does my phone say there is no internet? Because there isn't: in access point mode the ESP32 is not connected to the internet. Choose to stay connected to the network anyway.
How many phones can control it at once? Up to four by default, but the server answers them one at a time. All of them see the correct LED state because the page is rebuilt on every request.
Can I change the address from 192.168.4.1? Yes, with WiFi.softAPConfig(), but the default is fine for most projects.
Is the ESP32 5 V tolerant? No. Its pins are 3.3 V. Use a level shifter or voltage divider whenever you connect a 5 V signal.
Summary
You built an ESP32 that creates its own Wi-Fi network, serves a web page and turns the request path into pin changes. You learned which pins are safe to use, how to size an LED resistor for 3.3 V, how a browser request is structured, and how to move the same code onto your home network. Those four ideas (request, parse, act, respond) are the foundation of nearly every connected ESP32 project you will build from here.

