ESP32 Web Server
From: https://lastminuteengineers.com/creating-esp32-web-server-arduino-ide
Simple ESP32 Web Server In Arduino IDECreating Simple ESP32 Webserver in Arduino IDE using Access Point & Station mode The ESP32, the newly released successor to the ESP8266, has been a rising star in IoT or WiFi-related projects. It’s a low-cost WiFi module that can be programmed to run a standalone web server with a little extra work. How cool is that? This tutorial will walk you through the process of creating a simple ESP32 web server in the Arduino IDE. So, let’s get started. What exactly is a Web server and how does it work? A web server is a place where web pages are stored, processed, and served to web clients. A web client is just a web browser that we use on our computers and phones. A web client and a web server communicate using a special protocol known as Hypertext Transfer Protocol (HTTP).
HTTP Web Server Client Illustration In this protocol, a client starts a conversation by sending an HTTP request for a specific web page. The server then sends back the content of that web page or an error message if it can’t find it (like the famous 404 Error).
ESP32 Operating Modes One of the most useful features of the ESP32 is its ability to not only connect to an existing WiFi network and act as a Web Server, but also to create its own network, allowing other devices to connect directly to it and access web pages. This is possible because the ESP32 can operate in three modes: Station (STA) mode, Soft Access Point (AP) mode, and both simultaneously.
Station (STA) Mode In Station (STA) mode, the ESP32 connects to an existing WiFi network (the one created by your wireless router).ESP32 Web Server Station STA Mode Demonstration In STA mode, the ESP32 obtains an IP address from the wireless router to which it is connected. With this IP address, it can set up a web server and serve web pages to all connected devices on the existing WiFi network.
Soft Access Point (AP) Mode In Access Point (AP) mode, the ESP32 sets up its own WiFi network and acts as a hub, just like a WiFi router. However, unlike a WiFi router, it does not have an interface to a wired network. So, this mode of operation is called Soft Access Point (soft-AP). Also, no more than five stations can connect to it at the same time.ESP32 Web Server Soft Access Point AP Mode Demonstration In AP mode, the ESP32 creates a new WiFi network and assigns it an SSID (the network’s name) and an IP address. With this IP address, it can serve web pages to all connected devices.
Wiring LEDs to an ESP32 Now that we understand the fundamentals of how a web server works and the modes in which the ESP32 can create one, it’s time to connect some LEDs to the ESP32 that we want to control via WiFi. Begin by placing the ESP32 on your breadboard, making sure that each side of the board is on a different side of the breadboard. Next, connect two LEDs to digital GPIO 4 and 5 using a 220Ω current limiting resistor. When you are finished, you should have something that looks like the image below. Simple ESP32 Web Server Wiring Fritzing Connections with LEDWiring LEDs to ESP32
The Idea Behind Using an ESP32 Web Server to Control Things So you might be wondering, “How do I control things from a web server that only processes and serves web pages?”. It’s extremely simple. We’re going to control things by visiting a specific URL. When you enter a URL into a web browser, it sends an HTTP request (also known as a GET request) to a web server. It is the web server’s responsibility to handle this request. Assume you entered a URL like http://192.168.1.1/ledon into a browser. The browser then sends an HTTP request to the ESP32. When the ESP32 receives this request, it recognizes that the user wishes to turn on the LED. As a result, it turns on the LED and sends a dynamic webpage to a browser that displays the LED’s status as “on.” Quite simple, right?
Example 1 – Configuring the ESP32 Web Server in Access Point (AP) mode Let’s get to the interesting stuff now! This example, as the title suggests, shows how to configure the ESP32 Web Server in Access Point (AP) mode and serve web pages to any connected client. To begin, connect your ESP32 to your computer and run the sketch. Then we will look at it in more detail. /*H******************************************************* * ********************************************************/ #include <WiFi.h> #include <WebServer.h> // **************** DEFINES **************************** // **************** PROTOTYPES ************************* void handle_OnConnect(); void handle_led1on(); void handle_led1off(); void handle_led2on(); void handle_led2off(); void handle_NotFound(); String SendHTML( uint8_t led1stat,uint8_t led2stat ); // **************** VARIABLES ************************** // PUT YOUR SSID & PASSWORD const char* ssid = "ESP32"; // ENTER SSID HERE const char* password = "12345678"; // ENTER PASSWORD HERE IPAddress local_ip( 192, 168, 1, 1 ); // PUT IP ADDRESS DETAILS IPAddress gateway( 192, 168, 1, 1 ); IPAddress subnet( 255, 255, 255, 0); WebServer server( 80 ); uint8_t LED1pin = 4; bool LED1status = LOW; uint8_t LED2pin = 5; bool LED2status = LOW; /*F******************************************************* * ********************************************************/ void setup() { Serial.begin( 115200 ); pinMode( LED1pin, OUTPUT); pinMode( LED2pin, OUTPUT); WiFi.softAP( ssid, password); WiFi.softAPConfig( local_ip, gateway, subnet); delay( 100 ); server.on( "/", handle_OnConnect); server.on( "/led1on", handle_led1on); server.on( "/led1off", handle_led1off); server.on( "/led2on", handle_led2on); server.on( "/led2off", handle_led2off); server.onNotFound( handle_NotFound); server.begin(); Serial.println( "HTTP server started"); } /*F******************************************************* * ********************************************************/ void loop() { server.handleClient(); if( LED1status) { digitalWrite(LED1pin, HIGH); } else { digitalWrite(LED1pin, LOW); } if( LED2status ) { digitalWrite(LED2pin, HIGH); } else { digitalWrite(LED2pin, LOW); } } /*F******************************************************* * ********************************************************/ void handle_OnConnect() { LED1status = LOW; LED2status = LOW; Serial.println("GPIO4 Status: OFF | GPIO5 Status: OFF"); server.send(200, "text/html", SendHTML(LED1status,LED2status)); } /*F******************************************************* * ********************************************************/ void handle_led1on() { LED1status = HIGH; Serial.println("GPIO4 Status: ON"); server.send(200, "text/html", SendHTML(true,LED2status)); } /*F******************************************************* * ********************************************************/ void handle_led1off() { LED1status = LOW; Serial.println("GPIO4 Status: OFF"); server.send(200, "text/html", SendHTML(false,LED2status)); } /*F******************************************************* * ********************************************************/ void handle_led2on() { LED2status = HIGH; Serial.println("GPIO5 Status: ON"); server.send( 200, "text/html", SendHTML(LED1status,true)); } /*F******************************************************* * ********************************************************/ void handle_led2off() { LED2status = LOW; Serial.println( "GPIO5 Status: OFF"); server.send( 200, "text/html", SendHTML(LED1status,false)); } /*F******************************************************* * ********************************************************/ void handle_NotFound() { server.send( 404, "text/plain", "Not found"); } /*F******************************************************* * ********************************************************/ String SendHTML( uint8_t led1stat,uint8_t led2stat ) { String ptr = "<!DOCTYPE html> <html>\n"; ptr += "<head><meta name=\"viewport\" content=\"width=device-width , initial-scale=1.0, user-scalable=no\">\n"; ptr += "<title>LED Control</title>\n"; ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n"; ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n"; ptr +=".button { display: block;width: 80px;background-color: #3498db;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n"; ptr +=".button-on {background-color: #3498db;}\n"; ptr +=".button-on:active {background-color: #2980b9;}\n"; ptr +=".button-off {background-color: #34495e;}\n"; ptr +=".button-off:active {background-color: #2c3e50;}\n"; ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n"; ptr +="</style>\n"; ptr +="</head>\n"; ptr +="<body>\n"; ptr +="<h1>ESP32 Web Server</h1>\n"; ptr +="<h3>Using Access Point(AP) Mode</h3>\n"; if( led1stat ) { ptr +="<p>LED1 Status: ON</p><a class=\"button button-off\" href=\"/led1off\">OFF</a>\n"; } else { ptr +="<p>LED1 Status: OFF</p><a class=\"button button-on\" href=\"/led1on\">ON</a>\n"; } if( led2stat ) { ptr +="<p>LED2 Status: ON</p><a class=\"button button-off\" href=\"/led2off\">OFF</a>\n"; } else { ptr +="<p>LED2 Status: OFF</p><a class=\"button button-on\" href=\"/led2on\">ON</a>\n"; } ptr +="</body>\n"; ptr +="</html>\n"; return( ptr ); }
Accessing the Web Server in AP Mode After uploading the sketch, open the Serial Monitor at 115200 baud and press the RESET button on the ESP32. If everything is fine, it will show the “HTTP server started” message.ESP32 Web Server Access Point Mode Serial Monitor Output - Server Started Now, get a phone, laptop, or other device that can connect to a WiFi network, and look for a network called “ESP32”. Connect to the network using the password 12345678.
ESP32 Web Server Access Point Mode - Joining Server After connecting to your ESP32 AP network, open a browser and navigate to 192.168.1.1. The ESP32 should return a web page displaying the current status of the LEDs and buttons. At the same time, you can check the serial monitor to see the status of the ESP32’s GPIO pins.
ESP32 Web Server Access Point Mode - Web Page |
ESP32 Web Server Access Point Mode Serial Monitor Output - Webpage Accessed |
ESP32 Web Server Access Point Mode Web Page - LED Control |
ESP32 Web Server Access Point Mode Serial Monitor Output - LED Control |
Change SSID & Password before trying STA mode web server sketch
Once you’re done, go ahead and try out the sketch.
/*H*******************************************************
*
********************************************************/
#include <WiFi.h>
#include <WebServer.h>
// **************** DEFINES ****************************
/*Put your SSID & Password*/
const char* ssid = " YourNetworkName"; // Enter SSID here
const char* password = " YourPassword"; //Enter Password here
// **************** PROTOTYPES *************************
void handle_OnConnect();
void handle_led1on();
void handle_led1off();
void handle_led2on();
void handle_led2off();
void handle_NotFound();
String SendHTML( uint8_t led1stat,uint8_t led2stat);
// **************** VARIABLES **************************
WebServer server( 80 );
uint8_t LED1pin = 4;
bool LED1status = LOW;
uint8_t LED2pin = 5;
bool LED2status = LOW;
/*F*******************************************************
*
********************************************************/
void
setup()
{
Serial.begin( 115200 );
delay( 100 );
pinMode( LED1pin, OUTPUT);
pinMode( LED2pin, OUTPUT);
Serial.println( "Connecting to " );
Serial.println( ssid );
// connect to your local wi-fi network
WiFi.begin( ssid, password );
// check wi-fi is connected to wi-fi network
while( WiFi.status() != WL_CONNECTED)
{
delay( 1000 );
Serial.print( "." );
}
Serial.println( "" );
Serial.println( "WiFi connected..!");
Serial.print( "Got IP: ");
Serial.println( WiFi.localIP() );
server.on( "/", handle_OnConnect);
server.on( "/led1on", handle_led1on);
server.on( "/led1off", handle_led1off);
server.on( "/led2on", handle_led2on);
server.on( "/led2off", handle_led2off);
server.onNotFound( handle_NotFound);
server.begin();
Serial.println( "HTTP server started");
}
/*F*******************************************************
*
********************************************************/
void
loop()
{
server.handleClient();
if(LED1status)
{ digitalWrite( LED1pin, HIGH); }
else
{ digitalWrite( LED1pin, LOW); }
if(LED2status)
{ digitalWrite( LED2pin, HIGH); }
else
{ digitalWrite( LED2pin, LOW); }
}
/*F*******************************************************
*
********************************************************/
void
handle_OnConnect()
{
LED1status = LOW;
LED2status = LOW;
Serial.println( "GPIO4 Status: OFF | GPIO5 Status: OFF");
server.send( 200, "text/html", SendHTML (LED1status, LED2status) );
}
/*F*******************************************************
*
********************************************************/
void
handle_led1on()
{
LED1status = HIGH;
Serial.println( "GPIO4 Status: ON");
server.send( 200, "text/html", SendHTML( true, LED2status) );
}
/*F*******************************************************
*
********************************************************/
void
handle_led1off()
{
LED1status = LOW;
Serial.println( "GPIO4 Status: OFF");
server.send( 200, "text/html", SendHTML( false, LED2status));
}
/*F*******************************************************
*
********************************************************/
void
handle_led2on()
{
LED2status = HIGH;
Serial.println( "GPIO5 Status: ON" );
server.send( 200, "text/html", SendHTML( LED1status, true) );
}
/*F*******************************************************
*
********************************************************/
void
handle_led2off()
{
LED2status = LOW;
Serial.println( "GPIO5 Status: OFF");
server.send( 200, "text/html", SendHTML(LED1status,false));
}
/*F*******************************************************
*
********************************************************/
void
handle_NotFound()
{
server.send( 404, "text/plain", "Not found");
}
/*F*******************************************************
*
********************************************************/
String
SendHTML( uint8_t led1stat,uint8_t led2stat)
{
String ptr = "<DOCTYPE html> <html>n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width
, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>LED Control</title>\n";
ptr +="<style>html
{
font-family: Helvetica; display: inline-block;
margin: 0px
auto; text-align: center;
}\n";
ptr +="body{margin-top: 50px;
} h1 {color: #444444;margin: 50px auto 30px;
}
h3 {color: #444444;margin-bottom: 50px;}\n";
ptr +=".button
{
display: block;width: 80px;background-color:#3498db;border:
none;color: white;padding: 13px 30px;text-decoration:
none;font-size: 25px;margin: 0px auto 35px;cursor:
pointer;border-radius: 4px;
}\n";
ptr += ".button-on {background-color: #3498db;}\n";
ptr += ".button-on:active {background-color: #2980b9;}\n";
ptr += ".button-off {background-color: #34495e;}\n";
ptr += ".button-off:active {background-color: #2c3e50;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<h1>ESP32 Web Server</h1>\n";
ptr += "<h3>Using Station(STA) Mode</h3>\n";
if( led1stat )
{
ptr +="<p>LED1 Status: ON</p><a
class=\"button button-off\" href=\"/led1off\">OFF</a>\n";}
else
{
ptr +="<p>LED1 Status: OFF</p><a
class=\"button button-on\" href=\"/led1on\">ON</a>\n";}
if( led2stat )
{
ptr +="<p>LED2 Status: ON</p><a
class=\"button button-off\" href=\"/led2off\">OFF</a>\n";
}
else
{
ptr +="<p>LED2 Status: OFF</p><a
class=\"button button-on\" href=\"/led2on\">ON</a>\n";
}
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}
ESP32 Web Server Station Mode Serial Monitor Output - Server Started
Next, launch a browser and navigate to the IP address displayed on the
serial monitor. The ESP32 should display a web page with the current status
of the LEDs and two buttons for controlling them. At the same time, you can
check the serial monitor to see the status of the ESP32’s GPIO pins.
ESP32 Web Server Station Mode - Web Page |
ESP32 Web Server Station Mode Serial Monitor Output - Webpage Accessed |
ESP32 Web Server Station Mode Web Page - LED Control |
ESP32 Web Server Station Mode Serial Monitor Output - LED Control |