I'm working on an ESP8266 project using the LilyGO TTGO T-Base hardware (which basically is a Lolin Wemos D1 Mini clone).
The board is connected to a sensor and takes readings at a specified time interval. These readings are saved locally and only uploaded to a server when WiFi is switched on. For that I have connected a physical on/off flip switch to the board to manually enable or disable WiFi connectivity using software (calling WifiOnOrOff = digitalRead(wifiSwitchPin);). This is necessary to preserve power since the board runs of a battery pack and is normally placed in a nature environment where no WiFi networks are present.
Turning on WiFi and making a connection to a network using the <ESP8266WiFi.h> library is very straight forward and works like a charm. But when all data uploading to the server is done, how do I fully disable the WiFi after flipping the physical switch to the off position? That is: until I switch it on again, say after 15 days, for a new round of uploading.
I found the following functions online but could not quite understand what is the best approach here with the <ESP8266WiFi.h> library to fully ditch an existing WiFi connection:
WiFi.forceSleepBegin();
WiFi.mode(WIFI_OFF);
wifi.suspend(ms); // this is not usable in my case since the time is not a fixed number
Are there any advantages or drawbacks I need to be aware of with the functions above? Or do they basically do the same but just have a different name?
Are there better ways to achieve this programmatically?
(And just for my understanding: do the functions above have opposites to turn the WiFi on again? Something like WiFi.mode(WIFI_ON); or WiFi.forceWakeBegin();? Or should I just call WiFi.begin(); to make a connection again?)
It turns out WiFi.disconnect(); is the function needed. Not well documented but it works:
if(WiFi.status() == WL_CONNECTED){ // wifi connection is active
// ditch the existing connection
WiFi.disconnect();
// allow some time for the connection to be fully dropped, **important!**
delay(100);
// check if the connection was really dropped
if(WiFi.status() == WL_CONNECTED){
Serial.println("Connection is still alive...");
}else{
Serial.println("Connection successfully terminated.");
}
}else{ // there was no wifi connection to begin with, so nothing to disconnect
Serial.println("No active connection was found to be terminated.");
}
Doesn't that just terminate the 'connection', leaving WiFi active?
I guess you'd have to get out the ammeter to know for certain (unless estab'd by the Docs).
I'm not 100% sure since it appears that turning off WiFi is not near as well documented as turning it on.
It seems that WiFi.mode(WIFI_OFF); is the only thing that kills it completely, but getting WiFi turned on again after calling that is a pain in the behind. I haven't been able to get that to work reliably. It should be something along the line of what's below but when searching the internet there are lots of reports it's plagued by bugs over the years.
Another approach involves WiFi.forceSleepBegin(); combined with WiFi.forceSleepWake(); or WiFi.resume(). It looks like I have to create multiple setups and measure current draw to get to the bottom of this. For now WiFi.disconnect(); is acceptable.