Free Nationwide Doorstep Delivery on orders over ₹499
← All 10 Projects•Intermediate•60 mins

ESP32 Wi-Fi Local Web Server LED Control

Host an embedded HTTP web server on ESP32 to toggle an LED remotely from any smartphone or browser on the local Wi-Fi network.

Real-World Engineering Application

Smart plugs, wireless industrial actuators, and smart appliance web dashboards.

Physics & Working Principle

ESP32 connects to an 802.11 b/g/n Wi-Fi Access Point and obtains a local IP via DHCP. An embedded HTTP daemon listens on port 80. When a client browser accesses HTTP endpoints (e.g., /H or /L), the microcontroller toggles GPIO states and serves dynamic HTML.

Key Learning Outcome:Understand TCP/IP, HTTP GET protocols, local IP addressing, and building responsive embedded web control dashboards.

Pin Connection & Wiring Table

Component Pin / TerminalMicrocontroller Pin (ESP32 / Arduino)Wiring Note
LED Anode via 220ΩGPIO 2Actuator output
LED CathodeGNDGround

Step-by-Step Plug & Play Assembly

1

Wire the LED and 220Ω resistor to GPIO 2.

2

Replace YOUR_SSID and YOUR_PASSWORD in the code with your local home or mobile hotspot Wi-Fi credentials.

3

Upload the program and open the Serial Monitor at 115200 baud to find your assigned IP address.

Complete Tested Source Code

// NovaCircuit Project 7: ESP32 Wi-Fi Web Server
#include <WiFi.h>

const char* ssid     = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

WiFiServer server(80);
const int LED_PIN = 2;

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);

  Serial.print("Connecting to Wi-Fi ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("
Wi-Fi Connected successfully!");
  Serial.print("Access Web Dashboard at: http://");
  Serial.println(WiFi.localIP());
  
  server.begin();
}

void loop() {
  WiFiClient client = server.available();
  if (!client) return;

  String request = client.readStringUntil('\r');
  client.flush();

  if (request.indexOf("/H") != -1) {
    digitalWrite(LED_PIN, HIGH);
  } else if (request.indexOf("/L") != -1) {
    digitalWrite(LED_PIN, LOW);
  }

  // Send HTTP response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-type:text/html");
  client.println("Connection: close");
  client.println();
  
  client.println("<!DOCTYPE html><html><head><meta name='viewport' content='width=device-width, initial-scale=1'>");
  client.println("<style>body{font-family:sans-serif;text-align:center;padding:40px;background:#0a192f;color:#fff;}");
  client.println(".btn{padding:16px 32px;font-size:18px;margin:10px;border-radius:8px;border:none;cursor:pointer;}");
  client.println(".on{background:#00e5ff;color:#0a192f;font-weight:bold;} .off{background:#ff4d4d;color:#fff;}</style></head>");
  client.println("<body><h2>NovaCircuit Smart Controller</h2>");
  client.println("<a href='/H'><button class='btn on'>TURN ON</button></a>");
  client.println("<a href='/L'><button class='btn off'>TURN OFF</button></a>");
  client.println("</body></html>");
  client.stop();
}
Code Logic Breakdown:

The ESP32 acts as a web server on port 80. When a user clicks a button, the browser requests http://<IP>/H or /L, which the ESP32 decodes from the request header and updates the GPIO state.

Expected Physical Output

Serial monitor prints local IP address. Opening that IP in a browser renders the NovaCircuit control panel allowing wireless lamp switching.
Troubleshooting Tips
  • •Cannot connect: Ensure your Wi-Fi is 2.4GHz. ESP32 does not support 5GHz Wi-Fi networks.
  • •IP not loading: Ensure your mobile or PC is on the exact same Wi-Fi network.
Electrical Safety Directives
  • •Safe low-voltage wireless DC control.
Companion Resources

Related Book Chapter & Hardware Kit

Covered in depth within Chapter 10: Internet-Connected Devices. Build this easily with the IoT Innovator Kit.