Forum / Windows & Yazılım

ESP32 Wi-Fi LED Kontrol

25 gün önce
#include <WiFi.h>
#include <WebServer.h>

// Wi-Fi bilgileri
const char* ssid = "WIFI_ADI";
const char* password = "WIFI_SIFRESI";

// LED pini
const int ledPin = 23;

bool ledDurumu = false;

// Web sunucusu
WebServer server(80);

// Ana web sayfasi
const char HTML[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ESP32 Smart Control</title>
<style>
* { box-sizing:border-box; }
body {
margin:0;
min-height:100vh;
background:#101827;
color:white;
font-family:Arial,sans-serif;
display:flex;
justify-content:center;
align-items:center;
padding:20px;
}
.panel {
width:100%;
max-width:420px;
background:#1c293b;
padding:30px;
border-radius:22px;
text-align:center;
box-shadow:0 15px 45px #0006;
}
h1 { font-size:25px; }
.subtitle { color:#9caec4; }
.light {
font-size:75px;
margin:30px 0 15px;
}
.status {
font-size:19px;
margin-bottom:25px;
color:#ff7373;
}
button {
width:100%;
padding:17px;
border:0;
border-radius:12px;
color:white;
font-size:17px;
font-weight:bold;
margin:7px 0;
cursor:pointer;
}
.on { background:#16a36a; }
.off { background:#dc4545; }
button:active { transform:scale(.98); }
.footer {
color:#8293a9;
font-size:12px;
margin-top:25px;
}
</style>
</head>
<body>
<div class="panel">
<h1>ESP32 SMART CONTROL</h1>
<div class="subtitle">Wi-Fi LED Kontrol Paneli</div>

<div class="light" id="icon">💡</div>
<div class="status" id="status">LED KAPALI</div>

<button class="on" onclick="controlLED('on')">
LED AÇ
</button>

<button class="off" onclick="controlLED('off')">
LED KAPAT
</button>

<div class="footer">
ESP32 Wi-Fi Control System
</div>
</div>

<script>
async function controlLED(action) {
try {
const response = await fetch('/' + action);
if (!response.ok) throw new Error('Baglanti hatasi');
updateStatus(await response.text());
} catch(e) {
document.getElementById('status').innerText =
'Baglanti hatasi!';
}
}

function updateStatus(state) {
const on = state.trim() === 'ON';
document.getElementById('status').innerText =
on ? 'LED AÇIK' : 'LED KAPALI';
document.getElementById('status').style.color =
on ? '#38dc94' : '#ff7373';
document.getElementById('icon').style.opacity =
on ? '1' : '0.35';
}

async function getStatus() {
try {
const r = await fetch('/state');
updateStatus(await r.text());
} catch(e) {}
}

getStatus();
</script>
</body>
</html>
)rawliteral";

void handleRoot() {
server.send(200, "text/html; charset=utf-8", HTML);
}

void handleOn() {
ledDurumu = true;
digitalWrite(ledPin, HIGH);
server.send(200, "text/plain", "ON");
Serial.println("LED ACILDI");
}

void handleOff() {
ledDurumu = false;
digitalWrite(ledPin, LOW);
server.send(200, "text/plain", "OFF");
Serial.println("LED KAPATILDI");
}

void handleState() {
server.send(200, "text/plain",
ledDurumu ? "ON" : "OFF");
}

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

pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);

Serial.println();
Serial.println("ESP32 baslatiliyor...");

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

Serial.print("Wi-Fi baglantisi");

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println();
Serial.println("Wi-Fi baglandi!");

Serial.print("IP Adresi: ");
Serial.println(WiFi.localIP());

server.on("/", handleRoot);
server.on("/on", handleOn);
server.on("/off", handleOff);
server.on("/state", handleState);

server.begin();

Serial.println("Web sunucusu hazir!");
}

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

Cevap yazmak için giriş yapın veya kayıt olun. Destek hesabınız forumda da geçerlidir.