[go: up one dir, main page]

0% found this document useful (0 votes)
18 views4 pages

Experiment No. 10

The document outlines an experiment to design and implement an HTTP-based IoT web server for controlling an LED using an ESP32 development board and a temperature and humidity sensor. It describes the apparatus needed, the working principle of the web server, and includes sample code for setting up the server and handling client requests to toggle the LED. The experiment aims to demonstrate the interaction between IoT nodes and web clients through HTTP commands.

Uploaded by

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

Experiment No. 10

The document outlines an experiment to design and implement an HTTP-based IoT web server for controlling an LED using an ESP32 development board and a temperature and humidity sensor. It describes the apparatus needed, the working principle of the web server, and includes sample code for setting up the server and handling client requests to toggle the LED. The experiment aims to demonstrate the interaction between IoT nodes and web clients through HTTP commands.

Uploaded by

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

Experiment -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>

const char* ssid = "Mahesh";


const char* password = "012345678";

WiFiServer server(80);

void setup()
{
Serial.begin(115200);
pinMode(2, OUTPUT); // set the LED pin mode

delay(10);

// We start by connecting to a WiFi network

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


delay(500);
Serial.print(".");
}

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 (client) { // if you get a client,


Serial.println("New Client."); // print a message out 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
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();

// the content of the HTTP response follows the header:


client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 2 on.<br>");
client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 2 off.<br>");

// 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
}

// 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:

You might also like