-
Notifications
You must be signed in to change notification settings - Fork 98
Starting WiFIManger is very slow (2000ms) #6
Comments
Hi @CrispinP
I'll move that to when it's
This will be included in future new v1.0.7. In the mean time, I'll update the master version, you can just download and use. New debug output:
You can see that the total time in WiFiManager is only ~150ms. |
Your contribution is noted in README.md
|
To answer your question
The ESP32 WiFi Credentials are also stored in Flash, the same as ESP8266. In ESP_WiFiManager.cpp
|
Awesome! Thank you. I got the latest version and can confirm it's a lot faster :D
That's 214ms to connect. I've tried it a number of times and it is mostly around that. Sometimes it faults to around 1600ms but that is when, for rssi reasons, it jumps to another AP (I have multiple APs in the house). Thank you for your library and your response!! Question: I've asked the same question over at the ubnt forums. Maybe they can help too :) Thanks again :) |
Hi @CrispinP |
I've asked a question here: https://community.ui.com/questions/Reduce-connection-duration/ee97e943-9c6d-4b6a-a85c-f9c5a24bc4f2 I did a wireshark on the dhcp server I have, pfsense, and see that it is taking around 37ms from request to response. So, that leaves me with around 160ms "lost". I'll set up another SSID for testing and mess around with the DTIM and see if I can reduce it without any ill effect. I'm wondering if I am nearing what is reasonable with WiFi. If I wanted quicker then something like Z-Wave might be a better suited option. If you do find anything I would be interested in trying it 😄 |
Can you try to reduce the Router's
to see if it's improving and to isolate if the problem is in WiFi libraries or Router's settings ? Certainly, we have to find the balanced and optimized solutions / settings. |
Yup, I will try that.
You can see the main work is actually completed in around 60ms. The preamble to get to do work takes 230ms 😆 |
I just had a preliminary test, and it seems that the real culprit is the Router DHCP server. Certainly, it will help if you have faster Router with lighter load. This is the terminal debug output, showing
I almost copied / modified the ESP32 WiFi code and saw no issue yet with the slow WiFi connection. I also tested by modifying If you're interested, I'll post the whole ( |
You need a faster router / dhcp server 😸 Thanks for digging into this. I'd be interested in seeing the code you used as a comparison. BTW - you posted your WiFi creds in the post above |
Hi,
//#define ESP32"
// #include <esp_wifi.h>
// #include <WiFiClient.h>
#define ESP_getChipId() ((uint32_t)ESP.getEfuseMac())
#define LED_ON HIGH
#define LED_OFF LOW
String ssid = "ESP_" + String(ESP_getChipId(), HEX);
String password = "";
// SSID and PW for your Router
String Router_SSID;
String Router_Pass;
// Use false if you don't like to display Available Pages in Information Page of Config Portal
// Comment out or use true to display Available Pages in Information Page of Config Portal
// Must be placed before #include <ESP_WiFiManager.h>
#define USE_AVAILABLE_PAGES false
#include <ESP_WiFiManager.h> //https://github.com/khoih-prog/ESP_WiFiManager
// Indicates whether ESP has WiFi credentials saved from previous session, or double reset detected
bool initialConfig = false;
IPAddress ip(192, 168, 2, 199) ; //WeatherStationESP
IPAddress gateway(192, 168, 2, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(10, 10, 50, 2); //Google dns
void printDebug(String msg)
{
Serial.print("[");
Serial.print(millis());
Serial.print("\t\t]\t");
Serial.println(msg);
}
// You have to remove static from these 2 vars declaration in WiFiSTA.cpp
// From
//static wl_status_t _sta_status = WL_NO_SHIELD;
//static EventGroupHandle_t _sta_status_group = NULL;
// To
//wl_status_t _sta_status = WL_NO_SHIELD;
//EventGroupHandle_t _sta_status_group = NULL;
//
extern wl_status_t _sta_status;
extern EventGroupHandle_t _sta_status_group;
bool _autoReconnect = true;
bool _useStaticIp = false;
const char * status_names[] = { "WL_IDLE_STATUS", "WL_NO_SSID_AVAIL", "WL_SCAN_COMPLETED",
"WL_CONNECTED", "WL_CONNECT_FAILED", "WL_CONNECTION_LOST", "WL_DISCONNECTED"
};
/*
typedef enum
{
WL_NO_SHIELD = 255, // for compatibility with WiFi Shield library
WL_IDLE_STATUS = 0,
WL_NO_SSID_AVAIL = 1,
WL_SCAN_COMPLETED = 2,
WL_CONNECTED = 3,
WL_CONNECT_FAILED = 4,
WL_CONNECTION_LOST = 5,
WL_DISCONNECTED = 6
} wl_status_t;
*/
wl_status_t WiFi_status()
{
if (!_sta_status_group)
{
//printDebug("status 1: = " + String(_sta_status));
return _sta_status;
}
wl_status_t status = (wl_status_t) xEventGroupClearBits(_sta_status_group, 0);
//printDebug("status 2: = " + String(status_names[status]));
return status;
}
bool sta_config_equal(const wifi_config_t& lhs, const wifi_config_t& rhs)
{
if (memcmp(&lhs, &rhs, sizeof(wifi_config_t)) != 0)
{
return false;
}
return true;
}
wl_status_t WiFi_begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true)
{
//printDebug("WiFi_begin: 1");
if (!WiFi.enableSTA(true))
{
printDebug("STA enable failed!");
return WL_CONNECT_FAILED;
}
//printDebug("WiFi_begin: 2");
if (!ssid || *ssid == 0x00 || strlen(ssid) > 31)
{
printDebug("SSID too long or missing!");
return WL_CONNECT_FAILED;
}
//printDebug("WiFi_begin: 3");
if (passphrase && strlen(passphrase) > 64)
{
printDebug("passphrase too long!");
return WL_CONNECT_FAILED;
}
//printDebug("WiFi_begin: 4");
wifi_config_t conf;
memset(&conf, 0, sizeof(wifi_config_t));
strcpy(reinterpret_cast<char*>(conf.sta.ssid), ssid);
//printDebug("WiFi_begin: 5");
if (passphrase) {
if (strlen(passphrase) == 64)
{
// it's not a passphrase, is the PSK
memcpy(reinterpret_cast<char*>(conf.sta.password), passphrase, 64);
}
else
{
strcpy(reinterpret_cast<char*>(conf.sta.password), passphrase);
}
}
//printDebug("WiFi_begin: 6");
if (bssid)
{
conf.sta.bssid_set = 1;
memcpy((void *) &conf.sta.bssid[0], (void *) bssid, 6);
}
//printDebug("WiFi_begin: 7");
if (channel > 0 && channel <= 13)
{
conf.sta.channel = channel;
}
//printDebug("WiFi_begin: 8");
wifi_config_t current_conf;
esp_wifi_get_config(WIFI_IF_STA, ¤t_conf);
//printDebug("WiFi_begin: 9");
if (!sta_config_equal(current_conf, conf))
{
if (esp_wifi_disconnect())
{
printDebug("disconnect failed!");
return WL_CONNECT_FAILED;
}
esp_wifi_set_config(WIFI_IF_STA, &conf);
}
else if (WiFi_status() == WL_CONNECTED)
{
return WL_CONNECTED;
}
//KH
// Espressif added this
else
{
esp_wifi_set_config(WIFI_IF_STA, &conf);
}
//
//printDebug("WiFi_begin: 10");
if (!_useStaticIp)
{
if (tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA) == ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED) {
printDebug("dhcp client start failed!");
return WL_CONNECT_FAILED;
}
}
else
{
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
}
//printDebug("WiFi_begin: 11");
if (connect && esp_wifi_connect())
{
printDebug("connect failed!");
return WL_CONNECT_FAILED;
}
//printDebug("WiFi_begin: 12");
return WiFi_status();
}
void setup()
{
// put your setup code here, to run once:
// initialize the LED digital pin as an output.
Serial.begin(115200);
printDebug("Hello");
//Local intialization. Once its business is done, there is no need to keep it around
printDebug("Starting WiFiManager");
ESP_WiFiManager ESP_wifiManager("ConfigOnDoubleReset");
printDebug("Starting WiFiManager. Done");
ESP_wifiManager.setMinimumSignalQuality(-1);
// We can't use WiFi.SSID() in ESP32 as it's only valid after connected.
// SSID and Password stored in ESP32 wifi_ap_record_t and wifi_config_t are also cleared in reboot
// Have to create a new function to store in EEPROM/SPIFFS for this purpose
Router_SSID = ESP_wifiManager.WiFi_SSID();
Router_Pass = ESP_wifiManager.WiFi_Pass();
//Remove this line if you do not want to see WiFi password printed
printDebug("Stored: SSID = " + Router_SSID + ", Pass = " + Router_Pass);
// SSID to uppercase
ssid.toUpperCase();
printDebug("Starting WiFiManager. Finished all setup");
if (Router_SSID != "")
{
printDebug("Got stored Credentials. Timeout 60s");
ESP_wifiManager.setConfigPortalTimeout(60); //If no access point name has been previously entered disable timeout.
}
else
{
printDebug("No stored Credentials. No timeout");
initialConfig = true;
}
// Remove this to force into Config Portal
//initialConfig = true;
if (initialConfig)
{
ssid.toUpperCase();
password = "My" + ssid;
printDebug("Starting configuration portal: AP SSID = " + ssid + ", Pass = " + password );
//sets timeout in seconds until configuration portal gets turned off.
//If not specified device will remain in configuration mode until
//switched off via webserver or device is restarted.
//ESP_wifiManager.setConfigPortalTimeout(600);
//it starts an access point
//and goes into a blocking loop awaiting configuration
if (!ESP_wifiManager.startConfigPortal((const char *) ssid.c_str(), password.c_str()))
printDebug("Not connected to WiFi but continuing anyway.");
else
printDebug("WiFi connected...yeey :)");
}
#define WIFI_CONNECT_TIMEOUT 30000L
#define WHILE_LOOP_DELAY 25
#define WHILE_LOOP_STEPS (WIFI_CONNECT_TIMEOUT / ( 3 * WHILE_LOOP_DELAY ))
// To force connecting to new Router
Router_SSID = "New-Router";
Router_Pass = "New-Pass";
unsigned long startedAt = millis();
WiFi.mode(WIFI_STA);
WiFi.persistent (true);
printDebug("Trying to connect SSID = " + Router_SSID + ", Pass = " + Router_Pass);
//wl_status_t connectStatus = WiFi.status();
wl_status_t connectStatus = WiFi_status();
while ( (connectStatus != WL_CONNECTED) && (millis() - startedAt < WIFI_CONNECT_TIMEOUT ) )
{
//WiFi.begin(Router_SSID.c_str(), Router_Pass.c_str());
WiFi_begin(Router_SSID.c_str(), Router_Pass.c_str());
//printDebug("WiFi_begin done");
int i = 0;
while (i++ < WHILE_LOOP_STEPS)
{
//printDebug("Retry" + String(i));
connectStatus = WiFi_status();
if (connectStatus == WL_CONNECTED)
break;
else
delay(WHILE_LOOP_DELAY);
}
}
if (connectStatus == WL_CONNECTED)
{
printDebug("WiFi connected!");
}
else
printDebug("Failed to connect");
printDebug("WiFi done");
}
void loop()
{
} |
### Releases 1.0.7 1. Use `just-in-time` scanWiFiNetworks() to reduce connection time necessary for battery-operated DeepSleep application. Thanks to [CrispinP](https://github.com/CrispinP) for identifying, requesting and testing. See [Starting WiFIManger is very slow (2000ms)](#6) 2. Fix bug relating SPIFFS in examples : - [ConfigOnSwitchFS](examples/ConfigOnSwitchFS) - [ConfigPortalParamsOnSwitch](examples/ConfigPortalParamsOnSwitch) (now support ArduinoJson 6.0.0+ as well as 5.13.5-) - [AutoConnectWithFSParameters](examples/AutoConnectWithFSParameters) See [Having issue to read the SPIFF file](#14), Thanks to [OttoKlaasen](https://github.com/OttoKlaasen) to report. 3. Fix [README](README.md). See [Accessing manager after connection](#15)
Hi,
The original WiFIManager manager was very slow at connecting. A workaround was to try connect with normal WiFI and if that failed after 1000ms, invoke WiFi Manager. This work around does not seem to work with your library on the ESP32.
The debug:
The delay happens here:
The normal duration to connect to my wifi, with and without DHCP is around 200-250ms.
All my other ESP8266 do this with a few exceptions when they bounce to another bssid.
Sample code to show duration.
Running on batteries and sleeping for most of of the time, the wasted 1500ms is crucial. My whole program goes to sleep in around 400ms. :)
WorkAround for the esp8266:
Options / Questions:
thanks!
The text was updated successfully, but these errors were encountered: