#include <WiFi.h>
#include <WebServer.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 4
#define DHTTYPE DHT22
#define LED_PIN 26
const char* ssid = "Wokwi-GUEST";
const char* password = "";
DHT dht(DHTPIN, DHTTYPE);
WebServer server(80);
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool ledState = false;
void handleRoot() {
float t = dht.readTemperature();
float h = dht.readHumidity();
String page = "<html><body>";
page += "<h2>ESP32 Dashboard</h2>";
page += "<p>Harorat: " + String(t,1) + " C</p>";
page += "<p>Namlik: " + String(h,1) + " %</p>";
page += "<p>LED: " + String(ledState?"YONIQ":"OCHIQ") + "</p>";
page += "<a href='/ledon'>LED Yoq</a> | ";
page += "<a href='/ledoff'>LED Ochir</a>";
page += "</body></html>";
server.send(200, "text/html", page);
}
void handleLedOn() {
ledState = true;
digitalWrite(LED_PIN, HIGH);
lcd.setCursor(0,1);
lcd.print("LED: YONIQ ");
server.sendHeader("Location", "/");
server.send(302);
}
void handleLedOff() {
ledState = false;
digitalWrite(LED_PIN, LOW);
lcd.setCursor(0,1);
lcd.print("LED: OCHIQ ");
server.sendHeader("Location", "/");
server.send(302);
}
void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
dht.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("WiFi ulanmoqda..");
WiFi.begin(ssid, password);
int tries = 0;
while (WiFi.status() != WL_CONNECTED && tries < 30) {
delay(500);
tries++;
}
lcd.clear();
if (WiFi.status() == WL_CONNECTED) {
String ip = WiFi.localIP().toString();
lcd.setCursor(0,0);
lcd.print("IP:");
lcd.print(ip);
lcd.setCursor(0,1);
lcd.print("Server tayyor!");
delay(4000);
} else {
lcd.print("WiFi XATO!");
delay(2000);
}
server.on("/", handleRoot);
server.on("/ledon", handleLedOn);
server.on("/ledoff", handleLedOff);
server.begin();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("T:-- H:--");
lcd.setCursor(0,1);
lcd.print("LED: OCHIQ");
}
void loop() {
server.handleClient();
static unsigned long last = 0;
if (millis() - last > 2000) {
float t = dht.readTemperature();
float h = dht.readHumidity();
lcd.setCursor(0,0);
lcd.print("T:" + String(t,1) + "C H:" + String(h,0) + "% ");
last = millis();
}
}Loading
esp32-devkit-v1
esp32-devkit-v1