leave WiFi disconnected by default

implement controlled retry - fixed at 60 seconds
This commit is contained in:
Rick Watson 2019-06-04 21:13:55 +01:00
parent 94656de3c0
commit e7248c4774
6 changed files with 53 additions and 24 deletions

View File

@ -1,5 +1,5 @@
{ {
"ssid":"ssid", "ssid":"",
"password":"password", "password":"password",
"hostname":"esp8266-react", "hostname":"esp8266-react",
"static_ip_config":false "static_ip_config":false

View File

@ -66,7 +66,7 @@ class APSettingsForm extends React.Component {
<Fragment> <Fragment>
<TextValidator <TextValidator
validators={['required', 'matchRegexp:^.{1,32}$']} 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" name="ssid"
label="Access Point SSID" label="Access Point SSID"
className={classes.textField} className={classes.textField}

View File

@ -99,8 +99,8 @@ class WiFiSettingsForm extends React.Component {
{ {
selectedNetwork ? this.renderSelectedNetwork() : selectedNetwork ? this.renderSelectedNetwork() :
<TextValidator <TextValidator
validators={['required', 'matchRegexp:^.{1,32}$']} validators={['matchRegexp:^.{0,32}$']}
errorMessages={['SSID is required', 'SSID must be 32 characeters or less']} errorMessages={['SSID must be 32 characters or less']}
name="ssid" name="ssid"
label="SSID" label="SSID"
className={classes.textField} className={classes.textField}

View File

@ -56,27 +56,11 @@ void WiFiSettingsService::onConfigUpdated() {
} }
void WiFiSettingsService::reconfigureWiFiConnection() { void WiFiSettingsService::reconfigureWiFiConnection() {
Serial.println("Reconfiguring WiFi...");
// disconnect and de-configure wifi and software access point // disconnect and de-configure wifi and software access point
WiFi.disconnect(true); WiFi.disconnect(true);
// configure for static IP // reset last connection attempt to force loop to reconnect immediately
if (_staticIPConfig) { _lastConnectionAttempt = 0;
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());
} }
void WiFiSettingsService::readIP(JsonObject& root, String key, IPAddress& _ip){ void WiFiSettingsService::readIP(JsonObject& root, String key, IPAddress& _ip){
@ -90,3 +74,40 @@ void WiFiSettingsService::writeIP(JsonObject& root, String key, IPAddress& _ip){
root[key] = _ip.toString(); 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());
} else {
Serial.println("Retrying WiFi connection.");
WiFi.reconnect();
}
}

View File

@ -6,6 +6,7 @@
#define WIFI_SETTINGS_FILE "/config/wifiSettings.json" #define WIFI_SETTINGS_FILE "/config/wifiSettings.json"
#define WIFI_SETTINGS_SERVICE_PATH "/rest/wifiSettings" #define WIFI_SETTINGS_SERVICE_PATH "/rest/wifiSettings"
#define WIFI_RECONNECTION_DELAY 1000 * 60
class WiFiSettingsService : public AdminSettingsService { class WiFiSettingsService : public AdminSettingsService {
@ -15,13 +16,13 @@ class WiFiSettingsService : public AdminSettingsService {
~WiFiSettingsService(); ~WiFiSettingsService();
void begin(); void begin();
void loop();
protected: protected:
void readFromJsonObject(JsonObject& root); void readFromJsonObject(JsonObject& root);
void writeToJsonObject(JsonObject& root); void writeToJsonObject(JsonObject& root);
void onConfigUpdated(); void onConfigUpdated();
void reconfigureWiFiConnection();
private: private:
// connection settings // connection settings
@ -30,6 +31,9 @@ class WiFiSettingsService : public AdminSettingsService {
String _hostname; String _hostname;
bool _staticIPConfig; bool _staticIPConfig;
// for the mangement delay loop
unsigned long _lastConnectionAttempt;
// optional configuration for static IP address // optional configuration for static IP address
IPAddress _localIP; IPAddress _localIP;
IPAddress _gatewayIP; IPAddress _gatewayIP;
@ -39,6 +43,8 @@ class WiFiSettingsService : public AdminSettingsService {
void readIP(JsonObject& root, String key, IPAddress& _ip); void readIP(JsonObject& root, String key, IPAddress& _ip);
void writeIP(JsonObject& root, String key, IPAddress& _ip); void writeIP(JsonObject& root, String key, IPAddress& _ip);
void reconfigureWiFiConnection();
void manageSTA();
}; };

View File

@ -41,8 +41,9 @@ APStatus apStatus = APStatus(&server, &securitySettingsService);
SystemStatus systemStatus = SystemStatus(&server, &securitySettingsService);; SystemStatus systemStatus = SystemStatus(&server, &securitySettingsService);;
void setup() { void setup() {
// Disable wifi config persistance // Disable wifi config persistance and auto reconnect
WiFi.persistent(false); WiFi.persistent(false);
WiFi.setAutoReconnect(false);
Serial.begin(SERIAL_BAUD_RATE); Serial.begin(SERIAL_BAUD_RATE);
SPIFFS.begin(); SPIFFS.begin();
@ -86,6 +87,7 @@ void setup() {
} }
void loop() { void loop() {
wifiSettingsService.loop();
apSettingsService.loop(); apSettingsService.loop();
ntpSettingsService.loop(); ntpSettingsService.loop();
otaSettingsService.loop(); otaSettingsService.loop();