From 03e2afbf540917e67e2d1e68884e815c2c118f0f Mon Sep 17 00:00:00 2001 From: Phat Nguyen Date: Mon, 26 Aug 2024 15:47:49 +0700 Subject: [PATCH] WiFi Connect to default `airgradient` if WiFi connected is empty --- .../DiyProIndoorV4_2/DiyProIndoorV4_2.ino | 4 +-- examples/OneOpenAir/OneOpenAir.ino | 3 +- src/AgWiFiConnector.cpp | 30 +++++++++++++++++++ src/AgWiFiConnector.h | 4 +++ 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/examples/DiyProIndoorV4_2/DiyProIndoorV4_2.ino b/examples/DiyProIndoorV4_2/DiyProIndoorV4_2.ino index 20be418b..997616bc 100644 --- a/examples/DiyProIndoorV4_2/DiyProIndoorV4_2.ino +++ b/examples/DiyProIndoorV4_2/DiyProIndoorV4_2.ino @@ -328,9 +328,7 @@ static void factoryConfigReset(void) { // } /** Reset WIFI */ - // WiFi.enableSTA(true); // Incase offline mode - // WiFi.disconnect(true, true); - wifiConnector.reset(); + WiFi.disconnect(true, true); /** Reset local config */ configuration.reset(); diff --git a/examples/OneOpenAir/OneOpenAir.ino b/examples/OneOpenAir/OneOpenAir.ino index 978c345b..fc1c022a 100644 --- a/examples/OneOpenAir/OneOpenAir.ino +++ b/examples/OneOpenAir/OneOpenAir.ino @@ -424,8 +424,7 @@ static void factoryConfigReset(void) { } /** Reset WIFI */ - Serial.println("Set wifi connect to 'airgradient' as default"); - WiFi.begin("airgradient", "cleanair"); + WiFi.disconnect(true, true); /** Reset local config */ configuration.reset(); diff --git a/src/AgWiFiConnector.cpp b/src/AgWiFiConnector.cpp index a64ed83d..ddf47366 100644 --- a/src/AgWiFiConnector.cpp +++ b/src/AgWiFiConnector.cpp @@ -41,6 +41,28 @@ bool WifiConnector::connect(void) { } } + WiFi.begin(); + String wifiSSID = WIFI()->getWiFiSSID(true); + if (wifiSSID.isEmpty()) { + logInfo("Connected WiFi is empty, connect to default wifi \"" + + String(this->defaultSsid) + String("\"")); + + /** Set wifi connect */ + WiFi.begin(this->defaultSsid, this->defaultPassword); + + /** Wait for wifi connect to AP */ + int count = 0; + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + count++; + if (count >= 15) { + logError("Try connect to default wifi \"" + String(this->defaultSsid) + + String("\" failed")); + break; + } + } + } + WIFI()->setConfigPortalBlocking(false); WIFI()->setConnectTimeout(15); WIFI()->setTimeout(WIFI_CONNECT_COUNTDOWN_MAX); @@ -383,3 +405,11 @@ bool WifiConnector::hasConfigurated(void) { * @return false */ bool WifiConnector::isConfigurePorttalTimeout(void) { return connectorTimeout; } + +/** + * @brief Set wifi connect to default WiFi + * + */ +void WifiConnector::setDefault(void) { + WiFi.begin("airgradient", "cleanair"); +} diff --git a/src/AgWiFiConnector.h b/src/AgWiFiConnector.h index 10efba60..e22ea0fc 100644 --- a/src/AgWiFiConnector.h +++ b/src/AgWiFiConnector.h @@ -46,6 +46,10 @@ class WifiConnector : public PrintLog { String localIpStr(void); bool hasConfigurated(void); bool isConfigurePorttalTimeout(void); + + const char* defaultSsid = "airgradient"; + const char* defaultPassword = "cleanair"; + void setDefault(void); }; #endif /** _AG_WIFI_CONNECTOR_H_ */