What is an ESP32 Web Server?

In this comprehensive tutorial, you will learn how to transform your ESP32 into a fully functional web server. Using the Arduino IDE, we will program the board to serve a web page that allows you to control its outputs remotely from any device on your local network. Imagine turning an LED on or off with a simple click in your web browser—this project will show you exactly how to do that, providing the foundational knowledge for more advanced Internet of Things (IoT) applications.

ESP32-Web-Servers-Beginners-guide

Project Overview

Our goal is to create a simple web server with the ESP32 that controls two onboard LED outputs. The web page we build will display a title, two buttons, and the current GPIO state (ON or OFF). Clicking a button will send a request to the ESP32 to toggle the corresponding LED, and the page will update to reflect the new state. This "stateful" behavior, where the page updates without needing manual refresh, is a key feature we'll implement.

ESP32-Access-Point-Mode

Parts Required

To follow this tutorial, you will need:

Note: If your ESP32 board doesn't have built-in LEDs on GPIO 26 and 27, you can connect external LEDs with 220-ohm resistors to these pins and GND.

Step-by-Step Guide

1 Install ESP32 Board in Arduino IDE

To program the ESP32 with Arduino IDE, you must first install the ESP32 board add-on.

  1. Open Arduino IDE and go to File > Preferences.
  2. In the "Additional Boards Manager URLs" field, enter: https://espressif.github.io/arduino-esp32/package_esp32_index.json
  3. Open the Boards Manager via Tools > Board > Boards Manager, search for "esp32", and install the "ESP32 by Espressif Systems" package.
  4. Select your ESP32 board (e.g., DOIT ESP32 DEVKIT V1) from Tools > Board.

2 Complete Code for ESP32 Web Server

Copy the following code to your Arduino IDE. Before uploading, you must insert your network credentials.

// Load necessary libraries #include <WiFi.h> #include <WebServer.h> // Replace with your network credentials const char* ssid = "YOUR_SSID"; const char* password = "YOUR_PASSWORD"; // Instantiate a web server on port 80 WebServer server(80); // Variable to store the HTTP request String header; // Auxiliary variables for GPIO states String output26State = "off"; String output27State = "off"; // Assign output variables to GPIO pins const int output26 = 26; const int output27 = 27; // 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(output26, OUTPUT); pinMode(output27, OUTPUT); // Set outputs to LOW digitalWrite(output26, LOW); digitalWrite(output27, 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(){ server.handleClient(); }
Important: You must replace YOUR_SSID and YOUR_PASSWORD with the actual name and password of your 2.4GHz Wi-Fi network.

3 Setting up the Web Page (HTML)

The web page is sent to the client as an HTML document. We add this code inside the setup() function, right before the server.begin() line.

ESP32-ESP8266-Control-GPIOs-from-anywhere-cloud

This HTML code creates a responsive web interface with two buttons that toggle the LED states. The interface updates dynamically without requiring page refresh.

4 Uploading the Code and Testing

  1. Insert your SSID and password in the code.
  2. Select the correct board and COM port in the Arduino IDE.
  3. Click the upload button.
  4. Open the Serial Monitor at a baud rate of 115200. You should see the ESP32's IP address once it connects to Wi-Fi.
Arduino IDE showing successful upload and IP address in serial monitor

Copy the IP address and paste it into any web browser on a device connected to the same network. You should see the web page with the two buttons. Click the buttons to control the LEDs. The page updates instantly to show the new state.

Troubleshooting Common Issues

Can't upload code?

Ensure you've selected the correct board and that the correct COM port is chosen. You might need to press the BOOT button on the ESP32 during upload.

Not connecting to Wi-Fi?

Double-check your SSID and password. Ensure your router is broadcasting a 2.4GHz network (the ESP32 doesn't support 5GHz).

Can't access the web page?

Verify your client device is on the same network. Check if your firewall is blocking port 80.

esp32-waiting-for-packet-header-featured

Going Further

Congratulations! You've built a basic web server. You can expand this project by:

Pro Tip: This ESP32 web server project is your gateway into the world of IoT. By mastering these fundamentals, you can now create a wide range of networked devices controllable from anywhere in your home. The possibilities are nearly endless!
4 Node Home-Automation System Using Smallest ESP32