#include #include #include // WiFiManager.h আর দরকার নেই, কারণ WiFi এখন কোডের মধ্যেই বিল্টইন থাকবে // ---------------- WIFI CREDENTIALS (হার্ডকোডেড, পরিবর্তন করে re-upload করুন) ---------------- const char* WIFI_SSID = "YOUR_WIFI_SSID"; // এখানে আপনার WiFi নাম দিন const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD"; // এখানে আপনার WiFi পাসওয়ার্ড দিন // ---------------- ADMIN NUMBERS (hardcoded, edit + re-upload to change) ---------------- const char* ADMIN_NUMBER_1 = "+8801878924242"; // controls Load 1 by calling/SMS const char* ADMIN_NUMBER_2 = "+8801923996953"; // controls Load 2 by calling/SMS // ---------------- PIN CONFIG ---------------- #define AC_DETECT_PIN 34 #define LOAD_RELAY1_PIN 26 #define LOAD_RELAY2_PIN 27 #define STATUS_LED_PIN 2 #define SIM900_RX_PIN 16 // ESP32 RX2 <- SIM900A TXD #define SIM900_TX_PIN 17 // ESP32 TX2 -> SIM900A RXD // বেশিরভাগ সস্তা 2-Channel Relay Module "Active-LOW" (IN পিনে LOW দিলে রিলে ON হয়)। // আপনার মডিউল যদি উল্টো (HIGH = ON) হয়, তাহলে এটা false করে দিন। #define RELAY_ACTIVE_LOW true // রিলে অন/অফ করার জন্য এই একটা ফাংশন ব্যবহার হবে সব জায়গায়, যাতে লজিক একবারই ঠিক করলে চলে void writeRelay(int pin, bool onState) { if (RELAY_ACTIVE_LOW) digitalWrite(pin, onState ? LOW : HIGH); else digitalWrite(pin, onState ? HIGH : LOW); } HardwareSerial sim900(2); // UART2 WebServer server(80); // built-in web dashboard, replaces Blynk // WiFi রিকানেক্ট রিট্রাই ইন্টারভাল const unsigned long WIFI_RETRY_INTERVAL = 10UL * 1000UL; // ১০ সেকেন্ড পর পর রিট্রাই // পিরিয়ডিক ব্যালেন্স/SMS-count চেক ইন্টারভাল (মিলিসেকেন্ড) const unsigned long BALANCE_CHECK_INTERVAL = 3UL * 24UL * 60UL * 60UL * 1000UL; // প্রতি ৩ দিন // ---------------- STATE VARIABLES ---------------- bool acStatus = true; bool lastAcStatus = true; bool wifiConnected = false; bool lastWifiConnected = false; bool load1State = false; bool load2State = false; unsigned long lastBalanceCheck = 0; String sim900Buffer = ""; // ================================================== // শুধু হার্ডকোডেড SSID/PASSWORD দিয়ে WiFi কানেক্ট করে। কোনো AP config portal খুলবে না। void setupWifi() { WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.print("Connecting to WiFi: "); Serial.println(WIFI_SSID); unsigned long startAttempt = millis(); const unsigned long connectTimeout = 20000; // প্রথমবার ২০ সেকেন্ড চেষ্টা করবে while (WiFi.status() != WL_CONNECTED && millis() - startAttempt < connectTimeout) { delay(500); Serial.print("."); } Serial.println(); wifiConnected = (WiFi.status() == WL_CONNECTED); if (wifiConnected) { Serial.print("WiFi Connected. IP: "); Serial.println(WiFi.localIP()); } else { Serial.println("WiFi connect failed, will keep retrying in background (loop)."); } } // ================================================== // -------------- WEB DASHBOARD (replaces Blynk) -------------- String htmlPage() { String html = ""; html += "ESP32 Load Control"; html += "

ESP32 Load Control

"; html += "
"; html += "
AC Power: " + String(acStatus ? "OK" : "NOT PRESENT") + "
"; html += "
WiFi: " + String(wifiConnected ? "Connected" : "Disconnected") + "
"; html += "
"; html += "
"; html += "
Load 1: " + String(load1State ? "ON" : "OFF") + "
"; html += ""; html += ""; html += "
"; html += "
"; html += "
Load 2: " + String(load2State ? "ON" : "OFF") + "
"; html += ""; html += ""; html += "
"; html += ""; return html; } void handleRoot() { server.send(200, "text/html", htmlPage()); } void handleLoad1On() { setLoad1(true, "web"); server.sendHeader("Location", "/"); server.send(303); } void handleLoad1Off() { setLoad1(false, "web"); server.sendHeader("Location", "/"); server.send(303); } void handleLoad2On() { setLoad2(true, "web"); server.sendHeader("Location", "/"); server.send(303); } void handleLoad2Off() { setLoad2(false, "web"); server.sendHeader("Location", "/"); server.send(303); } // simple JSON status endpoint, handy if you ever want to poll it (e.g. from a script) void handleStatusJson() { String json = "{"; json += "\"ac\":" + String(acStatus ? "true" : "false") + ","; json += "\"wifi\":" + String(wifiConnected ? "true" : "false") + ","; json += "\"load1\":" + String(load1State ? "true" : "false") + ","; json += "\"load2\":" + String(load2State ? "true" : "false"); json += "}"; server.send(200, "application/json", json); } void setupWebServer() { server.on("/", handleRoot); server.on("/load1/on", handleLoad1On); server.on("/load1/off", handleLoad1Off); server.on("/load2/on", handleLoad2On); server.on("/load2/off", handleLoad2Off); server.on("/status", handleStatusJson); server.begin(); } // ================================================== void setup() { Serial.begin(115200); pinMode(AC_DETECT_PIN, INPUT); // external 10k pull-down already fitted pinMode(LOAD_RELAY1_PIN, OUTPUT); pinMode(LOAD_RELAY2_PIN, OUTPUT); pinMode(STATUS_LED_PIN, OUTPUT); writeRelay(LOAD_RELAY1_PIN, false); // Load 1 OFF at boot writeRelay(LOAD_RELAY2_PIN, false); // Load 2 OFF at boot sim900.begin(9600, SERIAL_8N1, SIM900_RX_PIN, SIM900_TX_PIN); delay(3000); initSim900(); setupWifi(); setupWebServer(); if (wifiConnected) { Serial.print("Web dashboard ready at: http://"); Serial.println(WiFi.localIP()); } acStatus = digitalRead(AC_DETECT_PIN); lastAcStatus = acStatus; lastWifiConnected = wifiConnected; notifyAdmins("System started. AC: " + String(acStatus ? "OK" : "NOT PRESENT") + " | WiFi: " + String(wifiConnected ? "Connected" : "Not connected")); } // ================================================== void loop() { server.handleClient(); checkAcStatus(); checkWifiStatus(); checkSim900Events(); handlePeriodicBalanceCheck(); static unsigned long lastReconnectAttempt = 0; if (WiFi.status() != WL_CONNECTED && millis() - lastReconnectAttempt > WIFI_RETRY_INTERVAL) { lastReconnectAttempt = millis(); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); // হার্ডকোডেড ক্রেডেনশিয়াল দিয়ে বারবার রিট্রাই করবে } } // ================= NOTIFY HELPER ================= void notifyAdmins(String message) { sendSMS(ADMIN_NUMBER_1, message); sendSMS(ADMIN_NUMBER_2, message); } // ================= WIFI STATUS ================= void checkWifiStatus() { wifiConnected = (WiFi.status() == WL_CONNECTED); if (wifiConnected != lastWifiConnected) { lastWifiConnected = wifiConnected; notifyAdmins(wifiConnected ? "WiFi Connected" : "WiFi Disconnected"); } } // ================= AC DETECTION ================= // NOTE: AC lost/restored SMS notification has been removed as requested. // Status is still tracked here (used by the web dashboard + SMS "STATUS" command). void checkAcStatus() { acStatus = digitalRead(AC_DETECT_PIN); if (acStatus != lastAcStatus) { lastAcStatus = acStatus; // (SMS notification intentionally removed here) } } // ================= LOAD CONTROL (shared by web, call, SMS) ================= void setLoad1(bool state, String source) { load1State = state; writeRelay(LOAD_RELAY1_PIN, load1State); notifyAdmins(String("Load 1 turned ") + (load1State ? "ON" : "OFF") + " (via " + source + ")"); } void setLoad2(bool state, String source) { load2State = state; writeRelay(LOAD_RELAY2_PIN, load2State); notifyAdmins(String("Load 2 turned ") + (load2State ? "ON" : "OFF") + " (via " + source + ")"); } void toggleLoad1FromCall() { setLoad1(!load1State, "call"); } void toggleLoad2FromCall() { setLoad2(!load2State, "call"); } // ================= SIM900A CORE ================= void initSim900() { sendAtCommand("AT", 1000); sendAtCommand("ATE0", 1000); // echo off sendAtCommand("AT+CMGF=1", 1000); // text mode SMS sendAtCommand("AT+CNMI=2,2,0,0,0", 1000); // deliver SMS directly to serial sendAtCommand("AT+CLIP=1", 1000); // caller ID on incoming call } String sendAtCommand(const char* cmd, unsigned long waitMs) { sim900.println(cmd); unsigned long start = millis(); String response = ""; while (millis() - start < waitMs) { while (sim900.available()) response += (char)sim900.read(); } return response; } void sendSMS(const char* number, String message) { if (strlen(number) == 0) return; sim900.println("AT+CMGF=1"); delay(200); sim900.print("AT+CMGS=\""); sim900.print(number); sim900.println("\""); delay(300); sim900.print(message); delay(100); sim900.write(26); // Ctrl+Z to send delay(3000); } void sendSMS(String number, String message) { sendSMS(number.c_str(), message); } // Extracts the quoted phone number from a +CLIP: or +CMT: line String extractPhoneNumber(String line) { int firstQuote = line.indexOf('"'); int secondQuote = line.indexOf('"', firstQuote + 1); if (firstQuote < 0 || secondQuote < 0) return ""; return line.substring(firstQuote + 1, secondQuote); } // Handles both incoming calls (via +CLIP caller ID) and incoming SMS (+CMT) on one // shared serial stream so bytes are never missed between the two parsers. void checkSim900Events() { static bool expectingSmsBody = false; static String smsSender = ""; while (sim900.available()) { char c = sim900.read(); sim900Buffer += c; if (c == '\n') { String line = sim900Buffer; line.trim(); if (expectingSmsBody) { processSmsCommand(smsSender, line); expectingSmsBody = false; } else if (line.indexOf("+CLIP:") >= 0) { String caller = extractPhoneNumber(line); handleIncomingCall(caller); } else if (line.indexOf("+CMT:") >= 0) { smsSender = extractPhoneNumber(line); expectingSmsBody = true; // message body arrives on the next line } sim900Buffer = ""; } if (sim900Buffer.length() > 200) sim900Buffer = ""; // overflow guard } } // Caller ID decides which relay to toggle. Call is rejected right away (no answer), // so it costs the caller nothing beyond a brief ring. void handleIncomingCall(String caller) { sendAtCommand("ATH", 800); // reject / hang up immediately if (caller.length() == 0) return; if (caller == String(ADMIN_NUMBER_1)) { toggleLoad1FromCall(); } else if (caller == String(ADMIN_NUMBER_2)) { toggleLoad2FromCall(); } } // Only ADMIN_NUMBER_1 / ADMIN_NUMBER_2 SMS commands are accepted, everything else ignored. // Supported: LOAD1 ON/OFF, LOAD2 ON/OFF, STATUS void processSmsCommand(String sender, String body) { if (sender != String(ADMIN_NUMBER_1) && sender != String(ADMIN_NUMBER_2)) return; body.trim(); body.toUpperCase(); if (body == "LOAD1 ON") setLoad1(true, "SMS"); else if (body == "LOAD1 OFF") setLoad1(false, "SMS"); else if (body == "LOAD2 ON") setLoad2(true, "SMS"); else if (body == "LOAD2 OFF") setLoad2(false, "SMS"); else if (body == "STATUS") { String msg = "AC: " + String(acStatus ? "OK" : "LOST"); msg += " | WiFi: " + String(wifiConnected ? "Connected" : "Disconnected"); msg += " | Load1: " + String(load1State ? "ON" : "OFF"); msg += " | Load2: " + String(load2State ? "ON" : "OFF"); sendSMS(sender, msg); } } // ================= BALANCE / SMS COUNT CHECK ================= void handlePeriodicBalanceCheck() { if (millis() - lastBalanceCheck >= BALANCE_CHECK_INTERVAL) { lastBalanceCheck = millis(); checkBalanceAndSmsCount(); } } void checkBalanceAndSmsCount() { // USSD ব্যালেন্স চেক কোড আপনার অপারেটর অনুযায়ী বদলান // (Grameenphone: *566#, Robi: *222#, Banglalink: *121#) String resp = sendAtCommand("AT+CUSD=1,\"*566#\",15", 8000); notifyAdmins("Balance check response: " + resp.substring(0, 140)); String smsCountResp = sendAtCommand("AT+CPMS?", 2000); notifyAdmins("SMS memory status: " + smsCountResp.substring(0, 140)); }