leave WiFi disconnected by default
implement controlled retry - fixed at 60 seconds
This commit is contained in:
parent
94656de3c0
commit
e7248c4774
@ -1,5 +1,5 @@
|
||||
{
|
||||
"ssid":"ssid",
|
||||
"ssid":"",
|
||||
"password":"password",
|
||||
"hostname":"esp8266-react",
|
||||
"static_ip_config":false
|
||||
|
@ -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}
|
||||
|
@ -56,27 +56,11 @@ void WiFiSettingsService::onConfigUpdated() {
|
||||
}
|
||||
|
||||
void WiFiSettingsService::reconfigureWiFiConnection() {
|
||||
Serial.println("Reconfiguring WiFi...");
|
||||
|
||||
// disconnect and de-configure wifi and software access point
|
||||
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 +74,40 @@ 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());
|
||||
} else {
|
||||
Serial.println("Retrying WiFi connection.");
|
||||
WiFi.reconnect();
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
@ -39,6 +43,8 @@ class WiFiSettingsService : public AdminSettingsService {
|
||||
|
||||
void readIP(JsonObject& root, String key, IPAddress& _ip);
|
||||
void writeIP(JsonObject& root, String key, IPAddress& _ip);
|
||||
void reconfigureWiFiConnection();
|
||||
void manageSTA();
|
||||
|
||||
};
|
||||
|
||||
|
@ -41,8 +41,9 @@ APStatus apStatus = APStatus(&server, &securitySettingsService);
|
||||
SystemStatus systemStatus = SystemStatus(&server, &securitySettingsService);;
|
||||
|
||||
void setup() {
|
||||
// Disable wifi config persistance
|
||||
// Disable wifi config persistance and auto reconnect
|
||||
WiFi.persistent(false);
|
||||
WiFi.setAutoReconnect(false);
|
||||
|
||||
Serial.begin(SERIAL_BAUD_RATE);
|
||||
SPIFFS.begin();
|
||||
@ -86,6 +87,7 @@ void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
wifiSettingsService.loop();
|
||||
apSettingsService.loop();
|
||||
ntpSettingsService.loop();
|
||||
otaSettingsService.loop();
|
||||
|
Loading…
Reference in New Issue
Block a user