From 9666c3ef44aa052e94b98bc79c23be1b6be2feca Mon Sep 17 00:00:00 2001 From: Ghassan Yusuf Date: Tue, 10 Jun 2025 16:46:34 +0300 Subject: [PATCH] Update wifi.ino --- wifi.ino | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/wifi.ino b/wifi.ino index 7a6f8ef..193886e 100644 --- a/wifi.ino +++ b/wifi.ino @@ -6,6 +6,8 @@ #include #include +#define PRODUCT_NAME "TAKEONE" + // OLED display setup (on I2C bus 0) #define OLED_SDA 18 #define OLED_SCL 19 @@ -26,24 +28,36 @@ char mqtt_port[6] = "8080"; char api_token[34] = "YOUR_API_TOKEN"; bool shouldSaveConfig = false; +// WiFi loss detection +unsigned long wifiLostTime = 0; +bool portalOpened = false; + +// Button reset +#define BOOT_BUTTON 0 // Usually GPIO 0 on ESP32 + void saveConfigCallback() { + shouldSaveConfig = true; display.clearDisplay(); display.setCursor(0, 0); display.println("Saving config..."); display.display(); + } void showStatus(const String& line1, const String& line2 = "", const String& line3 = "") { + display.clearDisplay(); display.setCursor(0, 0); display.println(line1); if (!line2.isEmpty()) display.println(line2); if (!line3.isEmpty()) display.println(line3); display.display(); + } void setup() { + Serial.begin(115200); // Start both I2C buses @@ -89,7 +103,7 @@ void setup() { wifiManager.addParameter(&custom_api_token); showStatus("Connecting WiFi..."); - if (!wifiManager.autoConnect("AutoConnectAP", "password")) { + if (!wifiManager.autoConnect(PRODUCT_NAME)) { showStatus("WiFi Connect", "Failed!", "Restarting..."); delay(3000); ESP.restart(); @@ -116,9 +130,48 @@ void setup() { // Show final info (truncate to fit OLED) String ipStr = WiFi.localIP().toString(); if (ipStr.length() > 15) ipStr = ipStr.substring(0, 15); - showStatus("WiFi OK: " + ipStr, "MQTT: " + String(mqtt_server), "Port: " + String(mqtt_port)); + showStatus("IPAD: " + ipStr, "MQTT: " + String(mqtt_server), "Port: " + String(mqtt_port)); + + // Button setup + pinMode(BOOT_BUTTON, INPUT_PULLUP); } void loop() { - // You can use I2C_Two for other sensors/devices here -} \ No newline at end of file + + // WiFi loss detection + if (WiFi.status() != WL_CONNECTED) { + if (wifiLostTime == 0) { + wifiLostTime = millis(); + } else if (millis() - wifiLostTime > 10000 && !portalOpened) { + showStatus("WiFi lost", "Opening portal..."); + WiFiManager wifiManager; + portalOpened = true; + wifiManager.startConfigPortal(PRODUCT_NAME); + portalOpened = false; + wifiLostTime = 0; + } + } else { + wifiLostTime = 0; + } + + // Button hold check + static unsigned long buttonPressedTime = 0; + static bool buttonActionTaken = false; + + if (digitalRead(BOOT_BUTTON) == LOW) { + if (buttonPressedTime == 0) { + buttonPressedTime = millis(); + } else if (millis() - buttonPressedTime > 5000 && !buttonActionTaken) { + showStatus("Erasing WiFi", "Opening portal..."); + WiFi.disconnect(true); + WiFiManager wifiManager; + wifiManager.resetSettings(); + wifiManager.startConfigPortal("AutoConnectAP", "12345678"); + buttonActionTaken = true; + } + } else { + buttonPressedTime = 0; + buttonActionTaken = false; + } + +}