#include <WiFi.h>
#include <WebServer.h>
#include <DHT.h>

#define DHTPIN 4       // Pin where the DHT22 is connected
#define DHTTYPE DHT22  // DHT 22 (AM2302), AM2321
#define MQ2PIN 36      // Pin where the MQ-2 analog output is connected

// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

DHT dht(DHTPIN, DHTTYPE);
WebServer server(80);

void setup() {
  Serial.begin(115200);
  dht.begin();

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Start the server
  server.on("/", handleRoot);
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
}

void handleRoot() {
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();
  int gas = analogRead(MQ2PIN);

  if (isnan(temperature) || isnan(humidity)) {
    server.send(500, "text/plain", "Failed to read from DHT sensor");
    return;
  }

  String html = "<!DOCTYPE html><html><head><title>ESP32 Sensor Data</title></head><body>";
  html += "<h1>ESP32 Sensor Data</h1>";
  html += "<p>Temperature: " + String(temperature) + " °C</p>";
  html += "<p>Humidity: " + String(humidity) + " %</p>";
  html += "<p>Gas: " + String(gas) + "</p>";
  html += "</body></html>";

  server.send(200, "text/html", html);
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK