2024-04-04 18:35:15 +07:00
|
|
|
#ifdef ESP32
|
|
|
|
|
2024-04-03 11:40:46 +07:00
|
|
|
#include "AgWiFiConnector.h"
|
2024-04-04 18:35:15 +07:00
|
|
|
#include "Libraries/WiFiManager/WiFiManager.h"
|
2024-04-03 11:40:46 +07:00
|
|
|
|
2024-04-03 21:26:04 +07:00
|
|
|
#define WIFI_CONNECT_COUNTDOWN_MAX 180
|
|
|
|
#define WIFI_HOTSPOT_PASSWORD_DEFAULT "cleanair"
|
|
|
|
|
|
|
|
#define WIFI() ((WiFiManager *)(this->wifi))
|
|
|
|
|
2024-04-04 18:35:15 +07:00
|
|
|
/**
|
|
|
|
* @brief Construct a new Ag Wi Fi Connector:: Ag Wi Fi Connector object
|
|
|
|
*
|
|
|
|
* @param disp AgOledDisplay
|
|
|
|
* @param log Stream
|
|
|
|
* @param sm AgStateMachine
|
|
|
|
*/
|
2024-04-04 10:36:59 +07:00
|
|
|
AgWiFiConnector::AgWiFiConnector(AgOledDisplay &disp, Stream &log,
|
|
|
|
AgStateMachine &sm)
|
|
|
|
: PrintLog(log, "AgWiFiConnector"), disp(disp), sm(sm) {}
|
2024-04-03 11:40:46 +07:00
|
|
|
|
2024-04-03 21:26:04 +07:00
|
|
|
AgWiFiConnector::~AgWiFiConnector() {}
|
|
|
|
|
2024-04-04 18:35:15 +07:00
|
|
|
/**
|
|
|
|
* @brief Set reference AirGradient instance
|
|
|
|
*
|
|
|
|
* @param ag Point to AirGradient instance
|
|
|
|
*/
|
2024-04-04 10:36:59 +07:00
|
|
|
void AgWiFiConnector::setAirGradient(AirGradient *ag) { this->ag = ag; }
|
2024-04-03 21:26:04 +07:00
|
|
|
|
2024-04-04 18:35:15 +07:00
|
|
|
/**
|
|
|
|
* @brief Connection to WIFI AP process. Just call one times
|
|
|
|
*
|
|
|
|
* @return true Success
|
|
|
|
* @return false Failure
|
|
|
|
*/
|
2024-04-04 10:36:59 +07:00
|
|
|
bool AgWiFiConnector::connect(void) {
|
2024-04-03 21:26:04 +07:00
|
|
|
if (wifi == NULL) {
|
|
|
|
wifi = new WiFiManager();
|
|
|
|
if (wifi == NULL) {
|
|
|
|
logError("Create 'WiFiManger' failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WIFI()->setConfigPortalBlocking(false);
|
2024-04-04 10:36:59 +07:00
|
|
|
WIFI()->setTimeout(WIFI_CONNECT_COUNTDOWN_MAX);
|
2024-04-03 21:26:04 +07:00
|
|
|
|
|
|
|
WIFI()->setAPCallback([this](WiFiManager *obj) { _wifiApCallback(); });
|
|
|
|
WIFI()->setSaveConfigCallback([this]() { _wifiSaveConfig(); });
|
|
|
|
WIFI()->setSaveParamsCallback([this]() { _wifiSaveParamCallback(); });
|
|
|
|
|
2024-04-04 10:36:59 +07:00
|
|
|
if (ag->isOneIndoor()) {
|
2024-04-03 21:26:04 +07:00
|
|
|
disp.setText("Connecting to", "WiFi", "...");
|
|
|
|
} else {
|
|
|
|
logInfo("Connecting to WiFi...");
|
|
|
|
}
|
|
|
|
|
2024-04-04 18:35:15 +07:00
|
|
|
ssid = "airgradient-" + ag->deviceId();
|
2024-04-03 21:26:04 +07:00
|
|
|
WIFI()->autoConnect(ssid.c_str(), WIFI_HOTSPOT_PASSWORD_DEFAULT);
|
2024-04-04 10:36:59 +07:00
|
|
|
|
|
|
|
// Task handle WiFi connection.
|
2024-04-03 21:26:04 +07:00
|
|
|
xTaskCreate(
|
|
|
|
[](void *obj) {
|
|
|
|
AgWiFiConnector *connector = (AgWiFiConnector *)obj;
|
|
|
|
while (connector->_wifiConfigPortalActive()) {
|
|
|
|
connector->_wifiProcess();
|
|
|
|
}
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
},
|
|
|
|
"wifi_cfg", 4096, this, 10, NULL);
|
|
|
|
|
|
|
|
/** Wait for WiFi connect and show LED, display status */
|
|
|
|
uint32_t dispPeriod = millis();
|
|
|
|
uint32_t ledPeriod = millis();
|
|
|
|
bool clientConnectChanged = false;
|
|
|
|
|
|
|
|
AgStateMachineState stateOld = sm.getDisplayState();
|
|
|
|
while (WIFI()->getConfigPortalActive()) {
|
2024-04-04 10:36:59 +07:00
|
|
|
/** LED animatoin and display update content */
|
2024-04-03 21:26:04 +07:00
|
|
|
if (WiFi.isConnected() == false) {
|
|
|
|
/** Display countdown */
|
2024-04-04 10:36:59 +07:00
|
|
|
uint32_t ms;
|
|
|
|
if (ag->isOneIndoor()) {
|
|
|
|
ms = (uint32_t)(millis() - dispPeriod);
|
2024-04-03 21:26:04 +07:00
|
|
|
if (ms >= 1000) {
|
|
|
|
dispPeriod = millis();
|
|
|
|
sm.displayHandle();
|
|
|
|
} else {
|
|
|
|
if (stateOld != sm.getDisplayState()) {
|
|
|
|
stateOld = sm.getDisplayState();
|
|
|
|
sm.displayHandle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** LED animations */
|
|
|
|
ms = (uint32_t)(millis() - ledPeriod);
|
|
|
|
if (ms >= 100) {
|
|
|
|
ledPeriod = millis();
|
|
|
|
sm.ledHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Check for client connect to change led color */
|
|
|
|
bool clientConnected = wifiClientConnected();
|
|
|
|
if (clientConnected != clientConnectChanged) {
|
|
|
|
clientConnectChanged = clientConnected;
|
|
|
|
if (clientConnectChanged) {
|
|
|
|
sm.ledHandle(AgStateMachineWiFiManagerPortalActive);
|
|
|
|
} else {
|
|
|
|
sm.ledAnimationInit();
|
|
|
|
sm.ledHandle(AgStateMachineWiFiManagerMode);
|
2024-04-04 10:36:59 +07:00
|
|
|
if (ag->isOneIndoor()) {
|
2024-04-03 21:26:04 +07:00
|
|
|
sm.displayHandle(AgStateMachineWiFiManagerMode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-04 10:36:59 +07:00
|
|
|
|
|
|
|
delay(1); // avoid watchdog timer reset.
|
2024-04-03 21:26:04 +07:00
|
|
|
}
|
|
|
|
/** Show display wifi connect result failed */
|
|
|
|
if (WiFi.isConnected() == false) {
|
|
|
|
sm.ledHandle(AgStateMachineWiFiManagerConnectFailed);
|
2024-04-04 10:36:59 +07:00
|
|
|
if (ag->isOneIndoor()) {
|
2024-04-03 21:26:04 +07:00
|
|
|
sm.displayHandle(AgStateMachineWiFiManagerConnectFailed);
|
|
|
|
}
|
|
|
|
delay(6000);
|
|
|
|
} else {
|
2024-04-04 10:36:59 +07:00
|
|
|
hasConfig = true;
|
2024-04-03 21:26:04 +07:00
|
|
|
}
|
|
|
|
|
2024-04-04 10:36:59 +07:00
|
|
|
return true;
|
2024-04-03 21:26:04 +07:00
|
|
|
}
|
|
|
|
|
2024-04-04 18:35:15 +07:00
|
|
|
/**
|
|
|
|
* @brief Disconnect to current connected WiFi AP
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void AgWiFiConnector::disconnect(void) {
|
|
|
|
if (WiFi.isConnected()) {
|
|
|
|
logInfo("Disconnect");
|
|
|
|
WiFi.disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Has wifi STA connected to WIFI softAP (this device)
|
|
|
|
*
|
|
|
|
* @return true Connected
|
|
|
|
* @return false Not connected
|
|
|
|
*/
|
2024-04-03 21:26:04 +07:00
|
|
|
bool AgWiFiConnector::wifiClientConnected(void) {
|
|
|
|
return WiFi.softAPgetStationNum() ? true : false;
|
|
|
|
}
|
|
|
|
|
2024-04-04 18:35:15 +07:00
|
|
|
/**
|
|
|
|
* @brief Handle WiFiManage softAP setup completed callback
|
|
|
|
*
|
|
|
|
*/
|
2024-04-03 21:26:04 +07:00
|
|
|
void AgWiFiConnector::_wifiApCallback(void) {
|
|
|
|
sm.displayWiFiConnectCountDown(WIFI_CONNECT_COUNTDOWN_MAX);
|
|
|
|
sm.setDisplayState(AgStateMachineWiFiManagerMode);
|
2024-04-04 10:36:59 +07:00
|
|
|
sm.ledAnimationInit();
|
|
|
|
sm.ledHandle(AgStateMachineWiFiManagerMode);
|
2024-04-03 21:26:04 +07:00
|
|
|
}
|
|
|
|
|
2024-04-04 18:35:15 +07:00
|
|
|
/**
|
|
|
|
* @brief Handle WiFiManager save configuration callback
|
|
|
|
*
|
|
|
|
*/
|
2024-04-03 21:26:04 +07:00
|
|
|
void AgWiFiConnector::_wifiSaveConfig(void) {
|
|
|
|
sm.setDisplayState(AgStateMachineWiFiManagerStaConnected);
|
2024-04-04 10:36:59 +07:00
|
|
|
sm.ledHandle(AgStateMachineWiFiManagerStaConnected);
|
2024-04-03 21:26:04 +07:00
|
|
|
}
|
|
|
|
|
2024-04-04 18:35:15 +07:00
|
|
|
/**
|
|
|
|
* @brief Handle WiFiManager save parameter callback
|
|
|
|
*
|
|
|
|
*/
|
2024-04-03 21:26:04 +07:00
|
|
|
void AgWiFiConnector::_wifiSaveParamCallback(void) {
|
|
|
|
sm.ledAnimationInit();
|
2024-04-04 10:36:59 +07:00
|
|
|
sm.ledHandle(AgStateMachineWiFiManagerStaConnecting);
|
2024-04-03 21:26:04 +07:00
|
|
|
sm.setDisplayState(AgStateMachineWiFiManagerStaConnecting);
|
2024-04-03 11:40:46 +07:00
|
|
|
}
|
2024-04-03 21:26:04 +07:00
|
|
|
|
2024-04-04 18:35:15 +07:00
|
|
|
/**
|
|
|
|
* @brief Check that WiFiManager Configure portal active
|
|
|
|
*
|
|
|
|
* @return true Active
|
|
|
|
* @return false Not-Active
|
|
|
|
*/
|
2024-04-03 21:26:04 +07:00
|
|
|
bool AgWiFiConnector::_wifiConfigPortalActive(void) {
|
|
|
|
return WIFI()->getConfigPortalActive();
|
|
|
|
}
|
|
|
|
|
2024-04-04 18:35:15 +07:00
|
|
|
/**
|
|
|
|
* @brief Process WiFiManager connection
|
|
|
|
*
|
|
|
|
*/
|
2024-04-03 21:26:04 +07:00
|
|
|
void AgWiFiConnector::_wifiProcess() { WIFI()->process(); }
|
2024-04-04 10:36:59 +07:00
|
|
|
|
2024-04-04 18:35:15 +07:00
|
|
|
/**
|
|
|
|
* @brief Handle and reconnect WiFi
|
|
|
|
*
|
|
|
|
*/
|
2024-04-04 10:36:59 +07:00
|
|
|
void AgWiFiConnector::handle(void) {
|
|
|
|
// Ignore if WiFi is not configured
|
|
|
|
if (hasConfig == false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WiFi.isConnected()) {
|
|
|
|
lastRetry = millis();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Retry connect WiFi each 10sec */
|
|
|
|
uint32_t ms = (uint32_t)(millis() - lastRetry);
|
|
|
|
if (ms >= 10000) {
|
|
|
|
lastRetry = millis();
|
|
|
|
WiFi.reconnect();
|
|
|
|
|
|
|
|
// Serial.printf("Re-Connect WiFi\r\n");
|
|
|
|
logInfo("Re-Connect WiFi");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 18:35:15 +07:00
|
|
|
/**
|
|
|
|
* @brief Is WiFi connected
|
|
|
|
*
|
|
|
|
* @return true Connected
|
|
|
|
* @return false Disconnected
|
|
|
|
*/
|
2024-04-04 10:36:59 +07:00
|
|
|
bool AgWiFiConnector::isConnected(void) { return WiFi.isConnected(); }
|
2024-04-04 18:35:15 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Reset WiFi configuretion and connection, disconnect wifi before call
|
|
|
|
* this method
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void AgWiFiConnector::reset(void) { WIFI()->resetSettings(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get wifi RSSI
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
int AgWiFiConnector::RSSI(void) { return WiFi.RSSI(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get WIFI IP as string format ex: 192.168.1.1
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
String AgWiFiConnector::localIpStr(void) { return WiFi.localIP().toString(); }
|
|
|
|
|
|
|
|
#endif
|