0% found this document useful (0 votes)
16 views3 pages

ESP32 DHT11 Ultrasonic Sensor Web Server

This document contains an Arduino sketch for an ESP32 microcontroller that reads data from a DHT11 temperature and humidity sensor and an ultrasonic distance sensor. It connects to a Wi-Fi network and sets up a web server to display the sensor data on a webpage. The code includes functions for reading sensor values and handling HTTP requests from clients.

Uploaded by

akshitha.k1911
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)
16 views3 pages

ESP32 DHT11 Ultrasonic Sensor Web Server

This document contains an Arduino sketch for an ESP32 microcontroller that reads data from a DHT11 temperature and humidity sensor and an ultrasonic distance sensor. It connects to a Wi-Fi network and sets up a web server to display the sensor data on a webpage. The code includes functions for reading sensor values and handling HTTP requests from clients.

Uploaded by

akshitha.k1911
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

#include <WiFi.

h> // For ESP32


#include "DHT.h" // DHT sensor library

// Pin definitions
#define DHTPIN 33 /* DHT11 sensor connected to GPIO14 */
#define TRIGPIN 13 /* Ultrasonic Trig pin connected to GPIO13 */
#define ECHOPIN 12 /* Ultrasonic Echo pin connected to GPIO15 */

#define DHTTYPE DHT11 /* Define DHT sensor type, DHT11 */

const char* ssid = "Galaxy"; /* Add your router's SSID */


const char* password = "123456789"; /* Add the password */

// Create DHT object


DHT dht(DHTPIN, DHTTYPE);

// Create a WiFi server on port 80


WiFiServer espServer(80);

// Ultrasonic distance function


long readUltrasonicDistance() {
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
long duration = pulseIn(ECHOPIN, HIGH);
long distance = (duration * 0.034) / 2; // Convert to distance in cm
return distance;
}

void setup() {
[Link](115200); /* Begin Serial Communication with 115200 Baud
Rate */

// Pin mode for ultrasonic and DHT


pinMode(TRIGPIN, OUTPUT);
pinMode(ECHOPIN, INPUT);

[Link](); // Start DHT sensor

// Connect to WiFi
[Link]("\nConnecting to: ");
[Link](ssid);
[Link](WIFI_STA); // ESP32 in STA Mode
[Link](ssid, password); // Connect to WiFi

while ([Link]() != WL_CONNECTED) {


[Link]("*");
delay(500);
}

[Link]("\nConnected to Wi-Fi.");
[Link](); // Start HTTP server
[Link]("ESP32 Web Server URL: [Link]
[Link]([Link]()); // Print IP address
}

void loop() {
// Read humidity and temperature from DHT11
float humidity = [Link]();
float temperature = [Link]();

// Read distance from Ultrasonic sensor


long distance = readUltrasonicDistance();

// Check for new clients


WiFiClient client = [Link]();
if (!client) {
return;
}

[Link]("New Client!");
String request = [Link]('\r'); // Read request
[Link](request);
[Link]();

// Send HTTP response


[Link]("HTTP/1.1 200 OK");
[Link]("Content-Type: text/html");
[Link]();
[Link]("<!DOCTYPE HTML>");
[Link]("<html>");
[Link]("<head><title>stanley collage of engg</title></head>");

// Webpage body
[Link]("<body>");
[Link]("<h2>sensor data display</h2>");

// DHT11 sensor data


if (isnan(humidity) || isnan(temperature)) {
[Link]("<p>Failed to read from DHT sensor!</p>");
} else {
[Link]("<p>Temperature: " + String(temperature) + "
&deg;C</p>");
[Link]("<p>Humidity: " + String(humidity) + " %</p>");
}

// Ultrasonic sensor data


[Link]("<p>Distance: " + String(distance) + " cm</p>");

[Link]("</body>");
[Link]("</html>");

delay(100); // Give the server time to handle the request


}

You might also like