Skip to content

Commit bc3daef

Browse files
authored
WIFI_RESUME improve speed and example (#7877)
Improve resume speed by passing in last known BSSID Provide a simpler example for WIFI_SHUTDOWN/WIFI_RESUME Add documentation for WIFI_SHUTDOWN and WIFI_RESUME.
1 parent e4435fa commit bc3daef

File tree

5 files changed

+122
-270
lines changed

5 files changed

+122
-270
lines changed

doc/esp8266wifi/generic-class.rst

+34-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ For the SoftAP interface, when the interface is brought up, any servers should b
3232

3333
A detailed explanation of ``WiFiEventHandler`` can be found in the section with `examples :arrow\_right: <generic-examples.rst>`__ dedicated specifically to the Generic Class..
3434

35-
Alternatively, check the example sketch `WiFiEvents.ino <https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiEvents/WiFiEvents.ino>`__ available inside examples folder of the ESP8266WiFi library.
35+
Alternatively, check the example sketch `WiFiEvents.ino <https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiEvents/WiFiEvents.ino>`__ available in the examples folder of the ESP8266WiFi library.
3636

3737

3838
persistent
@@ -42,30 +42,56 @@ persistent
4242
4343
WiFi.persistent(persistent)
4444
45-
ESP8266 is able to reconnect to the last used Wi-Fi network or establishes the same Access Point upon power up or reset.
45+
ESP8266 is able to reconnect to the last used WiFi network or establishes the same Access Point upon power up or reset.
4646
By default, these settings are written to specific sectors of flash memory every time they are changed in ``WiFi.begin(ssid, passphrase)`` or ``WiFi.softAP(ssid, passphrase, channel)``, and when ``WiFi.disconnect`` or ``WiFi.softAPdisconnect`` is invoked.
4747
Frequently calling these functions could cause wear on the flash memory (see issue `#1054 <https://github.com/esp8266/Arduino/issues/1054>`__).
4848

49-
Once ``WiFi.persistent(false)`` is called, ``WiFi.begin``, ``WiFi.disconnect``, ``WiFi.softAP``, or ``WiFi.softAPdisconnect`` only changes the current in-memory Wi-Fi settings, and does not affect the Wi-Fi settings stored in flash memory.
49+
Once ``WiFi.persistent(false)`` is called, ``WiFi.begin``, ``WiFi.disconnect``, ``WiFi.softAP``, or ``WiFi.softAPdisconnect`` only changes the current in-memory WiFi settings, and does not affect the WiFi settings stored in flash memory.
5050

5151
mode
5252
~~~~
5353

54+
Regular WiFi modes
55+
__________________
56+
5457
.. code:: cpp
5558
56-
WiFi.mode(m)
59+
bool mode(WiFiMode_t m)
60+
61+
Switches to one of the regular WiFi modes, where ``m`` is one of:
62+
63+
- ``WIFI_OFF``: turn WiFi off.
64+
- ``WIFI_STA``: switch to `Station (STA) <readme.rst#station>`__ mode.
65+
- ``WIFI_AP``: switch to `Access Point (AP) <readme.rst#soft-access-point>`__ mode.
66+
- ``WIFI_AP_STA``: enable both Station (STA) and Access Point (AP) mode.
67+
68+
Pseudo-modes
69+
____________
70+
71+
.. code:: cpp
72+
73+
bool mode(WiFiMode_t m, WiFiState* state)
74+
75+
Used with the following pseudo-modes, where ``m`` is one of:
76+
77+
- ``WIFI_SHUTDOWN``: Fills in the provided ``WiFiState`` structure, switches to ``WIFI_OFF`` mode and puts WiFi into forced sleep, preserving energy.
78+
- ``WIFI_RESUME``: Turns WiFi on and tries to re-establish the WiFi connection stored in the ``WiFiState`` structure.
79+
80+
These modes are used in low-power scenarios, e.g. where ESP.deepSleep is used between actions to preserve battery power.
81+
82+
It is the user's responsibility to preserve the WiFiState between ``WIFI_SHUTDOWN`` and ``WIFI_RESUME``, e.g. by storing it
83+
in RTC user data and/or flash memory.
5784

58-
- ``WiFi.mode(m)``: set mode to ``WIFI_AP``, ``WIFI_STA``,
59-
``WIFI_AP_STA`` or ``WIFI_OFF``
85+
There is an example sketch `WiFiShutdown.ino <https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiShutdown/WiFiShutdown.ino>`__ available in the examples folder of the ESP8266WiFi library.
6086

6187
getMode
6288
~~~~~~~
6389

6490
.. code:: cpp
6591
66-
WiFiMode_t WiFi.getMode()
92+
WiFiMode_t getMode()
6793
68-
- ``WiFi.getMode()``: return current Wi-Fi mode (one out of four modes above)
94+
Gets the current WiFi mode (one out of four regular modes above).
6995

7096
WiFi power management, DTIM
7197
~~~~~~~~~~~~~~~~~~~~~~~~~~~

doc/libraries.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Libraries
22
=========
33

4-
WiFi(ESP8266WiFi library)
5-
-------------------------
4+
WiFi (ESP8266WiFi library)
5+
--------------------------
66

77
ESP8266WiFi library has been developed basing on ESP8266 SDK, using naming convention and overall functionality philosophy of the `Arduino WiFi Shield library <https://www.arduino.cc/en/Reference/WiFi>`__. Over time the wealth Wi-Fi features ported from ESP8266 SDK to this library outgrew the APIs of WiFi Shield library and it became apparent that we need to provide separate documentation on what is new and extra.
88

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
// Demonstrate the use of WiFi.mode(WIFI_SHUTDOWN)/WiFi.mode(WIFI_RESUME)
3+
// Released to public domain
4+
5+
// Current on WEMOS D1 mini (including: LDO, usbserial chip):
6+
// ~85mA during normal operations
7+
// ~30mA during wifi shutdown
8+
// ~5mA during deepsleep
9+
10+
#ifndef STASSID
11+
#define STASSID "mynetwork"
12+
#define STAPSK "mynetworkpasswd"
13+
#endif
14+
15+
#ifndef RTC_USER_DATA_SLOT_WIFI_STATE
16+
#define RTC_USER_DATA_SLOT_WIFI_STATE 33u
17+
#endif
18+
19+
#include <ESP8266WiFi.h>
20+
#include <include/WiFiState.h> // WiFiState structure details
21+
22+
WiFiState state;
23+
24+
const char* ssid = STASSID;
25+
const char* password = STAPSK;
26+
27+
void preinit(void) {
28+
// Make sure, wifi stays off after boot.
29+
ESP8266WiFiClass::preinitWiFiOff();
30+
}
31+
32+
void setup() {
33+
Serial.begin(74880);
34+
//Serial.setDebugOutput(true); // If you need debug output
35+
Serial.println("Trying to resume WiFi connection...");
36+
37+
// May be necessary after deepSleep. Otherwise you may get "error: pll_cal exceeds 2ms!!!" when trying to connect
38+
delay(1);
39+
40+
// ---
41+
// Here you can do whatever you need to do that doesn't need a WiFi connection.
42+
// ---
43+
44+
ESP.rtcUserMemoryRead(RTC_USER_DATA_SLOT_WIFI_STATE, reinterpret_cast<uint32_t *>(&state), sizeof(state));
45+
unsigned long start = millis();
46+
47+
if (!WiFi.mode(WIFI_RESUME, &state)
48+
|| (WiFi.waitForConnectResult(10000) != WL_CONNECTED)) {
49+
Serial.println("Cannot resume WiFi connection, connecting via begin...");
50+
WiFi.persistent(false);
51+
52+
if (!WiFi.mode(WIFI_STA)
53+
|| !WiFi.begin(ssid, password)
54+
|| (WiFi.waitForConnectResult(10000) != WL_CONNECTED)) {
55+
WiFi.mode(WIFI_OFF);
56+
Serial.println("Cannot connect!");
57+
Serial.flush();
58+
ESP.deepSleep(10e6, RF_DISABLED);
59+
return;
60+
}
61+
}
62+
63+
unsigned long duration = millis() - start;
64+
Serial.printf("Duration: %f", duration * 0.001);
65+
Serial.println();
66+
67+
// ---
68+
// Here you can do whatever you need to do that needs a WiFi connection.
69+
// ---
70+
71+
WiFi.mode(WIFI_SHUTDOWN, &state);
72+
ESP.rtcUserMemoryWrite(RTC_USER_DATA_SLOT_WIFI_STATE, reinterpret_cast<uint32_t *>(&state), sizeof(state));
73+
74+
// ---
75+
// Here you can do whatever you need to do that doesn't need a WiFi connection anymore.
76+
// ---
77+
78+
Serial.println("Done.");
79+
Serial.flush();
80+
ESP.deepSleep(10e6, RF_DISABLED);
81+
}
82+
83+
void loop() {
84+
// Nothing to do here.
85+
}

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -826,11 +826,10 @@ bool ESP8266WiFiGenericClass::resumeFromShutdown (WiFiState* state)
826826
}
827827
}
828828
}
829-
// state->state.fwconfig.bssid is not real bssid (it's what user may have provided when bssid_set==1)
830829
auto beginResult = WiFi.begin((const char*)state->state.fwconfig.ssid,
831830
(const char*)state->state.fwconfig.password,
832831
state->state.channel,
833-
nullptr/*(const uint8_t*)state->state.fwconfig.bssid*/, // <- try with gw's mac address?
832+
state->state.fwconfig.bssid,
834833
true);
835834
if (beginResult == WL_CONNECT_FAILED)
836835
{

0 commit comments

Comments
 (0)