diff --git a/data/config/wifiSettings.json b/data/config/wifiSettings.json index 4f0b28a..8ff0621 100644 --- a/data/config/wifiSettings.json +++ b/data/config/wifiSettings.json @@ -1,5 +1,5 @@ { - "ssid":"ssid", + "ssid":"", "password":"password", "hostname":"esp8266-react", "static_ip_config":false diff --git a/data/www/index.html b/data/www/index.html index 7abfc5e..0fef79f 100644 --- a/data/www/index.html +++ b/data/www/index.html @@ -1 +1 @@ -ESP8266 React
\ No newline at end of file +ESP8266 React
\ No newline at end of file diff --git a/data/www/js/0.40b9.js.gz b/data/www/js/0.40b9.js.gz new file mode 100644 index 0000000..b821fd5 Binary files /dev/null and b/data/www/js/0.40b9.js.gz differ diff --git a/data/www/js/0.439a.js.gz b/data/www/js/0.439a.js.gz deleted file mode 100644 index a2b0ff8..0000000 Binary files a/data/www/js/0.439a.js.gz and /dev/null differ diff --git a/interface/src/forms/APSettingsForm.js b/interface/src/forms/APSettingsForm.js index 582241d..bdd9191 100644 --- a/interface/src/forms/APSettingsForm.js +++ b/interface/src/forms/APSettingsForm.js @@ -66,7 +66,7 @@ class APSettingsForm extends React.Component { end(); delete _arduinoOTA; _arduinoOTA = nullptr; } diff --git a/src/WiFiSettingsService.cpp b/src/WiFiSettingsService.cpp index 9e6c5c2..5483b46 100644 --- a/src/WiFiSettingsService.cpp +++ b/src/WiFiSettingsService.cpp @@ -1,6 +1,12 @@ #include -WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) {} +WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) { +#if defined(ESP8266) + _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1)); +#elif defined(ESP_PLATFORM) + WiFi.onEvent(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED); +#endif +} WiFiSettingsService::~WiFiSettingsService() {} @@ -56,27 +62,11 @@ void WiFiSettingsService::onConfigUpdated() { } void WiFiSettingsService::reconfigureWiFiConnection() { - Serial.println("Reconfiguring WiFi..."); - - // disconnect and de-configure wifi and software access point + // disconnect and de-configure wifi WiFi.disconnect(true); - // configure for static IP - if (_staticIPConfig) { - WiFi.config(_localIP, _gatewayIP, _subnetMask, _dnsIP1, _dnsIP2); - } else { - // configure for DHCP -#if defined(ESP8266) - WiFi.config(INADDR_ANY, INADDR_ANY, INADDR_ANY); - WiFi.hostname(_hostname); -#elif defined(ESP_PLATFORM) - WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); - WiFi.setHostname(_hostname.c_str()); -#endif - } - - // connect to the network - WiFi.begin(_ssid.c_str(), _password.c_str()); + // reset last connection attempt to force loop to reconnect immediately + _lastConnectionAttempt = 0; } void WiFiSettingsService::readIP(JsonObject& root, String key, IPAddress& _ip){ @@ -90,3 +80,48 @@ void WiFiSettingsService::writeIP(JsonObject& root, String key, IPAddress& _ip){ root[key] = _ip.toString(); } } + +void WiFiSettingsService::loop() { + unsigned long currentMillis = millis(); + if (!_lastConnectionAttempt || (unsigned long)(currentMillis - _lastConnectionAttempt) >= WIFI_RECONNECTION_DELAY) { + _lastConnectionAttempt = currentMillis; + manageSTA(); + } +} + +void WiFiSettingsService::manageSTA() { + // Abort if already connected, or if we have no SSID + if (WiFi.isConnected() || _ssid.length() == 0) { + return; + } + // Connect or reconnect as required + if ((WiFi.getMode() & WIFI_STA) == 0) { + Serial.println("Connecting to WiFi."); + if (_staticIPConfig) { + // configure for static IP + WiFi.config(_localIP, _gatewayIP, _subnetMask, _dnsIP1, _dnsIP2); + } else { + // configure for DHCP +#if defined(ESP8266) + WiFi.config(INADDR_ANY, INADDR_ANY, INADDR_ANY); + WiFi.hostname(_hostname); +#elif defined(ESP_PLATFORM) + WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); + WiFi.setHostname(_hostname.c_str()); +#endif + } + // attempt to connect to the network + WiFi.begin(_ssid.c_str(), _password.c_str()); + } +} + +#if defined(ESP8266) +void WiFiSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) { + WiFi.disconnect(true); +} +#elif defined(ESP_PLATFORM) +void WiFiSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { + WiFi.disconnect(true); +} +#endif + diff --git a/src/WiFiSettingsService.h b/src/WiFiSettingsService.h index 1675471..e846728 100644 --- a/src/WiFiSettingsService.h +++ b/src/WiFiSettingsService.h @@ -6,6 +6,7 @@ #define WIFI_SETTINGS_FILE "/config/wifiSettings.json" #define WIFI_SETTINGS_SERVICE_PATH "/rest/wifiSettings" +#define WIFI_RECONNECTION_DELAY 1000 * 60 class WiFiSettingsService : public AdminSettingsService { @@ -15,13 +16,13 @@ class WiFiSettingsService : public AdminSettingsService { ~WiFiSettingsService(); void begin(); + void loop(); protected: void readFromJsonObject(JsonObject& root); void writeToJsonObject(JsonObject& root); void onConfigUpdated(); - void reconfigureWiFiConnection(); private: // connection settings @@ -30,6 +31,9 @@ class WiFiSettingsService : public AdminSettingsService { String _hostname; bool _staticIPConfig; + // for the mangement delay loop + unsigned long _lastConnectionAttempt; + // optional configuration for static IP address IPAddress _localIP; IPAddress _gatewayIP; @@ -37,8 +41,17 @@ class WiFiSettingsService : public AdminSettingsService { IPAddress _dnsIP1; IPAddress _dnsIP2; +#if defined(ESP8266) + WiFiEventHandler _onStationModeDisconnectedHandler; + void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event); +#elif defined(ESP_PLATFORM) + void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info); +#endif + void readIP(JsonObject& root, String key, IPAddress& _ip); void writeIP(JsonObject& root, String key, IPAddress& _ip); + void reconfigureWiFiConnection(); + void manageSTA(); }; diff --git a/src/main.cpp b/src/main.cpp index d3256fb..1aa3266 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,7 +10,7 @@ #endif #include - +#include #include #include #include @@ -41,51 +41,59 @@ APStatus apStatus = APStatus(&server, &securitySettingsService); SystemStatus systemStatus = SystemStatus(&server, &securitySettingsService);; void setup() { - // Disable wifi config persistance - WiFi.persistent(false); + // Disable wifi config persistance and auto reconnect + WiFi.persistent(false); + WiFi.setAutoReconnect(false); - Serial.begin(SERIAL_BAUD_RATE); - SPIFFS.begin(); +#if defined(ESP_PLATFORM) + // Init the wifi driver on ESP32 + WiFi.mode(WIFI_MODE_MAX); + WiFi.mode(WIFI_MODE_NULL); +#endif - // start security settings service first - securitySettingsService.begin(); + Serial.begin(SERIAL_BAUD_RATE); + SPIFFS.begin(); + + // Start security settings service first + securitySettingsService.begin(); - // start services - ntpSettingsService.begin(); - otaSettingsService.begin(); - apSettingsService.begin(); - wifiSettingsService.begin(); + // Start services + ntpSettingsService.begin(); + otaSettingsService.begin(); + apSettingsService.begin(); + wifiSettingsService.begin(); - // Serving static resources from /www/ - server.serveStatic("/js/", SPIFFS, "/www/js/"); - server.serveStatic("/css/", SPIFFS, "/www/css/"); - server.serveStatic("/fonts/", SPIFFS, "/www/fonts/"); - server.serveStatic("/app/", SPIFFS, "/www/app/"); - server.serveStatic("/favicon.ico", SPIFFS, "/www/favicon.ico"); + // Serving static resources from /www/ + server.serveStatic("/js/", SPIFFS, "/www/js/"); + server.serveStatic("/css/", SPIFFS, "/www/css/"); + server.serveStatic("/fonts/", SPIFFS, "/www/fonts/"); + server.serveStatic("/app/", SPIFFS, "/www/app/"); + server.serveStatic("/favicon.ico", SPIFFS, "/www/favicon.ico"); - // Serving all other get requests with "/www/index.htm" - // OPTIONS get a straight up 200 response - server.onNotFound([](AsyncWebServerRequest *request) { - if (request->method() == HTTP_GET) { - request->send(SPIFFS, "/www/index.html"); - } else if (request->method() == HTTP_OPTIONS) { - request->send(200); - } else { - request->send(404); - } - }); + // Serving all other get requests with "/www/index.htm" + // OPTIONS get a straight up 200 response + server.onNotFound([](AsyncWebServerRequest *request) { + if (request->method() == HTTP_GET) { + request->send(SPIFFS, "/www/index.html"); + } else if (request->method() == HTTP_OPTIONS) { + request->send(200); + } else { + request->send(404); + } + }); - // Disable CORS if required - #if defined(ENABLE_CORS) - DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", CORS_ORIGIN); - DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization"); - DefaultHeaders::Instance().addHeader("Access-Control-Allow-Credentials", "true"); - #endif + // Disable CORS if required + #if defined(ENABLE_CORS) + DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", CORS_ORIGIN); + DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization"); + DefaultHeaders::Instance().addHeader("Access-Control-Allow-Credentials", "true"); + #endif - server.begin(); + server.begin(); } void loop() { + wifiSettingsService.loop(); apSettingsService.loop(); ntpSettingsService.loop(); otaSettingsService.loop();