trouble with Wifi connection on ESP 8266 after deep sleep

Hallo,

I started experimenting around with deep sleep of an ESP 8266 board for power saving reasons. Unfortunately, the Wifi connection takes much longer after a deep sleep and I have to use additional code to make it connect as well.

When I use the following sketch, it takes about 0,5 seconds to establish the Wifi connection:

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

ESP8266WiFiMulti wifiMulti;

void setup() {

delay(10);
WiFi.mode(WIFI_STA);
wifiMulti.addAP(“SSID”, “PW”);
}

void loop() {}

But when I try to combine it with deepsleep in the following sketch, it takes 5 seconds.

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

uint32_t counter = 0;
ESP8266WiFiMulti wifiMulti;

void setup() {

// start Deep Sleep loop

Serial.begin(9600);
ESP.rtcUserMemoryRead(0, &counter, sizeof(counter));
if (counter>10000000) counter=0; // quick fix for having trouble with a correct start from 0
counter++;
ESP.rtcUserMemoryWrite(0, &counter, sizeof(counter));

if(counter<5){
WiFi.disconnect( true );
delay( 1 );
ESP.deepSleep(2e6, WAKE_RF_DISABLED);
}
if(counter==5) {
counter++;
ESP.rtcUserMemoryWrite(0, &counter, sizeof(counter));
ESP.deepSleep(100, WAKE_RF_DEFAULT);
//WiFi.forceSleepWake();
}
// end deep sleep loop

delay(10);
WiFi.mode( WIFI_STA );
wifiMulti.addAP(“SSID”, “PW”);
}

void loop() {
if(wifiMulti.run() != WL_CONNECTED) {
delay(1);
}
}

I need to add

if(wifiMulti.run() != WL_CONNECTED) {
delay(1);

inside the loop, which I don’t need in the first sketch. Otherwise it won’t connect at all. What is the reason for this and how can I overcome this problem? I would like to be able to connect to the Wifi network in 0,5s as I am able in sketch one.

The sketches have no purpose and are just for getting used to how the different parts behave, so whether they make sense or not it out of question.

Do you actually need to use wifiMulti as you are connecting to specific access point.

I do not need it right now, wifi.begin() would do as well, I will need it later on though so I try to test it like that right from the start

It gets even stranger. After I switched back from sketch 2 to sketch 1, it doesn't connect to the AP anymore. Same as on sketch 1, I need to add

if(wifiMulti.run() != WL_CONNECTED) {
delay(1);

to the loop() for the first connection. After the connection has been made once, I can then proceed to delete these two lines again and the connection will afterwards be extremely fast again, as soon as I plug in the device the connection is made. So somehow the deepsleep phase seems to throw something off which needs to be reloaded afterwards. How is that possible?

I had similar problem where connecting to the wifi network worked only after a first few deep sleep cycles (or even didn’t work at all) and after adding forceSleepWake()

...
WiFi.persistent(false);     // <-- prevents flash wearing?
WiFi.forceSleepWake();      // <-- WITHOUT THIS ESP CONNECTS ONLY AFTER FIRST FEW RESTARTS OR DOESN'T CONNECT AT ALL
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
...

everything started to work as expected. Here I described everything in more details.

I don’t know if newer libraries solved the problem becouse I am still using the same code that I know always works.