#include <WiFi.h>
#include <WebServer.h>
const int HALL_PIN = 2;
const int LED_PIN = 1;
// اسم الشبكة مختصر وبدون كلمة مرور
const char* ssid = "Wokwi-GUEST";
const char* pass = "";
WebServer server(80);
int rawValue = 0;
float voltage = 0.0;
String statusText = "لا يوجد مغناطيس قريب";
void handleRoot() {
String html = "<!DOCTYPE html><html><head><meta charset='utf-8'>";
html += "<meta name='viewport' content='width=width-device, initial-scale=1.0'>";
html += "<title>ESP32 Sensor</title>";
html += "<style>body{font-family:sans-serif; text-align:center; background:#f4f4f9; padding:20px;}";
html += ".card{background:white; padding:30px; border-radius:10px; box-shadow:0 4px 8px rgba(0,0,0,0.1); display:inline-block;}";
html += "h1{color:#333;} .value{font-size:24px; color:#007BFF; font-weight:bold;}";
html += ".status{font-size:20px; color:#28a745; margin-top:15px; font-weight:bold;}</style>";
html += "<script>setInterval(function(){ fetch('/data').then(response => response.json()).then(data => {";
html += "document.getElementById('raw').innerText = data.raw;";
html += "document.getElementById('volt').innerText = data.volt + ' V';";
html += "document.getElementById('status').innerText = data.status;";
html += "}); }, 400);</script>"; // تم إصلاح علامة التنصيص هنا
html += "</head><body>";
html += "<div class='card'><h1>حساس SS49E</h1>";
html += "<p>القراءة الرقمية: <span id='raw'>" + String(rawValue) + "</span></p>";
html += "<p>الجهد: <span id='volt'>" + String(voltage, 2) + " V</span></p>";
html += "<div class='status' id='status'>" + statusText + "</div></div>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void handleData() {
String json = "{\"raw\":" + String(rawValue) + ",\"volt\":" + String(voltage, 2) + ",\"status\":\"" + statusText + "\"}";
server.send(200, "application/json", json);
}
void setup() {
pinMode(LED_PIN, OUTPUT);
analogReadResolution(12);
//WiFi.softAP(ssid, pass);
WiFi.begin(ssid, pass, 6);
server.on("/", handleRoot);
server.on("/data", handleData);
server.begin();
}
void loop() {
server.handleClient();
rawValue = analogRead(HALL_PIN);
voltage = (rawValue / 4095.0) * 3.3;
if (rawValue > 2300 || rawValue < 1800) {
digitalWrite(LED_PIN, HIGH);
statusText = "⚠️ مغناطيس قريب!";
} else {
digitalWrite(LED_PIN, LOW);
statusText = "✅ الوضع آمن";
}
delay(30);
}