Merge pull request #32 from rjwats/ft_autoreconnect
Fix Access Point Issues
This commit is contained in:
commit
067ca81315
@ -1,5 +1,5 @@
|
||||
{
|
||||
"ssid":"ssid",
|
||||
"ssid":"",
|
||||
"password":"password",
|
||||
"hostname":"esp8266-react",
|
||||
"static_ip_config":false
|
||||
|
@ -1 +1 @@
|
||||
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"><link rel="stylesheet" href="/css/roboto.css"><link rel="manifest" href="/app/manifest.json"><title>ESP8266 React</title></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script src="/js/1.b351.js"></script><script src="/js/2.8ca9.js"></script><script src="/js/0.439a.js"></script></body></html>
|
||||
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"><link rel="stylesheet" href="/css/roboto.css"><link rel="manifest" href="/app/manifest.json"><title>ESP8266 React</title></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script src="/js/1.b351.js"></script><script src="/js/2.8ca9.js"></script><script src="/js/0.40b9.js"></script></body></html>
|
BIN
data/www/js/0.40b9.js.gz
Normal file
BIN
data/www/js/0.40b9.js.gz
Normal file
Binary file not shown.
Binary file not shown.
@ -66,7 +66,7 @@ class APSettingsForm extends React.Component {
|
||||
<Fragment>
|
||||
<TextValidator
|
||||
validators={['required', 'matchRegexp:^.{1,32}$']}
|
||||
errorMessages={['Access Point SSID is required', 'Access Point SSID must be 32 characeters or less']}
|
||||
errorMessages={['Access Point SSID is required', 'Access Point SSID must be 32 characters or less']}
|
||||
name="ssid"
|
||||
label="Access Point SSID"
|
||||
className={classes.textField}
|
||||
|
@ -99,8 +99,8 @@ class WiFiSettingsForm extends React.Component {
|
||||
{
|
||||
selectedNetwork ? this.renderSelectedNetwork() :
|
||||
<TextValidator
|
||||
validators={['required', 'matchRegexp:^.{1,32}$']}
|
||||
errorMessages={['SSID is required', 'SSID must be 32 characeters or less']}
|
||||
validators={['matchRegexp:^.{0,32}$']}
|
||||
errorMessages={['SSID must be 32 characters or less']}
|
||||
name="ssid"
|
||||
label="SSID"
|
||||
className={classes.textField}
|
||||
|
@ -6,7 +6,7 @@ NTPSettingsService::NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityM
|
||||
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
|
||||
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1));
|
||||
#elif defined(ESP_PLATFORM)
|
||||
WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_AP_STADISCONNECTED);
|
||||
WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
|
||||
WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
|
||||
#endif
|
||||
|
||||
|
@ -39,6 +39,7 @@ void OTASettingsService::writeToJsonObject(JsonObject& root) {
|
||||
|
||||
void OTASettingsService::configureArduinoOTA() {
|
||||
if (_arduinoOTA){
|
||||
_arduinoOTA->end();
|
||||
delete _arduinoOTA;
|
||||
_arduinoOTA = nullptr;
|
||||
}
|
||||
|
@ -1,6 +1,12 @@
|
||||
#include <WiFiSettingsService.h>
|
||||
|
||||
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
|
||||
|
||||
|
@ -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();
|
||||
|
||||
};
|
||||
|
||||
|
80
src/main.cpp
80
src/main.cpp
@ -10,7 +10,7 @@
|
||||
#endif
|
||||
|
||||
#include <FS.h>
|
||||
|
||||
#include <esp_wifi.h>
|
||||
#include <SecuritySettingsService.h>
|
||||
#include <WiFiSettingsService.h>
|
||||
#include <APSettingsService.h>
|
||||
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user