Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wifi] Make more robust checks and increase timeout #1147

Merged
merged 1 commit into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ESPEasy-Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#define DEFAULT_IPRANGE_HIGH "255.255.255.255" // Allowed IP range to access webserver
#define DEFAULT_IP_BLOCK_LEVEL 1 // 0: ALL_ALLOWED 1: LOCAL_SUBNET_ALLOWED 2: ONLY_IP_RANGE_ALLOWED

#define DEFAULT_WIFI_CONNECTION_TIMEOUT 10000 // minimum timeout in ms for WiFi to be connected.

// --- Default Controller ------------------------------------------------------------------------------
#define DEFAULT_CONTROLLER false // true or false enabled or disabled, set 1st controller defaults
// using a default template, you also need to set a DEFAULT PROTOCOL to a suitable MQTT protocol !
Expand Down
46 changes: 30 additions & 16 deletions src/Wifi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
String WifiGetAPssid()
{
String ssid(Settings.Name);
ssid+=F("_");
ssid+=F("-");
ssid+=Settings.Unit;
return (ssid);
}
Expand All @@ -28,8 +28,8 @@ void WifiAPconfig()
// create and store unique AP SSID/PW to prevent ESP from starting AP mode with default SSID and No password!
// setup ssid for AP Mode when needed

String softAPSSID=WifiGetAPssid();
String pwd = SecuritySettings.WifiAPKey;
String softAPSSID=WifiGetAPssid();
String pwd = SecuritySettings.WifiAPKey;
WiFi.softAP(softAPSSID.c_str(),pwd.c_str());
// We start in STA mode
WifiAPMode(false);
Expand Down Expand Up @@ -85,7 +85,7 @@ void WifiAPMode(boolean state)
//********************************************************************************
// Set Wifi config
//********************************************************************************
void prepareWiFi() {
bool prepareWiFi() {
String log = "";
char hostname[40];
strncpy(hostname, WifiGetHostname().c_str(), sizeof(hostname));
Expand All @@ -108,15 +108,15 @@ void prepareWiFi() {
const IPAddress dns = Settings.DNS;
WiFi.config(ip, gw, subnet, dns);
}
return selectValidWiFiSettings();
}

//********************************************************************************
// Configure network and connect to Wifi SSID and SSID2
//********************************************************************************
boolean WifiConnect(byte connectAttempts)
{
prepareWiFi();
if (anyValidWifiSettings()) {
if (prepareWiFi()) {
//try to connect to one of the access points
bool connected = WifiConnectAndWait(connectAttempts);
if (!connected) {
Expand All @@ -138,10 +138,9 @@ boolean WifiConnect(byte connectAttempts)
// Start connect to WiFi and check later to see if connected.
//********************************************************************************
void WiFiConnectRelaxed() {
prepareWiFi();
wifiConnected = false;
wifi_connect_attempt = 0;
if (anyValidWifiSettings()) {
if (prepareWiFi()) {
tryConnectWiFi();
return;
}
Expand Down Expand Up @@ -172,20 +171,29 @@ bool anyValidWifiSettings() {
}

bool selectNextWiFiSettings() {
uint8_t tmp = lastWiFiSettings;
lastWiFiSettings = (lastWiFiSettings + 1) % 2;
if (!wifiSettingsValid(getLastWiFiSettingsSSID(), getLastWiFiSettingsPassphrase())) {
// other settings are not correct, switch back.
lastWiFiSettings = (lastWiFiSettings + 1) % 2;
lastWiFiSettings = tmp;
return false; // Nothing changed.
}
return true;
}

bool selectValidWiFiSettings() {
if (wifiSettingsValid(getLastWiFiSettingsSSID(), getLastWiFiSettingsPassphrase()))
return true;
return selectNextWiFiSettings();
}

bool wifiSettingsValid(const char* ssid, const char* pass) {
if (ssid[0] == 0 || (strcasecmp(ssid, "ssid") == 0)) {
return false;
}
if (pass[0] == 0) return false;
if (strlen(ssid) > 32) return false;
if (strlen(pass) > 64) return false;
return true;
}

Expand All @@ -194,7 +202,7 @@ bool wifiConnectTimeoutReached() {
// wait until it connects + add some device specific random offset to prevent
// all nodes overloading the accesspoint when turning on at the same time.
const unsigned int randomOffset_in_sec = wifi_connect_attempt == 1 ? 0 : 1000 * ((ESP.getChipId() & 0xF));
return timeOutReached(wifi_connect_timer + 7000 + randomOffset_in_sec);
return timeOutReached(wifi_connect_timer + DEFAULT_WIFI_CONNECTION_TIMEOUT + randomOffset_in_sec);
}

//********************************************************************************
Expand All @@ -207,6 +215,8 @@ bool tryConnectWiFi() {
return(true); //already connected, need to disconnect first
if (!wifiConnectTimeoutReached())
return true; // timeout not reached yet, thus no need to retry again.
if (!anyValidWifiSettings())
return false;

if (wifi_connect_attempt != 0 && ((wifi_connect_attempt % 3) == 0)) {
// Change to other wifi settings.
Expand All @@ -217,8 +227,12 @@ bool tryConnectWiFi() {
//everything failed, activate AP mode (will deactivate automatically after a while if its connected again)
WifiAPMode(true);
}
const char* ssid = getLastWiFiSettingsSSID();
const char* passphrase = getLastWiFiSettingsPassphrase();
String ssid;
ssid.reserve(32);
String passphrase;
passphrase.reserve(64);
ssid = getLastWiFiSettingsSSID();
passphrase = getLastWiFiSettingsPassphrase();
String log = F("WIFI : Connecting ");
log += ssid;
log += F(" attempt #");
Expand All @@ -229,12 +243,12 @@ bool tryConnectWiFi() {
switch (wifi_connect_attempt) {
case 0:
if (lastBSSID[0] == 0)
WiFi.begin(ssid, passphrase);
WiFi.begin(ssid.c_str(), passphrase.c_str());
else
WiFi.begin(ssid, passphrase, 0, &lastBSSID[0]);
WiFi.begin(ssid.c_str(), passphrase.c_str(), 0, &lastBSSID[0]);
break;
default:
WiFi.begin(ssid, passphrase);
WiFi.begin(ssid.c_str(), passphrase.c_str());
}
++wifi_connect_attempt;
switch (WiFi.status()) {
Expand Down Expand Up @@ -295,9 +309,9 @@ boolean WifiConnectAndWait(byte connectAttempts)

bool checkWifiJustConnected() {
if (wifiConnected) return true;
delay(1);
if (WiFi.status() != WL_CONNECTED) {
statusLED(false);
delay(1);
return false;
}
wifiConnected = true;
Expand Down