ESP32 - Control LED via Web

In this tutorial, we are going to learn how to control an LED through a web interface using a browser on a PC or smartphone, utilizing the ESP32. In detail, , the ESP32 will be programmed to work as a web server. Let's assume that the IP address of the ESP32 is 192.168.0.2. Here are the details of how it works:

ESP32 LED web browser

We will learn through two example codes:

The tutorial offers the fundamentals that you can readily and innovatively customize to achieve the following:

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to LED and ESP32

If you do not know about LED and ESP32 (pinout, how it works, how to program ...), learn about them in the following tutorials:

Wiring Diagram

ESP32 LED Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code - HTML content is embedded into ESP32 code

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-controls-led-via-web */ #include <WiFi.h> #include <ESPAsyncWebServer.h> #define LED_PIN 18 // ESP32 pin GPIO18 connected to LED const char *ssid = "YOUR_WIFI_SSID"; // CHANGE IT const char *password = "YOUR_WIFI_PASSWORD"; // CHANGE IT AsyncWebServer server(80); int LED_state = LOW; String getHTML() { String html = "<!DOCTYPE HTML>"; html += "<html>"; html += "<head>"; html += "<link rel='icon' href='data:,'>"; html += "</head>"; html += "<p>LED state: <span style='color: red;'>"; if (LED_state == LOW) html += "OFF"; else html += "ON"; html += "</span></p>"; html += "<a href='/led1/on'>Turn ON</a>"; html += "<br><br>"; html += "<a href='/led1/off'>Turn OFF</a>"; html += "</html>"; return html; } void setup() { Serial.begin(9600); pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LED_state); // Connect to Wi-Fi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); // Print the ESP32's IP address Serial.print("ESP32 Web Server's IP address: "); Serial.println(WiFi.localIP()); // home page server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { Serial.println("ESP32 Web Server: New request received:"); Serial.println("GET /"); request->send(200, "text/html", getHTML()); }); // Route to control the LED server.on("/led1/on", HTTP_GET, [](AsyncWebServerRequest *request) { Serial.println("ESP32 Web Server: New request received:"); Serial.println("GET /led1/on"); LED_state = HIGH; digitalWrite(LED_PIN, LED_state); request->send(200, "text/html", getHTML()); }); server.on("/led1/off", HTTP_GET, [](AsyncWebServerRequest *request) { Serial.println("ESP32 Web Server: New request received:"); Serial.println("GET /led1/off"); LED_state = LOW; digitalWrite(LED_PIN, LED_state); request->send(200, "text/html", getHTML()); }); // Start the server server.begin(); } void loop() { // Your code here }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Open the Library Manager by clicking on the Library Manager icon on the left navigation bar of Arduino IDE.
  • Search “ESPAsyncWebServer”, then find the ESPAsyncWebServer created by lacamera.
  • Click Install button to install ESPAsyncWebServer library.
ESP32 ESPAsyncWebServer library
  • You will be asked to install the dependency. Click Install All button.
ESP32 ESPAsyncWebServer dependencies library
  • Copy the above code and open with Arduino IDE
  • Change the wifi information (SSID and password) in the code to yours
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Open the Serial Monitor
  • See the result on Serial Monitor.
COM6
Send
Connecting to WiFi... Connected to WiFi ESP32 Web Server's IP address: 192.168.0.2
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • You will see an IP address, for example: 192.168.0.2. This is the IP address of the ESP32 Web Server
  • Open a web browser and enter one of the three formats below into the address bar:
192.168.0.2
192.168.0.2/led1/on
192.168.0.2/led1/off
  • Kindly be aware that the IP address might vary. Please verify the current value on the Serial Monitor.
  • You will also see the below output on Serial Monitor
COM6
Send
Connecting to WiFi... Connected to WiFi ESP32 Web Server's IP address: 192.168.0.2 ESP32 Web Server: New request recieved: GET / ESP32 Web Server: New request recieved: GET /led1/on ESP32 Web Server: New request recieved: GET /led1/off
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Check LED state
  • You will see the web page of ESP32 board on the web browser as below
ESP32 LED web browser
  • You are now able to control the LED on/off via the web interface

ESP32 Code - HTML content is separated from ESP32 code

As a graphic web page contains a large amount of HTML content, embedding it into the ESP32 code as before becomes inconvenient. To address this, we need to separate the ESP32 code and the HTML code into different files:

  • The ESP32 code will be placed in a .ino file.
  • The HTML code (including HTML, CSS, and Javascript) will be placed in a .h file.

Quick Instructions

  • Open Arduino IDE and create new sketch, Give it a name, for example, esp32io.com.ino
  • Copy the below code and open with Arduino IDE
/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-controls-led-via-web */ #include <WiFi.h> #include <ESPAsyncWebServer.h> #include "index.h" // Include the index.h file #define LED_PIN 18 // ESP32 pin GPIO18 connected to LED const char *ssid = "YOUR_WIFI_SSID"; // CHANGE IT const char *password = "YOUR_WIFI_PASSWORD"; // CHANGE IT AsyncWebServer server(80); int LED_state = LOW; String getHTML() { String html = webpage; // Use the HTML content from the index.h file html.replace("%LED_STATE%", LED_state ? "ON" : "OFF"); // update the led state return html; } void setup() { Serial.begin(9600); pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LED_state); // Connect to Wi-Fi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); // Print the ESP32's IP address Serial.print("ESP32 Web Server's IP address: "); Serial.println(WiFi.localIP()); // home page server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { Serial.println("ESP32 Web Server: New request received:"); Serial.println("GET /"); request->send(200, "text/html", getHTML()); }); // Route to control the LED server.on("/led1/on", HTTP_GET, [](AsyncWebServerRequest *request) { Serial.println("ESP32 Web Server: New request received:"); Serial.println("GET /led1/on"); LED_state = HIGH; digitalWrite(LED_PIN, LED_state); request->send(200, "text/html", getHTML()); }); server.on("/led1/off", HTTP_GET, [](AsyncWebServerRequest *request) { Serial.println("ESP32 Web Server: New request received:"); Serial.println("GET /led1/off"); LED_state = LOW; digitalWrite(LED_PIN, LED_state); request->send(200, "text/html", getHTML()); }); // Start the server server.begin(); } void loop() { // Your code here }
  • Change the WiFi information (SSID and password) in the code to yours
  • Create the index.h file On Arduino IDE by:
    • Either click on the button just below the serial monitor icon and choose New Tab, or use Ctrl+Shift+N keys.
    Arduino IDE 2 adds file
    • Give the file's name index.h and click OK button
    Arduino IDE 2 adds file index.h
    • Copy the below code and paste it to the index.h.
    /* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-controls-led-via-web */ #ifndef WEBPAGE_H #define WEBPAGE_H const char* webpage = R"=====( <!DOCTYPE HTML> <html> <head> <link rel='icon' href='data:,'> </head> <p>LED state: <span style='color: red;'>%LED_STATE%</span></p> <a href='/led1/on'>Turn ON</a> <br><br> <a href='/led1/off'>Turn OFF</a> </html> )====="; #endif
    • Now you have the code in two files: esp32io.com.ino and index.h
    • Click Upload button on Arduino IDE to upload code to ESP32
    • Access the web page of ESP32 board via web browser on your PC or smartphone as before. You will see it similar to the previous code as below:
    ESP32 LED web browser

    ※ NOTE THAT:

    • If you modify the HTML content in the index.h and does not touch anything in esp32io.com.ino file, when you compile and upload code to ESP32, Arduino IDE will not update the HTML content.
    • To make Arduino IDE update the HTML content in this case, make a change in the esp32io.com.ino file (e.g. adding empty line, add a comment....)

    You can readily and innovatively customize the above ocde to achieve the following:

    • Controlling multiple LEDs through the web
    • Redesigning the web user interface (UI)

※ OUR MESSAGES