Description
Hi, I am migrating the code of ESP8266 for battery powerred device. But seems, I have a problem to have reasonable time to connect to wifi. It takes at least 3 secods always. Here is the code:
#include <FS.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <SPIFFS.h>
// SKETCH BEGIN
AsyncWebServer server(80);
const char* ssid = "Bob";
const char* pwd = "xxx";
byte ip[] = {192,168,1,115};
byte gw[] = {192,168,1,1};
byte mask[] = {255,255,255,0};
byte dns[] = {194,41,3,193};
byte bssid[] = {0xF8, 0xD1, 0x11, 0x24, 0xB3, 0x84};
byte chnl = 4;
IPAddress getIpAddrFromArr(byte ipAddr[]) {
return IPAddress(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
}
void connectToWifi(String ssid, String pwd, byte ip[], byte gw[], byte mask[], byte dns[], byte bssid[], byte chnl ) {
if (WiFi.status() == WL_CONNECTED) return;
Serial.println(F("Connecting wifi..."));
long st = millis();
WiFi.enableSTA(true);
WiFi.mode(WIFI_STA);
if (ip[0] != 0) WiFi.config(getIpAddrFromArr(ip), getIpAddrFromArr(gw), getIpAddrFromArr(mask),getIpAddrFromArr(dns));
if ((ssid.length() != 0) && (pwd.length() == 0)) {
WiFi.begin(ssid.c_str());
} else if ((ssid.length() != 0) && (pwd.length() != 0)) {
if (bssid[0] == 0) {
WiFi.begin(ssid.c_str(), pwd.c_str());
} else {
WiFi.begin(ssid.c_str(), pwd.c_str(), chnl, bssid);
}
WiFi.setAutoConnect(true);
}
int attempts = 0;
while (WiFi.status() != WL_CONNECTED) {
if (++attempts < 300) {
Serial.print(".");
delay(50);
} else break;
}
if (WiFi.status() == WL_CONNECTED) {
long tm = millis() - st;
Serial.print(F("\nConnected to AP in "));
Serial.print(tm, DEC);
Serial.print(F("ms. IP: "));
for (byte thisByte = 0; thisByte < 4; thisByte++) {
Serial.print(String(WiFi.localIP()[thisByte], DEC));
if (thisByte != 3) Serial.print(".");
}
Serial.println();
}
}
void setup(){
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println(F("Connecting wifi..."));
long st = millis();
connectToWifi(ssid, pwd, ip, gw, mask, dns, bssid, chnl);
SPIFFS.begin();
server.on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/plain", String(ESP.getFreeHeap()));
});
server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html");
server.onNotFound([](AsyncWebServerRequest *request){
request->send(404);
});
server.onFileUpload([](AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) {
request->send(403);
});
server.begin();
}
void loop(){
}
So as is seen, I will do:
WiFi.enableSTA(true);
WiFi.mode(WIFI_STA);
WiFi.config(getIpAddrFromArr(ip), getIpAddrFromArr(gw), getIpAddrFromArr(mask),getIpAddrFromArr(dns));
WiFi.begin(ssid.c_str(), pwd.c_str(), chnl, bssid);
But this tahes around 3.2 seconds always:
Connecting wifi...
...............................................................
Connected to AP in 3201ms. IP: 192.168.1.115
On my ESP8266, this worked much much faster, I was connected in less than 100 ms always...
Anyone has an idea, why it take so long?????
Robert