#include <WebServer.h>
#include <DHT.h>
#include <WiFi.h>
#define DHTPIN 14
#define DHTTYPE DHT22
const char* ssid = "Wokwi-GUEST";
const char* password = "";
DHT dht(DHTPIN, DHTTYPE);
WebServer server(80);
String createWebPage(float temp, float humid)
{
String html = R"rawliteral(
<html>
<head>
<meta http-equiv="refresh" content="5">
<style>
body {
background: white;
font-family: Arial;
padding: 20px;
}
.bar {
background: red;
border-radius: 25px;
height: 30px;
width: 100%;
overflow: hidden;
margin-bottom: 20px;
}
.fill {
height: 100%;
background: green;
border-radius: 25px;
text-align: right;
padding-right: 10px;
line-height: 30px;
color: white;
}
</style>
</head>
<body>
<h2>ESP32 SENSOR DATA</h2>
<h3>Temperature</h3>
<div class="bar">
<div class="fill" style="width:TEMP%;">
TEMP %
</div>
</div>
<h3>Humidity</h3>
<div class="bar">
<div class="fill" style="width:HUMID%;">
HUMID %
</div>
</div>
</body>
</html>
)rawliteral";
html.replace("TEMP",String(temp,1));
html.replace("HUMID",String(humid,1));
return html;
}
void handleRoot() {
float temp = dht.readTemperature();
float humid = dht.readHumidity();
server.send(200, "text/html", createWebPage(temp,humid));
}
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.begin();
}
void loop() {
server.handleClient();
}