Experiment No. 10
Experiment No. 10
Design and Implementation of HTTP based IoT Web Server to control the status of LED
APPARATUS:
S No Name of the Equipment Quantity
1 ESP32 Development Board 1
2 DHT11 or DHT22 temperature and humidity sensor 1
3 Jumper wires 3
4 Micro USB cable 1
An HTTP based webserver provides access to data to an HTTP client such as web browser. In
IoT applications, the IoT Nodes are connected to sensors and actuators. The sensor data can be
accessed using a web browser and also the actuators can be controlled from the web browser.
This facilitates a wide variety of applications in IoT domain. The underlying protocol used for
this communication between a Client and a Server is HTTP. The goal of this lab is to understand
the configuration and working principle of an IoT web server. Using a browser, such as google
chrome, we will send some HTTP based commands to the web server. Based on the type of
command, the IoT Node web server will toggle the LEDs.
CODE:
#include <WiFi.h>
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
pinMode(2, OUTPUT); // set the LED pin mode
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
int value = 0;
void loop(){
WiFiClient client = server.available(); // listen for incoming clients
// 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();
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(2, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(2, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}
On Web browser/Web Client: