Project: ESP32 WiFi-Controlled LED
Build a web interface to control an LED on your ESP32 from any browser on your network.
Components Needed
| Component | Quantity |
|---|---|
| ESP32 DevKit | 1 |
| LED | 1 |
| 220Ω resistor | 1 |
| Breadboard | 1 |
| Jumper wires | ~5 |
Circuit
ESP32 GPIO 2 ────── 220Ω → LED anode → LED cathode → GND
Complete Code
#include <WiFi.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
WiFiServer server(80);
int ledPin = 2;
bool ledState = false;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Connect to WiFi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
// Send HTTP headers
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// Send HTML 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>body { font-family: sans-serif; text-align: center; padding: 50px; }");
client.println("button { font-size: 24px; padding: 20px 40px; margin: 10px; }");
client.println(".on { background-color: #4CAF50; color: white; }");
client.println(".off { background-color: #f44336; color: white; }");
client.println("</style></head>");
client.println("<body><h1>ESP32 LED Control</h1>");
if (ledState) {
client.println("<p>LED is ON</p>");
client.println("<a href=\"/off\"><button class=\"off\">TURN OFF</button></a>");
} else {
client.println("<p>LED is OFF</p>");
client.println("<a href=\"/on\"><button class=\"on\">TURN ON</button></a>");
}
client.println("</body></html>");
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
// Check for GET requests
if (currentLine.endsWith("GET /on")) {
digitalWrite(ledPin, HIGH);
ledState = true;
}
if (currentLine.endsWith("GET /off")) {
digitalWrite(ledPin, LOW);
ledState = false;
}
}
}
client.stop();
}
}How It Works
- WiFi connection — ESP32 connects to your WiFi network
- Web server — Listens on port 80 for HTTP requests
- HTML page — Serves a simple page with ON/OFF buttons
- Request handling — Parses GET requests for
/onand/off - LED control — Sets GPIO 2 HIGH or LOW based on request
Key idea:
This is a basic web server. For production, add authentication, use HTTPS, and handle multiple concurrent clients properly.
Portfolio Checklist
- ESP32 DevKit with LED on GPIO 2
- Web interface accessible at IP address
- Toggle LED on/off via browser
- WiFi credentials in sketch
- Git repo with meaningful commits
Resources
- Random Nerd Tutorials - ESP32 Web Server: https://randomnerdtutorials.com/esp32-web-server-arduino-ide/
- Video: ESP32 Web Server - https://www.youtube.com/watch?v=CB5WQGHy8fY