Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiUdp.h>
- #include <NTPClient.h>
- #include <ESP8266WebServer.h>
- #define RELAY_PIN D1
- #define LDR_PIN A0
- // ===== Wi-Fi Credentials =====
- const char* ssid = "TestESP";
- const char* password = "12345678";
- // ===== Settings =====
- int threshold = 600; // Default LDR threshold
- int startHour = 18, startMinute = 0, startSecond = 0;
- int endHour = 6, endMinute = 0, endSecond = 0;
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800, 60000); // +5:30 offset (India)
- ESP8266WebServer server(80);
- bool manualOverride = false;
- bool manualState = false;
- void handleRoot() {
- int ldrValue = analogRead(LDR_PIN);
- timeClient.update();
- int hourNow = timeClient.getHours();
- bool dark = (ldrValue < threshold);
- bool night = (hourNow >= startHour || hourNow < endHour);
- bool autoState = (dark || night);
- bool lightOn = manualOverride ? manualState : autoState;
- String html = R"rawliteral(
- <!DOCTYPE html><html><head>
- <meta name='viewport' content='width=device-width, initial-scale=1'>
- <title>Smart Auto Light</title>
- <style>
- body{margin:0;font-family:Poppins,Arial;background:#111;color:#fff;text-align:center;}
- .container{max-width:400px;margin:auto;padding:20px;}
- input,button{width:90%;padding:10px;margin:8px 0;border:none;border-radius:10px;font-size:16px;}
- button{cursor:pointer;}
- .on{background:#4caf50;color:white;}
- .off{background:#f44336;color:white;}
- .switch{background:#2196f3;color:white;}
- .card{background:#222;border-radius:15px;padding:15px;margin:10px 0;}
- </style></head><body>
- <div class='container'>
- <h2>🌙 Smart Auto Light</h2>
- <div class='card'>
- <b>LDR Value:</b> <span id='ldr'>)rawliteral" + String(ldrValue) + R"rawliteral(</span><br>
- <b>Time:</b> <span id='time'>)rawliteral" + String(hourNow) + R"rawliteral(:00</span><br>
- <b>Mode:</b> <span id='mode'>)rawliteral" + String(manualOverride ? "Manual" : "Auto") + R"rawliteral(</span><br>
- <b>Light:</b> <span id='light'>)rawliteral" + String(lightOn ? "ON" : "OFF") + R"rawliteral(</span>
- </div>
- <div class='card'>
- <h3>Manual Control</h3>
- <button class='on' onclick="send('on')">ON</button>
- <button class='off' onclick="send('off')">OFF</button>
- <button class='switch' onclick="send('mode')">Switch to )rawliteral" + String(manualOverride ? "Auto" : "Manual") + R"rawliteral(</button>
- </div>
- <div class='card'>
- <h3>Settings</h3>
- <form onsubmit="updateSettings(event)">
- Threshold: <input type='number' id='th' value=')rawliteral" + String(threshold) + R"rawliteral('><br>
- Start Time (HH:MM:SS): <input type='text' id='st' value=')rawliteral" + String(startHour) + ":" + String(startMinute) + ":" + String(startSecond) + R"rawliteral('><br>
- End Time (HH:MM:SS): <input type='text' id='et' value=')rawliteral" + String(endHour) + ":" + String(endMinute) + ":" + String(endSecond) + R"rawliteral('><br>
- <button class='switch' type='submit'>Update</button>
- </form>
- </div>
- </div>
- <script>
- function send(cmd){
- fetch('/'+cmd).then(()=>location.reload());
- }
- function updateSettings(e){
- e.preventDefault();
- const th = document.getElementById('th').value;
- const st = document.getElementById('st').value;
- const et = document.getElementById('et').value;
- fetch(`/set?th=${th}&st=${st}&et=${et}`).then(()=>location.reload());
- }
- </script>
- </body></html>
- )rawliteral";
- server.send(200, "text/html", html);
- }
- void handleCommand() {
- String cmd = server.uri();
- if (cmd == "/on") { manualOverride = true; manualState = true; }
- else if (cmd == "/off") { manualOverride = true; manualState = false; }
- else if (cmd == "/mode") { manualOverride = !manualOverride; }
- server.sendHeader("Location", "/");
- server.send(303);
- }
- void handleSet() {
- if (server.hasArg("th")) threshold = server.arg("th").toInt();
- if (server.hasArg("st")) {
- sscanf(server.arg("st").c_str(), "%d:%d:%d", &startHour, &startMinute, &startSecond);
- }
- if (server.hasArg("et")) {
- sscanf(server.arg("et").c_str(), "%d:%d:%d", &endHour, &endMinute, &endSecond);
- }
- server.sendHeader("Location", "/");
- server.send(303);
- }
- void setup() {
- Serial.begin(115200);
- pinMode(RELAY_PIN, OUTPUT);
- digitalWrite(RELAY_PIN, LOW);
- Serial.println("\nConnecting to Wi-Fi...");
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("\nWi-Fi connected: " + WiFi.localIP().toString());
- timeClient.begin();
- timeClient.update();
- server.on("/", handleRoot);
- server.on("/on", handleCommand);
- server.on("/off", handleCommand);
- server.on("/mode", handleCommand);
- server.on("/set", handleSet);
- server.begin();
- Serial.println("Web server started!");
- }
- void loop() {
- server.handleClient();
- timeClient.update();
- int ldrValue = analogRead(LDR_PIN);
- int hourNow = timeClient.getHours();
- bool dark = (ldrValue < threshold);
- bool night = (hourNow >= startHour || hourNow < endHour);
- bool autoState = (dark || night);
- bool lightOn = manualOverride ? manualState : autoState;
- digitalWrite(RELAY_PIN, lightOn ? LOW : HIGH);
- delay(1000);
- }
Advertisement