Hi folks!

Today I’m going to share my project on testing the web server capabilities on ESP32 Node-MCU. This web server runs using wifi over a local network. You can make this local network simply by using your phone’s mobile hotspot. In this project, I will try to make 2 LED turns on and off using a web browser on my laptop. First, let’s make a circuit like this.

The Circuit

Switching LED Circuit

The circuit is very simple. It basically just 2 LEDs connected with their respective GPIOs and the ground pin. Please take note that you can use any GPIOs you like. In this project I used GPIO 26 and 27.

The Code

// Load Wi-Fi library
#include <WiFi.h>

// Replace with your network credentials
const char* ssid = ""; //Replace with WiFi SSID
const char* password = ""; //Replace with WiFi Password

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

// Auxiliar variables to store the current output state
String output26State = "off";
String output27State = "off";

// Assign output variables to GPIO pins
const int output26 = 26; //Replace with the GPIO that you use
const int output27 = 27; //Replace with the GPIO that you use

void setup() {
// 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 ");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
// Print local IP address and start web server
Serial.println("WiFi connected.");
Serial.println("IP address: ");

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

if (client) { // If a new client connects,
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()) { // loop while the client's connected
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("Connection: close");

// turns the GPIOs on and off
if (header.indexOf("GET /26/on") >= 0) {
Serial.println("GPIO 26 on");
output26State = "on";
digitalWrite(output26, HIGH);
} else if (header.indexOf("GET /26/off") >= 0) {
Serial.println("GPIO 26 off");
output26State = "off";
digitalWrite(output26, LOW);
} else if (header.indexOf("GET /27/on") >= 0) {
Serial.println("GPIO 27 on");
output27State = "on";
digitalWrite(output27, HIGH);
} else if (header.indexOf("GET /27/off") >= 0) {
Serial.println("GPIO 27 off");
output27State = "off";
digitalWrite(output27, LOW);

// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555;}</style></head>");

// Web Page Heading
client.println("<body><h1>ESP32 Web Server</h1>");

// Display current state, and ON/OFF buttons for GPIO 26
client.println("<p>GPIO 26 - State " + output26State + "</p>");
// If the output26State is off, it displays the ON button
if (output26State=="off") {
client.println("<p><a href=\"/26/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");

// Display current state, and ON/OFF buttons for GPIO 27
client.println("<p>GPIO 27 - State " + output27State + "</p>");
// If the output27State is off, it displays the ON button
if (output27State=="off") {
client.println("<p><a href=\"/27/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/27/off\"><button class=\"button button2\">OFF</button></a></p>");

// The HTTP response ends with another blank line
// Break out of the while loop
} 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
Serial.println("Client disconnected.");

Before uploading the code to the board, change SSID and password to your network credentials. You could also change the GPIO that you want to use in output26 and output27 variables.

In a nutshell, the setup part in the program will try to connect to your WiFi with the credentials that you provided. You can check the board connectivity using a serial monitor at 115200. If your board is not connected, try to reset the board using the reset button on the board. If it has connected, the serial monitor will display the board IP address. Use this IP address to connect to the board via a web browser.

ESP32 Web Page

The loop part is a bit complicated. This part will listen for a button click in your browser. If there is a button click, the page will reload and the LED status will change to on or off. There is also a part to write the HTTP header in the program but I will not explain it here because it’s very complicated.

Here are some of my documentation on this project.

The Board Initial State
Red LED Turned On
Testing ESP32 Web Server with LEDs


Building a web server is not an easy task. It’s best to build it in an IDE like VS Code. Integrating it with an ESP32 is an even harder task. In this project, I just testing out some code I found on randomnerdtutorials.com and found it really basic. To further make ESP32 a practical web server, we must know how HTTP requests and responses are handled using Arduino IDE as a way to write programs to ESP32. So to make it actually useable in the real world, we must learn how to build an actual web server, and how to write that server to the board via Arduino IDE.

… and then there is the coronavirus

So I worked on this project quite differently than my previous ones. Indonesia has been experiencing a Covid-19 outbreak, popularly known as coronavirus. ITB has decided to ban all activities on the campus. As a result, all classes must be carried out via an online platform, which took a toll on my monthly internet quota (sad sound effect). This project supposed to be a 3-man team project. But due to the coronavirus, the only thing I could do is voice conferencing with my team and do the project alone while chatting with them. I hope that this outbreak could end quickly so that we all can get back to our daily lives. That’s it for this project, stay tuned and thank you for reading!

Information System and Technology Student at Bandung Institute of Technology, Indonesia