Skip to content

Commit

Permalink
Merge pull request #231 from airgradienthq/develop
Browse files Browse the repository at this point in the history
Add WiFi feature
  • Loading branch information
pnt325 authored Sep 2, 2024
2 parents 29491e4 + a1861be commit 682378a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
4 changes: 1 addition & 3 deletions examples/DiyProIndoorV4_2/DiyProIndoorV4_2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions examples/OneOpenAir/OneOpenAir.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
30 changes: 30 additions & 0 deletions src/AgWiFiConnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}
4 changes: 4 additions & 0 deletions src/AgWiFiConnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */

0 comments on commit 682378a

Please sign in to comment.