#include <WiFi.h>
#include <WebServer.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
#define MQ135_PIN 34
const char* ssid = "FST-B"; // Your WiFi SSID
const char* password = "WiFi$2026"; // Your WiFi password
WebServer server(80);
DHT dht(DHTPIN, DHTTYPE);
int airValue;
float temperature;
float humidity;
String getAirQuality(int value) {
if (value < 1000) return "Good";
else if (value < 2000) return "Moderate";
else return "Poor";
}
void handleRoot() {
airValue = analogRead(MQ135_PIN);
temperature = dht.readTemperature();
humidity = dht.readHumidity();
String airStatus = getAirQuality(airValue);
String html = "<!DOCTYPE html><html>";
html += "<head><meta http-equiv='refresh' content='2'>";
html += "<title>Air Quality Monitor</title>";
html += "<style>";
html += "body { font-family: Arial; text-align: center; background-color: #f4f4f4; }";
html += "h1 { color: #333; }";
html += ".card { background: white; padding: 20px; margin: 20px auto; width: 300px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,0.2);} ";
html += "</style></head><body>";
html += "<h1>ESP32 Air Quality Monitoring</h1>";
html += "<div class='card'>";
html += "<h3>Air Value: " + String(airValue) + "</h3>";
html += "<h3>Status: " + airStatus + "</h3>";
html += "<h3>Temperature: " + String(temperature) + " °C</h3>";
html += "<h3>Humidity: " + String(humidity) + " %</h3>";
html += "</div></body></html>";
server.send(200, "text/html", html);
}
void setup() {
Serial.begin(115200);
dht.begin();
Serial.println("Connecting to WiFi: " + String(ssid));
WiFi.begin(ssid, password);
int retries = 0;
while (WiFi.status() != WL_CONNECTED && retries < 20) { // Try 20 times (~10 seconds)
delay(500);
Serial.print(".");
retries++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nConnected to WiFi!");
Serial.print("ESP32 IP Address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("\nFailed to connect to WiFi.");
}
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}