diff --git a/examples/DiyProIndoorV4_2/DiyProIndoorV4_2.ino b/examples/DiyProIndoorV4_2/DiyProIndoorV4_2.ino index eec919e4..305d4a2b 100644 --- a/examples/DiyProIndoorV4_2/DiyProIndoorV4_2.ino +++ b/examples/DiyProIndoorV4_2/DiyProIndoorV4_2.ino @@ -327,9 +327,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 48457336..ee7314e7 100644 --- a/examples/OneOpenAir/OneOpenAir.ino +++ b/examples/OneOpenAir/OneOpenAir.ino @@ -423,8 +423,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_ */