From 2f39df176c9143a92f9a0e820940a47a2ddee6f1 Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Tue, 14 Nov 2023 16:55:18 +0100 Subject: [PATCH] Allow QR setup and webUI config at the same time --- src/lib/config.cpp | 44 ++++++++++++++++++++++++++--------------- src/lib/led_handler.cpp | 3 +++ src/lib/led_handler.hpp | 1 + 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/lib/config.cpp b/src/lib/config.cpp index 2c06590..1cd83bf 100644 --- a/src/lib/config.cpp +++ b/src/lib/config.cpp @@ -49,7 +49,7 @@ void setup() void tryImprovSetup() { - WiFi.onEvent(WiFiEvent); + //WiFi.onEvent(WiFiEvent); if (!preferences.getBool("wifiConfigured", false)) { @@ -65,8 +65,7 @@ void tryImprovSetup() std::lock_guard lockMcp(mcpMutex); buttonPress = (mcp.digitalRead(2) == LOW); } - // Hold second button to start QR code wifi config - if (buttonPress) + { WiFiManager wm; @@ -77,19 +76,22 @@ void tryImprovSetup() String softAP_password = base64::encode(String(mac[2], 16) + String(mac[4], 16) + String(mac[5], 16) + String(mac[1], 16)).substring(2, 10); wm.setConfigPortalTimeout(preferences.getUInt("wpTimeout", 600)); - wm.setWiFiAutoReconnect(true); + wm.setWiFiAutoReconnect(false); + wm.setDebugOutput(false); + wm.setConfigPortalBlocking(false); + wm.setAPCallback([&](WiFiManager *wifiManager) { - Serial.printf("Entered config mode:ip=%s, ssid='%s', pass='%s'\n", - WiFi.softAPIP().toString().c_str(), - wifiManager->getConfigPortalSSID().c_str(), - softAP_password.c_str()); - delay(6000); + // Serial.printf("Entered config mode:ip=%s, ssid='%s', pass='%s'\n", + // WiFi.softAPIP().toString().c_str(), + // wifiManager->getConfigPortalSSID().c_str(), + // softAP_password.c_str()); + //delay(6000); const String qrText = "qrWIFI:S:" + wifiManager->getConfigPortalSSID() + ";T:WPA;P:" + softAP_password.c_str() + ";;"; const String explainText = "*SSID: *\r\n" + wifiManager->getConfigPortalSSID() + "\r\n\r\n*Password:*\r\n" + softAP_password; - std::array epdContent = {"Welcome!", "", "To setup\r\nscan QR or\r\nconnect\r\nmanually", "", explainText, "", qrText}; + std::array epdContent = {"Welcome!", "Bienvenidos!", "To setup\r\nscan QR or\r\nconnect\r\nmanually", "Para\r\nconfigurar\r\nescanear QR\r\no conectar\r\nmanualmente", explainText, " ", qrText}; setEpdContent(epdContent); }); wm.setSaveConfigCallback([]() @@ -101,13 +103,12 @@ void tryImprovSetup() ESP.restart(); }); bool ac = wm.autoConnect(softAP_SSID.c_str(), softAP_password.c_str()); - } - else - { - waitUntilNoneBusy(); - std::array epdContent = {"Welcome!", "", "Use\r\nweb-interface\r\nto configure", "", "Or restart\r\nwhile\r\nholding\r\n2nd button\r\r\nto start\r\n QR-config", "", ""}; - setEpdContent(epdContent); + + //waitUntilNoneBusy(); + //std::array epdContent = {"Welcome!", "Bienvenidos!", "Use\r\nweb-interface\r\nto configure", "Use\r\nla interfaz web\r\npara configurar", "Or restart\r\nwhile\r\nholding\r\n2nd button\r\r\nto start\r\n QR-config", "O reinicie\r\nmientras\r\n mantiene presionado\r\nel segundo botón\r\r\npara iniciar\r\nQR-config", ""}; + //setEpdContent(epdContent); esp_task_wdt_init(30, false); + uint count = 0; while (WiFi.status() != WL_CONNECTED) { if (Serial.available() > 0) @@ -123,9 +124,17 @@ void tryImprovSetup() x_position = 0; } } + count++; + + if (count > 2000000) { + queueLedEffect(LED_EFFECT_HEARTBEAT); + count = 0; + } + esp_task_wdt_reset(); } esp_task_wdt_deinit(); esp_task_wdt_reset(); + } setFgColor(preferences.getUInt("fgColor", DEFAULT_FG_COLOR)); setBgColor(preferences.getUInt("bgColor", DEFAULT_BG_COLOR)); @@ -356,6 +365,9 @@ bool onImprovCommandCallback(improv::ImprovCommand cmd) improv_set_state(improv::STATE_PROVISIONED); std::vector data = improv::build_rpc_response(improv::WIFI_SETTINGS, getLocalUrl(), false); improv_send_response(data); + + delay(2500); + ESP.restart(); setupWebserver(); } else diff --git a/src/lib/led_handler.cpp b/src/lib/led_handler.cpp index 8a525b2..4a71f94 100644 --- a/src/lib/led_handler.cpp +++ b/src/lib/led_handler.cpp @@ -33,6 +33,9 @@ void ledTask(void *parameter) case LED_FLASH_ERROR: blinkDelayColor(250, 3, 255, 0, 0); break; + case LED_EFFECT_HEARTBEAT: + blinkDelayColor(150, 2, 0, 0, 255); + break; case LED_EFFECT_WIFI_CONNECT_SUCCESS: case LED_FLASH_SUCCESS: blinkDelayColor(150, 3, 0, 255, 0); diff --git a/src/lib/led_handler.hpp b/src/lib/led_handler.hpp index acca0bf..c28938f 100644 --- a/src/lib/led_handler.hpp +++ b/src/lib/led_handler.hpp @@ -17,6 +17,7 @@ const int LED_FLASH_UPDATE = 2; const int LED_FLASH_BLOCK_NOTIFY = 3; const int LED_EFFECT_START_TIMER = 4; const int LED_EFFECT_PAUSE_TIMER = 5; +const int LED_EFFECT_HEARTBEAT = 6; const int LED_EFFECT_WIFI_WAIT_FOR_CONFIG = 100; const int LED_EFFECT_WIFI_CONNECTING = 101; const int LED_EFFECT_WIFI_CONNECT_ERROR = 102;