✨ Use RCAPP and get 5% off 👇
Skip to content
Free Delivery on Orders Above Rs 999/- Pan-India
Cash on Delivery Available for Orders above Rs.500/- and Upto Rs 3000/-
SAVE more when you BUY more. Upto 30% Off on BULK PURCHASE
GST Invoices for Your Business
Dedicated Technical Support Team
Safely Delivering Genuine Products PAN INDIA

Building a Simple ESP32 LED Web Server Complete Guide

Building a Simple ESP32 LED Web Server Complete Guide
S
Written By Sumant Ghosh
📅 Updated on 27 Jan 2025
Summarize with AI
✅ Prompt copied

Summary

In the world of IoT (Internet of Things), the ESP32 microcontroller has become a popular choice for developers and hobbyists alike due to its powerful features and built-in Wi-Fi and Bluetooth capabilities. One of the most exciting applications of the ESP32 is creating web servers that allow you to control devices remotely over the internet. In this tutorial, we will guide you through the process of building a simple LED web server using the ESP32. By the end of this project, you'll be able to control an LED connected to the ESP32 from any device with a web browser, whether it's your smartphone, tablet, or computer.

Whether you're a beginner or an experienced developer, this project is a great way to dive into the world of IoT and explore the potential of the ESP32. Let's get started!

Episode EE01:

Building a Simple ESP32 LED Web Server

 

Do you know What is Esp32 Development Board? read now!

Components and Supplies

ESP32 NodeMCU Dev Board (CP2102, 30-Pin) – WiFi & BLE support. -Nodemcu ESP Board -RobocrazeESP32 NodeMCU Dev Board (CP2102, 30-Pin) – WiFi & BLE support. -Nodemcu ESP Board -Robocraze

    ESP32 Node MCU Development Board with Wifi and Bluetooth (CP2102 Driver, 30 PIN)

    ESP32 (CP2102 Driver, 30 Pin) WiFi + Bluetooth NodeMCU-32 Development Board) Looking to supercharge your IoT projects? The ESP32 NodeMCU Development Board with Wi-Fi and Bluetooth (CP2102) is your go-to solution. This NodeMCU ESP32 development board is widely considered the best ESP32 board for...
    Rs 383/-
    Rs 383/-
    Rs 597/-
    Save Rs 214/-
    1M Ohm Resistor (10PC) – Common resistors for circuits & sensors. -Basic Components -Robocraze1M Ohm Resistor (10PC) – Common resistors for circuits & sensors. -Basic Components -Robocraze

      1M Ohm Resistor - (Pack of 10)

      1M Ohm Resistor - (Pack of 10) The 1M ohm Carbon Film Resistors are typical axial-lead resistors, which have much better temperature stability and provide lower noise, and are generally better for high frequency or radiofrequency applications. A resistor is a passive two-terminal electrical component that implements...
      Rs 13/-
      Rs 13/-
      Rs 19/-
      Save Rs 6/-
      3mm Red LED (Pack of 10) – Bright & long-lasting LED for DIY circuits - Electronic Components - Robocraze3mm Red LED (Pack of 10) – Bright & long-lasting LED for DIY circuits - Electronic Components - Robocraze

      3mm Red Led (Pack of 10)

      3mm Red LED (Pack of 10) A Red light-emitting diode (LED) is a semiconductor light source. LED's are used as indicator lamps in many devices and are increasingly used for other lighting. It looks like a white led and illuminate's blue light. When a light-emitting...
      Rs 15/-
      Rs 15/-
      Rs 23/-
      Save Rs 8/-
      400 Tie Points Solderless Breadboard – Medium-sized breadboard for Arduino & projects. Electronic Components - Robocraze400 Tie Points Solderless Breadboard – Medium-sized breadboard for Arduino & projects. Electronic Components - Robocraze

        400 Tie Points Solderless Breadboard

        400 Tie Points Solderless Breadboard The 400 Tie Point solderless breadboard is perfect for building and testing electronics, especially with Arduino projects. It’s made of strong plastic with metal clips that hold wires securely. With 400 points, you can quickly set up circuits without...
        Rs 35/-
        Rs 35/-
        Rs 59/-
        Save Rs 24/-
        GL12 840 Points Solderless Breadboard for Large Prototyping - Electronic Components -RobocrazeGL12 840 Points Solderless Breadboard for Large Prototyping - Electronic Components -Robocraze

          GL12 840 Points Solderless Breadboard

          GL12 MB102 840 Points Solderless Breadboard Introducing the GL12 840 Points Breadboard at Robocraze! This solderless breadboard is an essential tool for any electronics engineer or hobbyist. It allows you to make temporary prototypes for your electronics projects, making it perfect for experimenting with...
          Rs 70/-
          Rs 70/-
          Rs 90/-
          Save Rs 20/-

          Hardware Required:

          Circuit Diagram ESP32 LED Web Server:

          Circuit Diagram ESP32 LED Web Server
          Component ESP32 Pin Description
          LED (Anode) GPIO Pin (e.g., GPIO 2) Connect the positive (longer) leg of the LED to a GPIO pin of the ESP32 (e.g., GPIO 2).
          LED (Cathode) GND Connect the negative (shorter) leg of the LED to the Ground (GND) pin of the ESP32.
          Resistor (e.g., 220Ω) Between LED Anode and GPIO Pin Add a resistor in series with the LED to limit the current and prevent damage to the LED and ESP32.


          Check out our latest blog Build Your Security Camera Using ESP32-CAM - A Step-by-Step Guide 

          Programming

          Before uploading the code. In this area, make sure to update the network credentials to your SSID and password.

          
          const char* ssid = " REPLACE_WITH_YOUR_SSID";
          const char* password = " REPLACE_WITH_YOUR_PASSWORD";
          

          Complete Code

          // Load Wi-Fi library #include // Replace with your network credentials const char* ssid = " REPLACE_WITH_YOUR_SSID"; const char* password = " REPLACE_WITH_YOUR_PASSWORD"; // Set web server port number to 80 WiFiServer server(80); // Variable to store the HTTP request String header; // Auxiliary variables to store the current output state String output12State = "off"; String output14State = "off"; // Assign output variables to GPIO pins const int output12 = 12; const int output14 = 14; // Current time unsigned long currentTime = millis(); // Previous time unsigned long previousTime = 0; // Define timeout time in milliseconds (example: 2000ms = 2s) const long timeoutTime = 2000; void setup() { Serial.begin(115200); // Initialize the output variables as outputs pinMode(output12, OUTPUT); pinMode(output14, OUTPUT); // Set outputs to LOW digitalWrite(output12, LOW); digitalWrite(output14, LOW); // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.begin(); } void loop(){ WiFiClient client = server.available(); // Listen for incoming clients if (client) { // If a new client connects, currentTime = millis(); previousTime = currentTime; Serial.println("New Client."); // print a message out in the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected currentTime = millis(); if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor header += c; if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); // turns the GPIOs on and off if (header.indexOf("GET /12/on") >= 0) { Serial.println("GPIO 12 on"); output12State = "on"; digitalWrite(output12, HIGH); } else if (header.indexOf("GET /12/off") >= 0) { Serial.println("GPIO 12 off"); output12State = "off"; digitalWrite(output12, LOW); } else if (header.indexOf("GET /14/on") >= 0) { Serial.println("GPIO 14 on"); output14State = "on"; digitalWrite(output14, HIGH); } else if (header.indexOf("GET /14/off") >= 0) { Serial.println("GPIO 14 off"); output14State = "off"; digitalWrite(output14, LOW); } // Display the HTML web page client.println(""); client.println(" "); client.println(" "); // CSS to style the on/off buttons // Feel free to change the background-color and font-size attributes to fit your preferences client.println(" "); // Web Page Heading client.println("

          ESP32 Web Server

          "); // Display current state, and ON/OFF buttons for GPIO 12 client.println("

          GPIO 12 - State " + output12State + "

          "); // If the output12State is off, it displays the ON button if (output12State=="off") { client.println("

          ON

          "); } else { client.println("

          OFF

          "); } // Display current state, and ON/OFF buttons for GPIO 14 client.println("

          GPIO 14 - State " + output14State + "

          "); // If the output14State is off, it displays the ON button if (output14State=="off") { client.println("

          ON

          "); } else { client.println("

          OFF

          "); } client.println(""); // The HTTP response ends with another blank line client.println(); // Break out of the while loop break; } else { // if you got a newline, then clear currentLine currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } } } // Clear the header variable header = ""; // Close the connection client.stop(); Serial.println("Client disconnected."); Serial.println(""); } }

          Upload the code to the esp32 board

          Finding the IP address of the ESP32 board

          1. Open the Serial Monitor in the Arduino IDE

          Steps to Finding the IP address of the ESP32 board

          2. Select a baud rate of 115200

          Steps to Finding the IP address of the ESP32 board

          3.Wait for the ESP32 board to connect to the WiFi network

          4.Once the ESP32 board is connected to the WiFi network, it will obtain an IP address from the network. The Serial Monitor will display the IP address of the ESP32 board.

          Steps to Finding the IP address of the ESP32 board

          Result:

          ESP32 LED Web Server Project Result


          Hey want to hunt treasures with ESP32 then check this blog - TREASURE HUNT USING ESP32 (BLE SCANNER)

          The Working Concept of the Project

          The core idea of this project is to control an LED connected to the ESP32 microcontroller through a web interface. Here's how the project functions:
          1. ESP32 as a Web Server: The ESP32 is programmed to function as a web server, meaning it can host a simple web page accessible from any device connected to the same network. When a user opens this web page in a browser, they can control the LED by sending commands to the ESP32.
          2. Wi-Fi Connection: The ESP32 connects to your Wi-Fi network, allowing it to communicate with other devices, such as your smartphone or computer. Once connected, the ESP32 is assigned an IP address, which you will use to access the web server.
          3. User Interface: The ESP32 serves a simple HTML page that contains buttons or switches for controlling the LED. For example, the page might have buttons labeled "Turn ON" and "Turn OFF." These buttons send HTTP requests to the ESP32 when clicked.
          4. Processing Requests: When you press a button on the web page, an HTTP request is sent to the ESP32. The ESP32 interprets this request and determines whether it needs to turn the LED on or off. It then sends the appropriate signal to the GPIO pin controlling the LED.
          5. LED Control: The ESP32 controls the LED by toggling the voltage on a specific GPIO pin. If the user presses the "Turn ON" button, the ESP32 sets the pin to HIGH, and the LED lights up. If the "Turn OFF" button is pressed, the pin is set to LOW, and the LED turns off.
          6. Real-Time Feedback: The changes in the LED state occur in real-time, allowing the user to see the result of their actions immediately. The web interface updates accordingly to reflect the current status of the LED.

           

           



          Also read LED Interfacing with Arduino.

          Conclusion:

          This project allows you to remotely control an LED using an ESP32 web server. By sending commands through a web browser, you can turn the LED on or off, showcasing the power of IoT and the flexibility of the ESP32 microcontroller in creating interactive and connected projects.




           

           

          Please do check out other blog posts about Popular electronics

           

          Check out other related blog posts about Drones: Drone transmitter and receiver , Drone motors and Getting started with a Quadcopter

           

          Make sure you check out our wide range of products and collections (we offer some exciting deals!)


          Excerpt

          The ESP32 is programmed to function as a web server, meaning it can host a simple web page accessible from any device connected to the same network .....
          Prev Post
          Next Post

          Leave a comment

          Please note, comments need to be approved before they are published.

          Thanks for subscribing!

          This email has been registered!

          Shop the look

          Choose Options

          Edit Option
          Back In Stock Notification
          Compare
          Product SKU Description Collection Availability Product Type Other Details

          Choose Options

          this is just a warning
          Login
          Shopping Cart
          0 items
          FREE SHIPPING!
          ₹100 OFF
          ₹200 OFF
          ₹999
          ₹2500
          ₹4900
          WhatsApp Chat Chat