← All Projects

ESP32 Wi-Fi Web Server for LED Control

272 viewsMarch 6, 2026
ESP32WiFiWeb ServerIoTLEDHTML

Create a web-based dashboard served directly from an ESP32 that lets you toggle LEDs on and off from any browser on the same Wi-Fi network. A great introduction to IoT and embedded web servers.

Components & Supplies

×1ESP32 Dev Module
×1Red LED (5mm)
×1Green LED (5mm)
×2220Ω Resistor
×1Breadboard

Overview

The ESP32 has built-in Wi-Fi, making it perfect for IoT projects. This project runs a lightweight HTTP server on the ESP32 that serves an HTML page with buttons to control two LEDs. No external server or cloud service needed — everything runs on the microcontroller itself.

Connect the ESP32 to your Wi-Fi, open the IP address in a browser, and control the LEDs remotely.

arduinoESP32 Web Server Sketch
#include <WiFi.h>
#include <WebServer.h>

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

WebServer server(80);
const int led1 = 2;
const int led2 = 4;
bool led1State = false;
bool led2State = false;

String getHTML() {
  String html = "<!DOCTYPE html><html><head>";
  html += "<meta name='viewport' content='width=device-width, initial-scale=1'>";
  html += "<style>body{font-family:monospace;background:#1a1a2e;color:#eee;text-align:center;padding:2rem}";
  html += ".btn{display:inline-block;padding:1rem 2rem;margin:0.5rem;font-size:1.2rem;border:none;border-radius:8px;cursor:pointer;font-family:monospace}";
  html += ".on{background:#22c55e;color:#fff}.off{background:#ef4444;color:#fff}</style></head><body>";
  html += "<h1>ESP32 LED Control</h1>";
  html += "<p>LED 1: " + String(led1State ? "ON" : "OFF") + "</p>";
  html += "<a href='/led1/toggle'><button class='btn " + String(led1State ? "on" : "off") + "'>Toggle LED 1</button></a>";
  html += "<p>LED 2: " + String(led2State ? "ON" : "OFF") + "</p>";
  html += "<a href='/led2/toggle'><button class='btn " + String(led2State ? "on" : "off") + "'>Toggle LED 2</button></a>";
  html += "</body></html>";
  return html;
}

void setup() {
  Serial.begin(115200);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

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

  server.on("/", []() { server.send(200, "text/html", getHTML()); });
  server.on("/led1/toggle", []() {
    led1State = !led1State;
    digitalWrite(led1, led1State);
    server.sendHeader("Location", "/");
    server.send(303);
  });
  server.on("/led2/toggle", []() {
    led2State = !led2State;
    digitalWrite(led2, led2State);
    server.sendHeader("Location", "/");
    server.send(303);
  });
  server.begin();
}

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