#include <WiFi.h>
#include <WebServer.h>
#include <DHT.h>
// --- Configuration DHT22 ---
#define DHTPIN 3
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// --- Serveur Web ---
WebServer server(80);
void handleRoot() {
// Lire les valeurs du capteur
float t = dht.readTemperature();
float h = dht.readHumidity();
// Gestion d'erreurs
if (isnan(t) || isnan(h)) {
t = 0;
h = 0;
}
// Construire la page HTML complète ici
String html = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>DHT22 - ESP32</title>
<meta http-equiv="refresh" content="5">
<style>
body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }
.card { display: inline-block; padding: 20px; border-radius: 12px; background: #f9f9f9; box-shadow: 0 2px 8px rgba(0,0,0,0.2); }
h1 { margin-bottom: 20px; }
p { font-size: 1.5rem; margin: 10px 0; }
</style>
</head>
<body>
<h1>📡 Mesures du DHT22</h1>
<div class="card">
)rawliteral";
// 🔥 On insère les valeurs dans la page HTML :
html += "<p><b>Température :</b> " + String(t, 1) + " °C</p>";
html += "<p><b>Humidité :</b> " + String(h, 1) + " %</p>";
// 🔚 Fin de la page HTML
html += R"rawliteral(
<p><small>Page actualisée toutes les 5 secondes</small></p>
</div>
</body>
</html>
)rawliteral";
// Envoyer la page au navigateur
server.send(200, "text/html", html);
}
void setup() {
Serial.begin(115200);
Serial.println("Connexion Wi-Fi...");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(200);
Serial.print(".");
}
Serial.println("\n✅ Connecté !");
Serial.print("🌐 Adresse IP : ");
Serial.println(WiFi.localIP());
dht.begin();
server.on("/", handleRoot);
server.begin();
Serial.println("🚀 Serveur HTTP démarré !");
}
void loop() {
server.handleClient();
}
Loading
xiao-esp32-c3
xiao-esp32-c3