#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#define DHTPIN 15
#define DHTTYPE DHT11
#define LDR_PIN 34
// Replace with your Wi-Fi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
WiFiServer server(80);
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi in STA mode
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP Address: ");
Serial.println(WiFi.localIP());
server.begin();
dht.begin();
lcd.init();
lcd.backlight();
}
void loop() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
int ldrValue = analogRead(LDR_PIN);
// Display on LCD
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(temp);
lcd.print("C H:");
lcd.print(hum);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("LDR:");
lcd.print(ldrValue);
WiFiClient client = server.available();
if (client) {
Serial.println("New Client.");
String request = client.readStringUntil('\r');
client.flush();
// HTML page
String html = "<!DOCTYPE html><html><head><meta name='viewport' content='width=device-width, initial-scale=1.0'></head><body>";
html += "<h1>ESP32 IoT Sensor Data</h1>";
html += "<p>Temperature: " + String(temp) + " °C</p>";
html += "<p>Humidity: " + String(hum) + " %</p>";
html += "<p>LDR Value: " + String(ldrValue) + "</p>";
html += "</body></html>";
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.println(html);
client.stop();
Serial.println("Client disconnected.");
}
delay(2000);
}