2018-02-26 00:11:31 +00:00
|
|
|
#include <WiFiSettingsService.h>
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
|
2020-05-29 19:18:43 +00:00
|
|
|
_httpEndpoint(WiFiSettings::read, WiFiSettings::update, this, server, WIFI_SETTINGS_SERVICE_PATH, securityManager),
|
|
|
|
_fsPersistence(WiFiSettings::read, WiFiSettings::update, this, fs, WIFI_SETTINGS_FILE),
|
2020-05-21 22:41:29 +00:00
|
|
|
_lastConnectionAttempt(0) {
|
2019-12-01 01:24:15 +00:00
|
|
|
// We want the device to come up in opmode=0 (WIFI_OFF), when erasing the flash this is not the default.
|
|
|
|
// If needed, we save opmode=0 before disabling persistence so the device boots with WiFi disabled in the future.
|
|
|
|
if (WiFi.getMode() != WIFI_OFF) {
|
2019-12-03 23:16:06 +00:00
|
|
|
WiFi.mode(WIFI_OFF);
|
2019-12-01 01:24:15 +00:00
|
|
|
}
|
2019-12-03 23:16:06 +00:00
|
|
|
|
2019-09-28 20:29:46 +00:00
|
|
|
// Disable WiFi config persistance and auto reconnect
|
2019-07-14 21:13:26 +00:00
|
|
|
WiFi.persistent(false);
|
|
|
|
WiFi.setAutoReconnect(false);
|
2019-12-24 11:19:19 +00:00
|
|
|
#ifdef ESP32
|
2019-07-14 21:13:26 +00:00
|
|
|
// Init the wifi driver on ESP32
|
|
|
|
WiFi.mode(WIFI_MODE_MAX);
|
|
|
|
WiFi.mode(WIFI_MODE_NULL);
|
2019-12-03 23:16:06 +00:00
|
|
|
WiFi.onEvent(
|
|
|
|
std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
|
|
|
|
WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
|
2020-02-01 20:21:18 +00:00
|
|
|
WiFi.onEvent(std::bind(&WiFiSettingsService::onStationModeStop, this, std::placeholders::_1, std::placeholders::_2),
|
|
|
|
WiFiEvent_t::SYSTEM_EVENT_STA_STOP);
|
2019-12-24 11:19:19 +00:00
|
|
|
#elif defined(ESP8266)
|
|
|
|
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(
|
|
|
|
std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
|
2019-06-04 23:05:16 +00:00
|
|
|
#endif
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2020-05-21 07:42:21 +00:00
|
|
|
addUpdateHandler([&](const String& originId) { reconfigureWiFiConnection(); }, false);
|
2019-12-03 23:16:06 +00:00
|
|
|
}
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2019-09-30 20:28:24 +00:00
|
|
|
void WiFiSettingsService::begin() {
|
2020-05-14 22:23:45 +00:00
|
|
|
_fsPersistence.readFromFS();
|
2018-02-26 00:11:31 +00:00
|
|
|
reconfigureWiFiConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiSettingsService::reconfigureWiFiConnection() {
|
2019-12-03 23:16:06 +00:00
|
|
|
// reset last connection attempt to force loop to reconnect immediately
|
|
|
|
_lastConnectionAttempt = 0;
|
2020-02-01 20:21:18 +00:00
|
|
|
|
|
|
|
// disconnect and de-configure wifi
|
|
|
|
#ifdef ESP32
|
|
|
|
if (WiFi.disconnect(true)) {
|
|
|
|
_stopping = true;
|
|
|
|
}
|
|
|
|
#elif defined(ESP8266)
|
|
|
|
WiFi.disconnect(true);
|
|
|
|
#endif
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 20:13:55 +00:00
|
|
|
void WiFiSettingsService::loop() {
|
|
|
|
unsigned long currentMillis = millis();
|
|
|
|
if (!_lastConnectionAttempt || (unsigned long)(currentMillis - _lastConnectionAttempt) >= WIFI_RECONNECTION_DELAY) {
|
|
|
|
_lastConnectionAttempt = currentMillis;
|
2019-12-03 23:16:06 +00:00
|
|
|
manageSTA();
|
2019-06-04 20:13:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiSettingsService::manageSTA() {
|
|
|
|
// Abort if already connected, or if we have no SSID
|
2020-05-14 22:23:45 +00:00
|
|
|
if (WiFi.isConnected() || _state.ssid.length() == 0) {
|
2019-06-04 20:13:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Connect or reconnect as required
|
|
|
|
if ((WiFi.getMode() & WIFI_STA) == 0) {
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.println(F("Connecting to WiFi."));
|
2020-05-14 22:23:45 +00:00
|
|
|
if (_state.staticIPConfig) {
|
2019-06-04 20:13:55 +00:00
|
|
|
// configure for static IP
|
2020-05-14 22:23:45 +00:00
|
|
|
WiFi.config(_state.localIP, _state.gatewayIP, _state.subnetMask, _state.dnsIP1, _state.dnsIP2);
|
2019-06-04 20:13:55 +00:00
|
|
|
} else {
|
|
|
|
// configure for DHCP
|
2019-12-24 11:19:19 +00:00
|
|
|
#ifdef ESP32
|
2019-06-04 20:13:55 +00:00
|
|
|
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
|
2020-05-14 22:23:45 +00:00
|
|
|
WiFi.setHostname(_state.hostname.c_str());
|
2019-12-24 11:19:19 +00:00
|
|
|
#elif defined(ESP8266)
|
|
|
|
WiFi.config(INADDR_ANY, INADDR_ANY, INADDR_ANY);
|
2020-05-14 22:23:45 +00:00
|
|
|
WiFi.hostname(_state.hostname);
|
2019-06-04 20:13:55 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
// attempt to connect to the network
|
2020-05-14 22:23:45 +00:00
|
|
|
WiFi.begin(_state.ssid.c_str(), _state.password.c_str());
|
2019-06-04 23:05:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
#ifdef ESP32
|
|
|
|
void WiFiSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
2019-06-04 23:05:16 +00:00
|
|
|
WiFi.disconnect(true);
|
|
|
|
}
|
2020-02-01 20:21:18 +00:00
|
|
|
void WiFiSettingsService::onStationModeStop(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|
|
|
if (_stopping) {
|
|
|
|
_lastConnectionAttempt = 0;
|
|
|
|
_stopping = false;
|
|
|
|
}
|
|
|
|
}
|
2019-12-24 11:19:19 +00:00
|
|
|
#elif defined(ESP8266)
|
|
|
|
void WiFiSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
|
2019-06-04 23:05:16 +00:00
|
|
|
WiFi.disconnect(true);
|
2019-06-04 20:13:55 +00:00
|
|
|
}
|
2019-06-04 23:05:16 +00:00
|
|
|
#endif
|