DEV Community

Cover image for ๐ŸŒ Control Your LED from a Web Server Using ESP32 Introduction
Mohamed Ahmed
Mohamed Ahmed

Posted on

๐ŸŒ Control Your LED from a Web Server Using ESP32 Introduction

Introduction

Welcome back, makers! ๐Ÿ‘‹
So far, weโ€™ve learned how to blink an LED, connect an external one, and control it using a button. Now, itโ€™s time to make things even more exciting โ€” weโ€™ll control the LED using a simple web interface hosted directly on your ESP32!

By the end of this tutorial, youโ€™ll be able to open a webpage on your phone or computer and turn the LED ON or OFF wirelessly.

๐Ÿงฐ What Youโ€™ll Need

  1. - ESP32 development board (e.g., ESP32 DevKit C)
  2. - 1 ร— LED (any color)
  3. - 1 ร— 220 ฮฉ resistor
  4. - Breadboard and jumper wires
  5. - Arduino IDE installed and configured
  6. - A Wi-Fi network (SSID and password)

โšก Step 1: Breadboard/Circuit Setup

Component ESP32 Pin Description
LED (anode โ€“ long leg) GPIO 4 Digital output pin
LED (cathode โ€“ short leg) GND Ground
Resistor (220 ฮฉ) In series with LED Limits current

๐Ÿ’ก Use the same wiring as in the previous post โ€” weโ€™ll just control it differently this time.

๐Ÿ’ป Step 2: Write the Code

Open your Arduino IDE and paste the following code:

#include <WiFi.h>

// Replace with your network credentials
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

WiFiServer server(80);  // Port 80 for HTTP
int ledPin = 4;         // GPIO 4 for LED

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

  Serial.println("Connecting to WiFi...");
  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());  // Your web server address

  server.begin();
}

void loop() {
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {
    Serial.println("New Client Connected.");
    String request = client.readStringUntil('\r');
    Serial.println(request);
    client.flush();

    // Control LED
    if (request.indexOf("/LED=ON") != -1) {
      digitalWrite(ledPin, HIGH);
    } else if (request.indexOf("/LED=OFF") != -1) {
      digitalWrite(ledPin, LOW);
    }

    // Build Web Page
    String html = "<!DOCTYPE html><html>";
    html += "<head><meta name='viewport' content='width=device-width, initial-scale=1'>";
    html += "<title>ESP32 LED Control</title></head>";
    html += "<body style='text-align:center; font-family:sans-serif;'>";
    html += "<h2>ESP32 Web LED Controller ๐Ÿ’ก</h2>";
    html += "<p><a href='/LED=ON'><button style='padding:10px 20px; background:green; color:white; border:none;'>TURN ON</button></a></p>";
    html += "<p><a href='/LED=OFF'><button style='padding:10px 20px; background:red; color:white; border:none;'>TURN OFF</button></a></p>";
    html += "</body></html>";

    client.println("HTTP/1.1 200 OK");
    client.println("Content-type:text/html");
    client.println();
    client.println(html);
    client.stop();
    Serial.println("Client disconnected.");
  }
}

Enter fullscreen mode Exit fullscreen mode

๐Ÿง  How It Works

  • The ESP32 connects to your Wi-Fi network using your SSID and password.
  • It starts a simple web server that listens for incoming connections on port 80.
  • When you open the ESP32โ€™s IP address in your browser, it serves a web page with two buttons.
  • Clicking TURN ON or TURN OFF sends a request (/LED=ON or /LED=OFF) that toggles the LED.

๐Ÿ–ฅ๏ธ Step 3: Upload and Test

  1. - Connect your ESP32 to your computer.
  2. - Select your board and port in the Arduino IDE.
  3. - Replace YOUR_WIFI_NAME and YOUR_WIFI_PASSWORD with your Wi-Fi credentials.
  4. - Click Upload.
  5. - Open Serial Monitor (115200 baud).
  6. - After connecting, note the IP address displayed (e.g., 192.168.0.105). Your IDE should look like this
  7. - Type that IP address in your browser โ€” youโ€™ll see your control page!

Your web page should look like this

Click the buttons โ€” your LED should respond instantly! โšก

Your ESP321 should look like this
โšก Troubleshooting
1.No IP address shown?

  • Double-check your Wi-Fi name and password.

2.Page not loading?

  • Ensure your computer/phone is on the same Wi-Fi network as your ESP32.

3.LED not turning on?

  • Verify your wiring and GPIO pin number in the code.

๐ŸŽฏ Whatโ€™s Next
Youโ€™ve now built your first IoT web interface! ๐ŸŒ
โœ… Connected ESP32 to Wi-Fi
โœ… Hosted a mini web server
โœ… Controlled hardware from a browser

In the next post, weโ€™ll go further โ€” read sensor data (like temperature or light) and display it on your web dashboard in real time. ๐Ÿ“Š

๐Ÿ’ฌ Your Turn
Did you manage to control your LED from your browser? ๐Ÿ’ก
Share your setup, screenshots, or questions in the comments โ€” letโ€™s keep learning and building together! ๐Ÿ™Œ

Top comments (0)