A Complete Beginners Tutorial On How To Create Esp32
A Complete Beginners Tutorial On How To Create Esp32
In this tutorial, we will see how to build a simple ESP32 Web Server. This ESP32
Standalone Web Server can be accessed by mobile phone, computers, laptops and tablets
which are connected to the same WiFi network as ESP32. As a demonstration of the web
server, I created a simple web page which can be accesses by clients and control couple of
LEDs connected to ESP32.
Speaking of Web Clients, the Web Browsers in your laptops and mobile phones are one of
the simplest types of web clients. The communication between a Web Server and a Web
Client is often referred to as Client – Server Communication Model.
1/16
Hyper Text Transfer Protocol or simply HTTP is the protocol responsible for
communication between Client and Server. In this type of communication, the Web Client
makes a request for information from the server using HTTP. The Web Server, which is
always waiting (listening) for a request, responds to the client’s request with appropriate
web page.
If the requested page is not found, then the server responds with HTTP 404 Error.
2/16
When a client, like a web browser in a mobile phone, sends a request for that web page
over HTTP, the web server in ESP32 must respond with the web page. Additionally, when
the client performs any operations, like clicking on a button, the server should respond
with appropriate actions (like turning ON / OFF an LED).
In station mode, the ESP32 Module connects to an existing WiFi Network, which is setup
by a Wireless Router, just like our Mobile Phones and Laptops.
The ESP32 Wi-Fi Module connects to a Wi-Fi Network of Router using the router’s SSID
and Password and the router assigns the local IP Address for ESP32.
3/16
Coming to Access Point Mode, the ESP32 Module creates its own WiFi Network like a
Wireless Router, so that other stations like Mobile Phones, Laptops and even other ESP32
modules (in STA Mode) can connect to that network.
Since ESP32 doesn’t have a Wired Ethernet connectivity to internet, this AP Mode is
called as Soft AP Mode. While configuring ESP32 in AP Mode, you have to set the SSID
and Password for the network, so that other devices can connect to that network using
those credentials.
4/16
Station + Soft AP is a combination of Station Mode and Soft AP Mode. In this, the ESP32
acts as both the Station as well as an Access Point.
Using this IP Address, clients can access the Web Page. Additionally, the clients do not
lose internet connectivity from the Router.
But if we create Web Server for ESP32 in AP Mode, then clients must connect to the
network provided by ESP32 using its own SSID and Password in order to access the Web
Pages. Since it is a soft AP Mode, clients do not have internet connectivity.
Creating ESP32 Web Server either in Station Mode or in Soft AP Mode is very similar
except the configuration part of the ESP32.
In this tutorial, I will show you how to create a Web Server on ESP32 configured in
Station Mode (STA).
To demonstrate this, I connected two 5mm LEDs to GPIO 16 and GPIO 17 of ESP32
through respective current limiting resistors (220Ω). GPIO 16 is labelled RX2 and GPIO
17 is labelled TX2 on ESP32 DevKit Development Board.
5/16
Code
Coming to the important and interesting stuff, the actual code for Web Server on ESP32.
It is just an HTML Code with some text, couple of buttons and some stylization.
The following block shows the complete code for ESP32’s Web Server. I commented the
code in order to explain the code.
#include <WiFi.h>
int gpio16Value;
int gpio17Value;
6/16
/* 80 is the Port Number for HTTP Web Server */
String request;
void setup()
pinMode(gpio16LEDPin, OUTPUT);
pinMode(gpio17LEDPin, OUTPUT);
digitalWrite(gpio16LEDPin, LOW);
digitalWrite(gpio17LEDPin, LOW);
Serial.print("\n");
Serial.println(ssid);
while(WiFi.status() != WL_CONNECTED)
Serial.print("*");
delay(100);
Serial.print("\n");
Serial.println(WiFi.SSID());
7/16
delay(100);
/* The next four lines of Code are used for assigning Static IP to ESP32 */
/* You have to check for free IP Addresses from your Router and */
/* assign it to ESP32 */
/* please un-comment the next four lines and make necessary changes */
//IPAddress ip(192,168,1,6);
//IPAddress gateway(192,168,1,1);
//IPAddress subnet(255,255,255,0);
delay(2000);
Serial.print("\n");
Serial.print("\n");
Serial.print("http://");
Serial.println(WiFi.localIP());
Serial.print("\n");
Serial.println("Use the above URL in your Browser to access ESP32 Web Server\n");
void loop()
if(!client)
8/16
{
return;
Serial.println("New Client!!!");
while (client.connected())
if (client.available())
char c = client.read();
request += c;
Serial.write(c);
/* character) and the line is blank, the http request has ended, */
* 192.168.1.6/GPIO16ON
* 192.168.1.6/GPIO16OFF
* 192.168.1.6/GPIO17ON
* 192.168.1.6/GPIO17OFF
*/
/* Based on the URL from the request, turn the LEDs ON or OFF */
if (request.indexOf("/GPIO16ON") != -1)
9/16
Serial.println("GPIO16 LED is ON");
digitalWrite(gpio16LEDPin, HIGH);
gpio16Value = HIGH;
if (request.indexOf("/GPIO16OFF") != -1)
digitalWrite(gpio16LEDPin, LOW);
gpio16Value = LOW;
if (request.indexOf("/GPIO17ON") != -1)
digitalWrite(gpio17LEDPin, HIGH);
gpio17Value = HIGH;
if (request.indexOf("/GPIO17OFF") != -1)
digitalWrite(gpio17LEDPin, LOW);
gpio17Value = LOW;
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println(); // IMPORTANT
10/16
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<style>");
client.println("</style>");
client.println("</head>");
client.println("<body>");
if(gpio16Value == LOW)
else
11/16
client.println("<p>GPIO16 LED Status: ON</p>");
if(gpio17Value == LOW)
else
client.println("</body>");
client.println("</html>");
break;
if(c == '\n')
currentLineIsBlank = true;
currentLineIsBlank = false;
12/16
}
//client.print("\n");
delay(1);
request = "";
//client.flush();
client.stop();
Serial.println("Client disconnected");
Serial.print("\n");
After making necessary modifications, make the necessary connections as per the circuit
diagram, connect the ESP32 Board to the computer, select the ESP32 Board and also the
right COM Port and upload the code.
If you are new to ESP32, then the Getting Started with ESP32 tutorial will help you in
configuring Arduino IDE.
Open the Serial Monitor and ESP32 Module will print some important information like
the progress of Wi-Fi Connection, IP Address and URL of Web Server (which is essentially
the IP Address of the ESP32).
13/16
So, in my case the IP Address of ESP32 is 192.168.1.6.
The following is a screenshot of Chrome Web Browser on a laptop accessing the Web
Server of ESP32.
As you can see from the image, the web page displays a main header text, followed by
status of the LED connected to GPIO 16. This is followed by a Button, which can be used
to turn ON or OFF the LED. The same stuff again for GPIO 17 (status followed by Button).
14/16
Now, if I click on the first button, the LED connected to GPIO 16 will turn ON, the status
is updated in the web page, the text and color of the Button is also changed.
If you take a look at the Serial Monitor, every time a client tries to connect (or send a
request), some key information is printed on the serial monitor. I will explain about this
information (this is actually a part of request from client) in the next section.
15/16
Next, I tried the same thing on a Mobile Phone. It works perfectly.
NOTE: All the clients i.e., mobiles, laptops, etc., must be connected to the same network
as the ESP32 Module.
Conclusion
A complete step-by-step tutorial on how to create a Web Server using ESP32 DevKit
Development Board. You learned some important basics about web servers, different
modes of operation of ESP32 WiFi, how to build ESP32 Web Server and how to access
this server from different clients.
Related Posts:
3 Responses
Leave a Reply
Your email address will not be published. Required fields are marked *
16/16