// ESP32 mDNS + Web Server + I2C LCD
// LED: GPIO 5 (220 ohm) | LCD: SDA=21 SCL=22 VCC=VIN
#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASS = "";
const char* MDNS_HOSTNAME = "bluegrays-esp";
#define LED_PIN 5
#define I2C_SDA 21
#define I2C_SCL 22
LiquidCrystal_I2C lcd(0x27, 16, 2);
WebServer server(80);
bool ledState = false;
String currentIP = "0.0.0.0";
bool wifiOk = false;
// Sahte istemciler (sunum icin) / Fake clients
const char* clients[] = {
"iPhone-Emre",
"Macbook-Pro",
"iPad-Mini"
};
const uint8_t clientCount = 3;
void lcdLine(uint8_t row, const String &text) {
lcd.setCursor(0, row);
String t = text;
while (t.length() < 16) t += " ";
lcd.print(t.substring(0, 16));
}
void showIdle() {
lcdLine(0, String(MDNS_HOSTNAME) + ".lo");
lcdLine(1, "IP:" + currentIP);
}
// Istemci baglantisi simulasyonu / Client request simulation
void handleClientAction(bool on, const char* client) {
// 1) Cihaz baglandi / Device connected
lcdLine(0, client);
lcdLine(1, on ? "-> AC istegi" : "-> KAPAT istegi");
delay(2000);
// 2) Islendi / Processed
ledState = on;
digitalWrite(LED_PIN, on ? HIGH : LOW);
lcdLine(0, String(MDNS_HOSTNAME) + ".lo");
lcdLine(1, on ? "LED: ACIK OK" : "LED: KAPALI OK");
delay(2500);
}
String htmlPage() {
String h = "<!DOCTYPE html><html><head><meta charset='UTF-8'>";
h += "<title>BlueGrays mDNS</title></head><body style='font-family:Arial;text-align:center;padding:30px'>";
h += "<h1>BlueGrays mDNS</h1>";
h += "<p>URL: http://"; h += MDNS_HOSTNAME; h += ".local/</p>";
h += "<p>IP: "; h += currentIP; h += "</p>";
h += "<p>LED: <b>"; h += (ledState ? "ACIK" : "KAPALI"); h += "</b></p>";
h += "<p><a href='/on'>AC</a> | <a href='/off'>KAPAT</a></p>";
h += "</body></html>";
return h;
}
void handleRoot() { server.send(200, "text/html; charset=utf-8", htmlPage()); }
void handleOn() { ledState = true; digitalWrite(LED_PIN, HIGH); server.sendHeader("Location","/"); server.send(303); }
void handleOff() { ledState = false; digitalWrite(LED_PIN, LOW); server.sendHeader("Location","/"); server.send(303); }
void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// LCD ilk basta hazirla / Init LCD first
Wire.begin(I2C_SDA, I2C_SCL);
lcd.init();
lcd.backlight();
// Splash 3sn / Splash 3s
lcdLine(0, " BlueGrays");
lcdLine(1, " mDNS v1.0");
delay(3000);
// WiFi baglanti — 20sn timeout / WiFi — 20s timeout
lcdLine(0, "WiFi baglaniyor");
lcdLine(1, "Connecting...");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
unsigned long startWifi = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startWifi < 20000) {
delay(400);
}
if (WiFi.status() == WL_CONNECTED) {
wifiOk = true;
currentIP = WiFi.localIP().toString();
lcdLine(0, "WiFi baglandi");
lcdLine(1, currentIP);
delay(2500);
if (MDNS.begin(MDNS_HOSTNAME)) {
MDNS.addService("http", "tcp", 80);
lcdLine(0, "mDNS baslatildi");
lcdLine(1, String(MDNS_HOSTNAME) + ".lo");
delay(2500);
}
server.on("/", handleRoot);
server.on("/on", handleOn);
server.on("/off", handleOff);
server.begin();
} else {
// WiFi yok — offline calismaya devam / WiFi failed — offline
currentIP = "10.0.0.42";
lcdLine(0, "WiFi baglanmadi");
lcdLine(1, "Offline calisma");
delay(2500);
}
showIdle();
delay(2000);
}
void loop() {
if (wifiOk) server.handleClient();
// 4sn'de bir farkli cihaz baglaniyor / Every 4s another client
static unsigned long lastAction = 0;
static uint8_t step = 0;
if (millis() - lastAction > 4000) {
lastAction = millis();
const char* who = clients[step % clientCount];
bool on = (step % 2 == 0);
handleClientAction(on, who);
showIdle();
step++;
}
delay(2);
}