0% found this document useful (0 votes)
50 views7 pages

Act06 2

The document discusses how web servers work using an ESP32 microcontroller as an example. It explains request-response, client-server models, IP addresses, and building a simple web server to control outputs. Code is provided to demonstrate a web page to toggle an LED connected to the ESP32.

Uploaded by

janethroseudarbe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views7 pages

Act06 2

The document discusses how web servers work using an ESP32 microcontroller as an example. It explains request-response, client-server models, IP addresses, and building a simple web server to control outputs. Code is provided to demonstrate a web page to toggle an LED connected to the ESP32.

Uploaded by

janethroseudarbe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

MARIANO MARCOS STATE UNIVERSITY

College of Engineering

Name: 1. _____________________________ Date: ____________________________


2. _____________________________
Course and Year: _____________________ Score: _______

Laboratory No. 06
Introduction Webserver

Pinout Reference:

Request-response

Request-response is a message exchange pattern in which a requestor sends a request


message to a replier system that receives and processes the request and returns a message in
response.

Client-Server

When you type an URL in your browser, what happens in the background is that you
(the client) send a request via Hypertext Transfer Protocol (HTTP) to a server. When the server
receives the request, it sends a response also through HTTP, and you see the web page you
requested in your browser. Clients and servers communicate over a computer network.

COE Building, Quiling Sur, City of Batac, Ilocos Norte


 coe@mmsu.edu.ph  +63(77)600-3458
www.mmsu.edu.ph

MMSU @45: ACHIEVE-ing


more for the future
MARIANO MARCOS STATE UNIVERSITY
College of Engineering

Server Host

A server host runs one or more server programs to share their resources with clients.
So, you can imagine a web server as a piece of software that listens for incoming HTTP requests
and sends responses when requested.

Your ESP32 can act as a server host, listening for HTTP requests from clients. When a
new client makes a request, the ESP32 sends an HTTP response.

IP Address

An IP address is a numerical label assigned to each device connected to a computer


network. This way, any information sent to that device can reach it by referring to its IP
address. So, your ESP has an IP address too.

ESP32 Web Server

Typically, a web server with the ESP32 in the local network looks like this: the ESP32
running as a web server is connected via Wi-Fi to your router. Your computer, smartphone, or
tablet are also connected to your router via Wi-Fi or Ethernet cable. So, the ESP32 and your
browser are on the same network.

COE Building, Quiling Sur, City of Batac, Ilocos Norte


 coe@mmsu.edu.ph  +63(77)600-3458
www.mmsu.edu.ph

MMSU @45: ACHIEVE-ing


more for the future
MARIANO MARCOS STATE UNIVERSITY
College of Engineering

When you type the ESP IP address in your browser, you are sending an HTTP request to
your ESP32. Then, the ESP32 responds with a response that can contain a value, a reading,
HTML text to display a web page, or any other data.

Web Server Example

Here’s an example of a web server we’ve built to control an output. The following web
page shows up when you enter the ESP IP address in a browser. When you press the ON button,
the URL changes to the ESP IP address followed by /ON. The ESP receives a request on that URL,
so it checks with an if statement which URL is being requested and changes the LED state
accordingly.

Sample Web Server – Control Outputs

Circuit

COE Building, Quiling Sur, City of Batac, Ilocos Norte


 coe@mmsu.edu.ph  +63(77)600-3458
www.mmsu.edu.ph

MMSU @45: ACHIEVE-ing


more for the future
MARIANO MARCOS STATE UNIVERSITY
College of Engineering

Webpage

Finding the ESP IP Address

This can be found in the Serial Monitor

Note: If nothing shows up on the Serial Monitor, press the ESP32 “EN” button (enable/reset
button next to the micro USB port).

Accessing the Web Server

Open your browser, paste the ESP32 IP address, and you’ll see the following page.

Source Code:

COE Building, Quiling Sur, City of Batac, Ilocos Norte


 coe@mmsu.edu.ph  +63(77)600-3458
www.mmsu.edu.ph

MMSU @45: ACHIEVE-ing


more for the future
MARIANO MARCOS STATE UNIVERSITY
College of Engineering

#include <WiFi.h>

// Replace with your network credentials


const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_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;
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(){
WiFiClient client = server.available(); // Listen for incoming clients

if (client) { // If a new client connects,


currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // print a message out in the serial port

COE Building, Quiling Sur, City of Batac, Ilocos Norte


 coe@mmsu.edu.ph  +63(77)600-3458
www.mmsu.edu.ph

MMSU @45: ACHIEVE-ing


more for the future
MARIANO MARCOS STATE UNIVERSITY
College of Engineering
String currentLine = ""; // make a String to hold incoming data
from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) { //
loop while the client's connected
currentTime = millis();
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("Content-type:text/html");
client.println("Connection: close");
client.println();

// 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>");

COE Building, Quiling Sur, City of Batac, Ilocos Norte


 coe@mmsu.edu.ph  +63(77)600-3458
www.mmsu.edu.ph

MMSU @45: ACHIEVE-ing


more for the future
MARIANO MARCOS STATE UNIVERSITY
College of Engineering
// 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>");
}
client.println("</body></html>");

// The HTTP response ends with another blank line


client.println();
// Break out of the while loop
break;
} 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
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}

Activity 06:
 Create a program that will control 3 LEDs with the following conditions:
a. Button1 is for the 1st LED
b. Button2 is for the 2nd LED
c. Button 3 is for the 3rd LED
d. Button4 is for all the 3 LEDs
e. Modify the display in the webpage (you can change the font color, style, and size; and the
object figure and its position)

COE Building, Quiling Sur, City of Batac, Ilocos Norte


 coe@mmsu.edu.ph  +63(77)600-3458
www.mmsu.edu.ph

MMSU @45: ACHIEVE-ing


more for the future

You might also like