Improve Wifi connection and portal
This commit is contained in:
@@ -22,20 +22,24 @@ void WifiDevice::initialize()
|
||||
String pass = _preferences->getString(preference_wifi_pass, "");
|
||||
WiFi.setHostname(_hostname.c_str());
|
||||
|
||||
if(!_ipConfiguration->dhcpEnabled())
|
||||
{
|
||||
WiFi.config(_ipConfiguration->ipAddress(), _ipConfiguration->dnsServer(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet());
|
||||
}
|
||||
|
||||
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info)
|
||||
{
|
||||
if(event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED)
|
||||
if(event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED || event == ARDUINO_EVENT_WIFI_STA_STOP)
|
||||
{
|
||||
if(!_openAP && !_connecting && _connected)
|
||||
{
|
||||
onDisconnected();
|
||||
_hasIP = false;
|
||||
}
|
||||
}
|
||||
else if(event == ARDUINO_EVENT_WIFI_STA_GOT_IP)
|
||||
{
|
||||
_hasIP = true;
|
||||
}
|
||||
else if(event == ARDUINO_EVENT_WIFI_STA_LOST_IP)
|
||||
{
|
||||
_hasIP = false;
|
||||
}
|
||||
else if(event == ARDUINO_EVENT_WIFI_STA_CONNECTED)
|
||||
{
|
||||
onConnected();
|
||||
@@ -117,10 +121,7 @@ void WifiDevice::initialize()
|
||||
}
|
||||
else
|
||||
{
|
||||
Log->println("No SSID or Wifi password saved, opening AP");
|
||||
_connectOnScanDone = false;
|
||||
_openAP = true;
|
||||
scan(false, true);
|
||||
restartEsp(RestartReason::ReconfigureWifi);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -158,23 +159,28 @@ void WifiDevice::initialize()
|
||||
|
||||
void WifiDevice::scan(bool passive, bool async)
|
||||
{
|
||||
WiFi.scanDelete();
|
||||
if(!_connecting)
|
||||
{
|
||||
WiFi.scanDelete();
|
||||
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
|
||||
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
|
||||
|
||||
if(async)
|
||||
{
|
||||
Log->println(F("Wi-Fi async scan started"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Log->println(F("Wi-Fi sync scan started"));
|
||||
}
|
||||
if(passive)
|
||||
{
|
||||
WiFi.scanNetworks(async,false,true,75U);
|
||||
}
|
||||
else
|
||||
{
|
||||
WiFi.scanNetworks(async);
|
||||
if(async)
|
||||
{
|
||||
Log->println(F("Wi-Fi async scan started"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Log->println(F("Wi-Fi sync scan started"));
|
||||
}
|
||||
if(passive)
|
||||
{
|
||||
WiFi.scanNetworks(async,false,true,75U);
|
||||
}
|
||||
else
|
||||
{
|
||||
WiFi.scanNetworks(async);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,12 +188,9 @@ void WifiDevice::openAP()
|
||||
{
|
||||
if(_startAP)
|
||||
{
|
||||
WiFi.persistent(false);
|
||||
WiFi.mode(WIFI_AP_STA);
|
||||
WiFi.persistent(false);
|
||||
WiFi.softAPsetHostname(_hostname.c_str());
|
||||
WiFi.softAP("NukiHub", "NukiHubESP32");
|
||||
WiFi.persistent(false);
|
||||
_startAP = false;
|
||||
}
|
||||
}
|
||||
@@ -197,7 +200,6 @@ bool WifiDevice::connect()
|
||||
bool ret = false;
|
||||
String ssid = _preferences->getString(preference_wifi_ssid, "");
|
||||
String pass = _preferences->getString(preference_wifi_pass, "");
|
||||
WiFi.persistent(false);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.setHostname(_hostname.c_str());
|
||||
delay(500);
|
||||
@@ -239,99 +241,75 @@ bool WifiDevice::connect()
|
||||
else
|
||||
{
|
||||
_connecting = true;
|
||||
esp_wifi_scan_stop();
|
||||
Log->println(String(F("Trying to connect to SSID ")) + ssid + String(F(" found with RSSI: ")) +
|
||||
String(WiFi.RSSI(bestConnection)) + String(F("(")) +
|
||||
String(constrain((100.0 + WiFi.RSSI(bestConnection)) * 2, 0, 100)) +
|
||||
String(F(" %) and BSSID: ")) + WiFi.BSSIDstr(bestConnection) +
|
||||
String(F(" and channel: ")) + String(WiFi.channel(bestConnection)));
|
||||
ret = WiFi.begin(ssid.c_str(), pass.c_str(), WiFi.channel(bestConnection), WiFi.BSSID(bestConnection), true);
|
||||
WiFi.persistent(false);
|
||||
_connecting = false;
|
||||
}
|
||||
|
||||
if(!ret)
|
||||
{
|
||||
int loop = 0;
|
||||
|
||||
while(!isConnected() && loop < 200)
|
||||
|
||||
|
||||
if(!_ipConfiguration->dhcpEnabled())
|
||||
{
|
||||
loop++;
|
||||
delay(100);
|
||||
WiFi.config(_ipConfiguration->ipAddress(), _ipConfiguration->dnsServer(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet());
|
||||
}
|
||||
|
||||
if(!isConnected())
|
||||
|
||||
WiFi.begin(ssid, pass);
|
||||
auto status = WiFi.waitForConnectResult(10000);
|
||||
|
||||
switch (status)
|
||||
{
|
||||
esp_wifi_disconnect();
|
||||
esp_wifi_stop();
|
||||
esp_wifi_deinit();
|
||||
|
||||
Log->println(F("Failed to connect. Wait for ESP restart."));
|
||||
delay(1000);
|
||||
restartEsp(RestartReason::WifiInitFailed);
|
||||
case WL_CONNECTED:
|
||||
Log->println("WiFi connected");
|
||||
break;
|
||||
case WL_NO_SSID_AVAIL:
|
||||
Log->println("WiFi SSID not available");
|
||||
break;
|
||||
case WL_CONNECT_FAILED:
|
||||
Log->println("WiFi connection failed");
|
||||
break;
|
||||
case WL_IDLE_STATUS:
|
||||
Log->println("WiFi changing status");
|
||||
break;
|
||||
case WL_DISCONNECTED:
|
||||
Log->println("WiFi disconnected");
|
||||
break;
|
||||
default:
|
||||
Log->println("WiFi timeout");
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!_preferences->getBool(preference_wifi_converted, false))
|
||||
{
|
||||
_preferences->putBool(preference_wifi_converted, true);
|
||||
}
|
||||
|
||||
int loop = 0;
|
||||
|
||||
while(!isConnected() && loop < 200)
|
||||
{
|
||||
loop++;
|
||||
delay(100);
|
||||
}
|
||||
|
||||
if(!isConnected())
|
||||
|
||||
if (status != WL_CONNECTED)
|
||||
{
|
||||
if(_preferences->getBool(preference_restart_on_disconnect, false) && ((esp_timer_get_time() / 1000) > 60000))
|
||||
{
|
||||
restartEsp(RestartReason::RestartOnDisconnectWatchdog);
|
||||
_connecting = false;
|
||||
return false;
|
||||
}
|
||||
Log->print("Connection failed, retrying");
|
||||
Log->println("Retrying");
|
||||
_connectOnScanDone = true;
|
||||
_openAP = false;
|
||||
scan(false, true);
|
||||
_connecting = false;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!_preferences->getBool(preference_wifi_converted, false))
|
||||
{
|
||||
_preferences->putBool(preference_wifi_converted, true);
|
||||
}
|
||||
_connecting = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
return false;
|
||||
}
|
||||
|
||||
void WifiDevice::reconfigure()
|
||||
{
|
||||
bool changed = false;
|
||||
wifi_config_t wifi_cfg;
|
||||
if(esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg) != ESP_OK) {
|
||||
Log->println("Failed to get Wi-Fi configuration in RAM");
|
||||
}
|
||||
|
||||
if (esp_wifi_set_storage(WIFI_STORAGE_FLASH) != ESP_OK) {
|
||||
Log->println("Failed to set storage Wi-Fi");
|
||||
}
|
||||
|
||||
if(sizeof(wifi_cfg.sta.ssid) > 0)
|
||||
{
|
||||
memset(wifi_cfg.sta.ssid, 0, sizeof(wifi_cfg.sta.ssid));
|
||||
changed = true;
|
||||
}
|
||||
if(sizeof(wifi_cfg.sta.password) > 0)
|
||||
{
|
||||
memset(wifi_cfg.sta.password, 0, sizeof(wifi_cfg.sta.password));
|
||||
changed = true;
|
||||
}
|
||||
if(changed)
|
||||
{
|
||||
if (esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg) != ESP_OK) {
|
||||
Log->println("Failed to clear NVS Wi-Fi configuration");
|
||||
}
|
||||
}
|
||||
|
||||
_preferences->putString(preference_wifi_ssid, "");
|
||||
_preferences->putString(preference_wifi_pass, "");
|
||||
delay(200);
|
||||
@@ -340,7 +318,16 @@ void WifiDevice::reconfigure()
|
||||
|
||||
bool WifiDevice::isConnected()
|
||||
{
|
||||
return (WiFi.status() == WL_CONNECTED);
|
||||
if (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!_hasIP)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void WifiDevice::onConnected()
|
||||
@@ -363,12 +350,17 @@ void WifiDevice::onDisconnected()
|
||||
_connecting = true;
|
||||
String ssid = _preferences->getString(preference_wifi_ssid, "");
|
||||
String pass = _preferences->getString(preference_wifi_pass, "");
|
||||
WiFi.begin(ssid.c_str(), pass.c_str(), _connectedChannel, _connectedBSSID, true);
|
||||
WiFi.persistent(false);
|
||||
|
||||
if(!_ipConfiguration->dhcpEnabled())
|
||||
{
|
||||
WiFi.config(_ipConfiguration->ipAddress(), _ipConfiguration->dnsServer(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet());
|
||||
}
|
||||
|
||||
WiFi.begin(ssid, pass);
|
||||
|
||||
int loop = 0;
|
||||
|
||||
while(!isConnected() && loop < 50)
|
||||
while(!isConnected() && loop < 200)
|
||||
{
|
||||
loop++;
|
||||
delay(100);
|
||||
@@ -381,7 +373,6 @@ void WifiDevice::onDisconnected()
|
||||
{
|
||||
if(_preferences->getBool(preference_restart_on_disconnect, false) && ((esp_timer_get_time() / 1000) > 60000)) restartEsp(RestartReason::RestartOnDisconnectWatchdog);
|
||||
|
||||
WiFi.persistent(false);
|
||||
WiFi.disconnect(true);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.disconnect();
|
||||
|
||||
Reference in New Issue
Block a user