Building a Simple ESP32 LED Web Server

Building a Simple ESP32 LED Web Server

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!

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("

"); } else { client.println("

"); } // 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("

"); } else { client.println("

"); } 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!)

Components and Supplies

You may also like to read

Frequently Asked Questions

Back to blog

Leave a comment

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

Components and Supplies

You may also like to read