#include <WiFi.h>
#include <DHT.h>
/**
* PROJET : Système Domotique via ESP32 (Web Server)
* ÉTUDIANTE : AMEL
* DESCRIPTION : Contrôle d'une Lampe et d'un Ventilateur + Capteur DHT11
*/
// --- Configuration du Réseau WiFi ---
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// --- Définition des Broches (Pins GPIO) ---
const int LAMP_PIN = 21; // المصباح مربوط بالرجل 21
const int VENT_PIN = 19; // المروحة مربوطة بالرجل 19
#define DHTPIN 22 // مستشعر الحرارة مربوط بالرجل 22 (GPIO 4)
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
WiFiServer server(80);
void setup() {
Serial.begin(115200);
// Configuration des sorties
pinMode(LAMP_PIN, OUTPUT);
pinMode(VENT_PIN, OUTPUT);
// Éteindre tout au début
digitalWrite(LAMP_PIN, LOW);
digitalWrite(VENT_PIN, LOW);
dht.begin();
// Connexion WiFi
Serial.print("Connexion à : ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Connecté !");
Serial.print("Adresse IP : ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n') {
if (currentLine.length() == 0) {
// Lecture des capteurs
float h = dht.readHumidity();
float t = dht.readTemperature();
// --- En-tête HTTP ---
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// --- Page Web (HTML/CSS) ---
client.print("<!DOCTYPE html><html lang='fr'>");
client.print("<head><meta charset='UTF-8'><meta name='viewport' content='width=device-width, initial-scale=1'>");
client.print("<title>Amel Smart Home</title>");
client.print("<style>");
client.print("body { font-family: 'Segoe UI', sans-serif; background: #f0f4f7; text-align: center; color: #333; }");
client.print(".container { max-width: 450px; margin: 30px auto; background: white; padding: 20px; border-radius: 25px; box-shadow: 0 10px 30px rgba(0,0,0,0.1); }");
client.print("h1 { color: #2c3e50; font-size: 26px; }");
client.print(".info { font-size: 14px; color: #7f8c8d; border-bottom: 2px solid #3498db; padding-bottom: 10px; margin-bottom: 20px; }");
client.print(".sensor-grid { display: flex; justify-content: space-around; margin-bottom: 25px; }");
client.print(".sensor-card { background: #3498db; color: white; padding: 15px; border-radius: 15px; width: 40%; }");
client.print(".control-card { background: #fdfdfd; padding: 15px; border: 1px solid #eee; border-radius: 15px; margin-bottom: 15px; }");
client.print(".btn { padding: 12px 25px; font-size: 16px; font-weight: bold; color: white; border: none; border-radius: 10px; cursor: pointer; text-decoration: none; display: inline-block; transition: 0.3s; }");
client.print(".on { background-color: #27ae60; margin-right: 5px; } .off { background-color: #c0392b; }");
client.print(".footer { font-size: 11px; color: #bdc3c7; margin-top: 20px; }");
client.print("</style></head>");
client.print("<body><div class='container'>");
client.print("<h1>🏠 Smart Control</h1>");
client.print("<div class='info'>Réalisé par : <b>AMEL</b><br>Univ Blida 1 - Electronique</div>");
// Section Capteurs
client.print("<div class='sensor-grid'>");
client.print("<div class='sensor-card'>🌡️ " + String(t, 1) + "°C</div>");
client.print("<div class='sensor-card'>💧 " + String(h, 0) + "%</div>");
client.print("</div>");
// Section Lampe
client.print("<div class='control-card'><h3>💡 Éclairage Salon</h3>");
client.print("<a href='/LON' class='btn on'>ON</a><a href='/LOFF' class='btn off'>OFF</a></div>");
// Section Ventilateur
client.print("<div class='control-card'><h3>🌀 Ventilateur</h3>");
client.print("<a href='/VON' class='btn on'>ON</a><a href='/VOFF' class='btn off'>OFF</a></div>");
client.print("<div class='footer'>Projet de Fin d'Études - 2026</div>");
client.print("</div></body></html>");
client.println();
break;
} else { currentLine = ""; }
} else if (c != '\r') { currentLine += c; }
// --- منطق التحكم (Logic) ---
if (currentLine.endsWith("GET /LON")) digitalWrite(LAMP_PIN, HIGH);
if (currentLine.endsWith("GET /LOFF")) digitalWrite(LAMP_PIN, LOW);
if (currentLine.endsWith("GET /VON")) digitalWrite(VENT_PIN, HIGH);
if (currentLine.endsWith("GET /VOFF")) digitalWrite(VENT_PIN, LOW);
}
}
client.stop();
}
}