esp8266 / esp8266/Arduino

Connection error when switching to other same SSID after wakeup from WiFi sleep

Open
#3,622 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
16.7k
Forks
13.1k
PR merge metrics
No merged PRs in 30d

Description

#### Hardware
Hardware: NodeMCU & ESP8266 12F

### Description

I have 2 Access Points with same ssid.
My ESP8266 connects to the first of them, does some tasks and then modem sleeps (to save battery). Other processes continue to work.
After a few minutes I wake the modem up to send some data to internet.
In case the first Access Points is down, the ESP connects to the second one.
The connection looks ok (WL_CONNECTED == WiFi.status()) , but real connection is not established.

I wrote a sample sketch to test by yourself.

### Sketch
```cpp
#include
#include
#include

String mySSID = "testSSID";
String myPSW = "testPSW";

void printConnectionInfo() {
Serial.print("WiFi is connected with IP: ");
Serial.print(WiFi.localIP());
Serial.print("\tChannel: ");
Serial.print(WiFi.channel());
Serial.print("\tMAC: ");
Serial.print(WiFi.BSSIDstr());
Serial.print("\tRSSI: ");
Serial.println(WiFi.RSSI());
}

bool wifiConnected()
{
return ((WL_CONNECTED == WiFi.status()) && (static_cast(WiFi.localIP()) != 0));
}

bool wifiConnect() {
bool ret = false;
Serial.println();
Serial.println("Connecting to WiFi");
WiFi.begin(mySSID.c_str(), myPSW.c_str());

int c=0;
ret = wifiConnected();
while (!ret && (c++ < 10)) {
Serial.print(".");
delay(1000);
ret = wifiConnected();
}
if (ret) printConnectionInfo();
return ret;
}

bool internetConnected()
{
HTTPClient client;
client.begin("http://www.google.com"); //Specify request destination
int httpCode = client.GET(); //Send the request
client.end(); //Close connection
return = (httpCode > 0)
}

void setup() {
Serial.begin(115200);
Serial.println();
}

void loop()
{
WiFi.forceSleepWake(); // wake wifi if sleeping
delay(1000);
wifiConnect();
delay(1000);

if (internetConnected()) {
Serial.println("Connected to google.");
}
else { Serial.println("Not connected."); }

Serial.print(F("Entering wifi sleep... "));
WiFi.forceSleepBegin();
delay(1000);
Serial.println("ok!");

for (int i=0; i<10; i++) {
Serial.print("s");
delay(1000);
}
Serial.println("ok.");

}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.