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

ESP32 DHT Sensor Web Server Code

This document contains an Arduino sketch for an ESP32 microcontroller that connects to a WiFi network and serves a web page displaying temperature and humidity readings from a DHT11 sensor. The web page refreshes every 4 seconds and uses HTML and CSS for formatting. The code includes functions to read temperature and humidity from the sensor and handle HTTP requests from clients.
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)
8 views4 pages

ESP32 DHT Sensor Web Server Code

This document contains an Arduino sketch for an ESP32 microcontroller that connects to a WiFi network and serves a web page displaying temperature and humidity readings from a DHT11 sensor. The web page refreshes every 4 seconds and uses HTML and CSS for formatting. The code includes functions to read temperature and humidity from the sensor and handle HTTP requests from clients.
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>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <DHT.h>

const char *ssid = "My-Network";


const char *password = "chaker123456";

WebServer server(80);
DHT dht(26, DHT11);

void handleRoot() {
char msg[1500];

snprintf(msg, 1500,
"<html>\
<head>\
<meta http-equiv='refresh' content='4'/>\
<meta name='viewport' content='width=device-width, initial-scale=1'>\
<link rel='stylesheet' href='[Link]
integrity='sha384-
fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr'
crossorigin='anonymous'>\
<title>ESP32 DHT Server</title>\
<style>\
html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center;}\
h2 { font-size: 3.0rem; }\
p { font-size: 3.0rem; }\
.units { font-size: 1.2rem; }\
.dht-labels{ font-size: 1.5rem; vertical-align:middle; padding-bottom: 15px;}\
</style>\
</head>\
<body>\
<h2>ESP32 DHT Server!</h2>\
<p>\
<i class='fas fa-thermometer-half' style='color:#ca3517;'></i>\
<span class='dht-labels'>Temperature</span>\
<span>%.2f</span>\
<sup class='units'>&deg;C</sup>\
</p>\
<p>\
<i class='fas fa-tint' style='color:#00add6;'></i>\
<span class='dht-labels'>Humidity</span>\
<span>%.2f</span>\
<sup class='units'>&percnt;</sup>\
</p>\
</body>\
</html>",
readDHTTemperature(), readDHTHumidity()
);
[Link](200, "text/html", msg);
}

void setup(void) {

[Link](115200);
[Link]();

[Link](WIFI_STA);
[Link](ssid, password);
[Link]("");

// Wait for connection


while ([Link]() != WL_CONNECTED) {
delay(500);
[Link](".");
}

[Link]("");
[Link]("Connected to ");
[Link](ssid);
[Link]("IP address: ");
[Link]([Link]());

if ([Link]("esp32")) {
[Link]("MDNS responder started");
}
[Link]("/", handleRoot);

[Link]();
[Link]("HTTP server started");
}

void loop(void) {
[Link]();
delay(2);//allow the cpu to switch to other tasks
}

float readDHTTemperature() {
// Sensor readings may also be up to 2 seconds
// Read temperature as Celsius (the default)
float t = [Link]();
if (isnan(t)) {
[Link]("Failed to read from DHT sensor!");
return -1;
}
else {
[Link](t);
return t;
}
}

float readDHTHumidity() {
// Sensor readings may also be up to 2 seconds
float h = [Link]();
if (isnan(h)) {
[Link]("Failed to read from DHT sensor!");
return -1;
}
else {
[Link](h);
return h;
}
}

You might also like