Skip to content

Commit 7442699

Browse files
committed
wifi: remove pseudo-modes for shutdown
make shutdown and resumeFromShutdown public removes extra code from the mode handler and include method description in the docs
1 parent 5ac64ff commit 7442699

File tree

7 files changed

+61
-59
lines changed

7 files changed

+61
-59
lines changed

doc/esp8266wifi/generic-class.rst

+34-24
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ Once ``WiFi.persistent(false)`` is called, ``WiFi.begin``, ``WiFi.disconnect``,
5151
mode
5252
~~~~
5353

54-
Regular WiFi modes
55-
__________________
56-
5754
.. code:: cpp
5855
5956
bool mode(WiFiMode_t m)
@@ -65,25 +62,6 @@ Switches to one of the regular WiFi modes, where ``m`` is one of:
6562
- ``WIFI_AP``: switch to `Access Point (AP) <readme.rst#soft-access-point>`__ mode.
6663
- ``WIFI_AP_STA``: enable both Station (STA) and Access Point (AP) mode.
6764

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.
84-
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.
86-
8765
getMode
8866
~~~~~~~
8967

@@ -170,6 +148,40 @@ getPhyMode
170148
171149
Gets the WiFi radio phy mode that is currently set.
172150

151+
forceSleepBegin
152+
~~~~~~~~~~~~~~~
153+
154+
.. code:: cpp
155+
156+
bool forceSleepBegin (uint32_t sleepUs=0)
157+
bool forceSleepWake ()
158+
159+
Saves the currently set WiFi mode and starts forced modem sleep for the specified time (us)
160+
161+
forceSleepWake
162+
~~~~~~~~~~~~~~
163+
164+
.. code:: cpp
165+
166+
bool forceSleepWake ()
167+
168+
Called after `forceSleepBegin()`. Restores the previous WiFi mode. Attemtps reconnection when STA was active.
169+
170+
shutdown and resumeFromShutdown
171+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
172+
173+
.. code:: cpp
174+
175+
bool shutdown (uint32_t sleepUs, WiFiState* state)
176+
bool resumeFromShutdown (WiFiState* state)
177+
178+
Stores the STA interface IP configuration in the specified state struct and calls ``forceSleepBegin(sleepUs)``.
179+
Restores STA interface configuration from the ``state`` and calls ``forceSleepWake()``.
180+
181+
These method is intended to be used in low-power scenarios, e.g. where ESP.deepSleep is used between actions to preserve battery power. It is the user's responsibility to preserve the WiFiState between ``shutdown()`` and ``resumeFromShutdown()`` by storing it in the RTC user data and/or flash memory.
182+
183+
See `WiFiShutdown.ino <https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiShutdown/WiFiShutdown.ino>`__ for an example of usage.
184+
173185
Other Function Calls
174186
~~~~~~~~~~~~~~~~~~~~
175187

@@ -179,8 +191,6 @@ Other Function Calls
179191
WiFiSleepType_t getSleepMode ()
180192
bool enableSTA (bool enable)
181193
bool enableAP (bool enable)
182-
bool forceSleepBegin (uint32 sleepUs=0)
183-
bool forceSleepWake ()
184194
int hostByName (const char *aHostname, IPAddress &aResult)
185195
186196
appeared with SDK pre-V3:

libraries/ESP8266WiFi/examples/WiFiShutdown/WiFiShutdown.ino

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
// Demonstrate the use of WiFi.mode(WIFI_SHUTDOWN)/WiFi.mode(WIFI_RESUME)
2+
// Demonstrate the use of WiFi.shutdown() and WiFi.resumeFromShutdown()
33
// Released to public domain
44

55
// Current on WEMOS D1 mini (including: LDO, usbserial chip):
@@ -44,7 +44,7 @@ void setup() {
4444
ESP.rtcUserMemoryRead(RTC_USER_DATA_SLOT_WIFI_STATE, reinterpret_cast<uint32_t *>(&state), sizeof(state));
4545
unsigned long start = millis();
4646

47-
if (!WiFi.mode(WIFI_RESUME, &state)
47+
if (!WiFi.resumeFromShutdown(&state)
4848
|| (WiFi.waitForConnectResult(10000) != WL_CONNECTED)) {
4949
Serial.println("Cannot resume WiFi connection, connecting via begin...");
5050
WiFi.persistent(false);
@@ -68,7 +68,7 @@ void setup() {
6868
// Here you can do whatever you need to do that needs a WiFi connection.
6969
// ---
7070

71-
WiFi.mode(WIFI_SHUTDOWN, &state);
71+
WiFi.shutdown(0, &state);
7272
ESP.rtcUserMemoryWrite(RTC_USER_DATA_SLOT_WIFI_STATE, reinterpret_cast<uint32_t *>(&state), sizeof(state));
7373

7474
// ---
@@ -82,4 +82,4 @@ void setup() {
8282

8383
void loop() {
8484
// Nothing to do here.
85-
}
85+
}

libraries/ESP8266WiFi/keywords.txt

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ enableSTA KEYWORD2
5656
enableAP KEYWORD2
5757
forceSleepBegin KEYWORD2
5858
forceSleepWake KEYWORD2
59+
shutdown KEYWORD2
60+
resumeFromShutdown KEYWORD2
5961

6062
#ESP8266WiFi
6163
printDiag KEYWORD2

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

+3-14
Original file line numberDiff line numberDiff line change
@@ -401,21 +401,10 @@ bool ESP8266WiFiGenericClass::getPersistent(){
401401
* set new mode
402402
* @param m WiFiMode_t
403403
*/
404-
bool ESP8266WiFiGenericClass::mode(WiFiMode_t m, WiFiState* state) {
405-
if (m == WIFI_SHUTDOWN) {
406-
return shutdown(0, state);
407-
}
408-
else if (m == WIFI_RESUME) {
409-
return resumeFromShutdown(state);
410-
}
411-
else if (m & ~(WIFI_STA | WIFI_AP))
404+
bool ESP8266WiFiGenericClass::mode(WiFiMode_t m) {
405+
if (m & ~(WIFI_STA | WIFI_AP))
412406
// any other bits than legacy disallowed
413407
return false;
414-
415-
// m is now WIFI_STA, WIFI_AP or WIFI_AP_STA
416-
if (state)
417-
{
418-
DEBUG_WIFI("core: state is useless without SHUTDOWN or RESUME\n");
419408
}
420409

421410
if (wifi_fpm_get_sleep_type() != NONE_SLEEP_T) {
@@ -729,7 +718,7 @@ bool ESP8266WiFiGenericClass::shutdownValidCRC (const WiFiState* state)
729718
return state && (crc32(&state->state, sizeof(state->state)) == state->crc);
730719
}
731720

732-
bool ESP8266WiFiGenericClass::shutdown (uint32 sleepUs, WiFiState* state)
721+
bool ESP8266WiFiGenericClass::shutdown (uint32_t sleepUs, WiFiState* state)
733722
{
734723
bool persistent = _persistent;
735724
WiFiMode_t before_off_mode = getMode();

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h

+11-9
Original file line numberDiff line numberDiff line change
@@ -122,30 +122,32 @@ class ESP8266WiFiGenericClass {
122122

123123
void persistent(bool persistent);
124124

125-
bool mode(WiFiMode_t, WiFiState* state = nullptr);
125+
bool mode(WiFiMode_t);
126126
WiFiMode_t getMode();
127127

128128
bool enableSTA(bool enable);
129129
bool enableAP(bool enable);
130130

131-
bool forceSleepBegin(uint32 sleepUs = 0);
131+
bool forceSleepBegin(uint32_t sleepUs = 0);
132132
bool forceSleepWake();
133133

134-
static uint32_t shutdownCRC (const WiFiState* state);
134+
// wrappers around mode() and forceSleepBegin/Wake
135+
// - sleepUs is WiFi.forceSleepBegin() parameter, 0 means forever
136+
// - saveState is the user's state to hold configuration on restore
137+
bool shutdown(uint32_t sleepUs = 0, WiFiState* stateSave = nullptr);
138+
bool resumeFromShutdown(WiFiState* savedState = nullptr);
139+
135140
static bool shutdownValidCRC (const WiFiState* state);
141+
136142
static void preinitWiFiOff (); //meant to be called in user-defined preinit()
137143

138144
protected:
139145
static bool _persistent;
140146
static WiFiMode_t _forceSleepLastMode;
141147

142-
static void _eventCallback(void *event);
148+
static uint32_t shutdownCRC (const WiFiState* state);
143149

144-
// called by WiFi.mode(SHUTDOWN/RESTORE, state)
145-
// - sleepUs is WiFi.forceSleepBegin() parameter, 0 = forever
146-
// - saveState is the user's state to hold configuration on restore
147-
bool shutdown (uint32 sleepUs = 0, WiFiState* stateSave = nullptr);
148-
bool resumeFromShutdown (WiFiState* savedState = nullptr);
150+
static void _eventCallback(void *event);
149151

150152
// ----------------------------------------------------------------------------------------------
151153
// ------------------------------------ Generic Network function --------------------------------

libraries/ESP8266WiFi/src/ESP8266WiFiType.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434

3535
typedef enum WiFiMode
3636
{
37-
WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3,
38-
/* these two pseudo modes are experimental: */ WIFI_SHUTDOWN = 4, WIFI_RESUME = 8
37+
WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3
3938
} WiFiMode_t;
4039

4140
typedef enum WiFiPhyMode

libraries/esp8266/examples/LowPowerDemo/LowPowerDemo.ino

+6-6
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ void runTest2() {
195195

196196
void runTest3() {
197197
Serial.println(F("\n3rd test - Forced Modem Sleep"));
198-
WiFi.mode(WIFI_SHUTDOWN, &nv->wss); // shut the modem down and save the WiFi state for faster reconnection
198+
WiFi.shutdown(0, &nv->wss); // shut the modem down and save the WiFi state for faster reconnection
199199
// WiFi.forceSleepBegin(delay_in_uS); // alternate method of Forced Modem Sleep for an optional timed shutdown,
200200
// with WiFi.forceSleepBegin(0xFFFFFFF); the modem sleeps until you wake it, with values <= 0xFFFFFFE it's timed
201201
// delay(10); // it doesn't always go to sleep unless you delay(10); yield() wasn't reliable
@@ -290,7 +290,7 @@ void runTest7() {
290290
delay(50); // debounce time for the switch, pushbutton released
291291
waitPushbutton(false, blinkDelay); // set true if you want to see Automatic Modem Sleep
292292
digitalWrite(LED, LOW); // turn the LED on, at least briefly
293-
//WiFi.mode(WIFI_SHUTDOWN, &nv->wss); // Forced Modem Sleep for a more Instant Deep Sleep,
293+
//WiFi.shutdown(0, &nv->wss); // Forced Modem Sleep for a more Instant Deep Sleep,
294294
// and no extended RFCAL as it goes into Deep Sleep
295295
Serial.println(F("going into Deep Sleep now..."));
296296
printMillis(); // show time difference across sleep
@@ -308,7 +308,7 @@ void runTest8() {
308308
readVoltage(); // read internal VCC
309309
Serial.println(F("press the switch to continue"));
310310
waitPushbutton(false, blinkDelay); // set true if you want to see Automatic Modem Sleep
311-
//WiFi.mode(WIFI_SHUTDOWN, &nv->wss); // Forced Modem Sleep for a more Instant Deep Sleep,
311+
//WiFi.shutdown(0, &nv->wss); // Forced Modem Sleep for a more Instant Deep Sleep,
312312
// and no extended RFCAL as it goes into Deep Sleep
313313
Serial.println(F("going into Deep Sleep now..."));
314314
Serial.flush(); // needs a delay(10) or Serial.flush() else it doesn't print the whole message
@@ -322,7 +322,7 @@ void runTest9() {
322322
readVoltage(); // read internal VCC
323323
Serial.println(F("press the switch to continue"));
324324
waitPushbutton(false, blinkDelay); // set true if you want to see Automatic Modem Sleep
325-
WiFi.mode(WIFI_SHUTDOWN, &nv->wss); // Forced Modem Sleep for a more Instant Deep Sleep
325+
WiFi.shutdown(0, &nv->wss); // Forced Modem Sleep for a more Instant Deep Sleep
326326
Serial.println(F("going into Deep Sleep now..."));
327327
Serial.flush(); // needs a delay(10) or Serial.flush() else it doesn't print the whole message
328328
testPoint_HIGH; // testPoint set HIGH to track Deep Sleep period, cleared at startup()
@@ -335,7 +335,7 @@ void runTest10() {
335335
readVoltage(); // read internal VCC
336336
Serial.println(F("press the switch to continue"));
337337
waitPushbutton(false, blinkDelay); // set true if you want to see Automatic Modem Sleep
338-
//WiFi.mode(WIFI_SHUTDOWN); // Forced Modem Sleep for a more Instant Deep Sleep
338+
//WiFi.shutdown(); // Forced Modem Sleep for a more Instant Deep Sleep
339339
Serial.println(F("going into Deep Sleep now..."));
340340
Serial.flush(); // needs a delay(10) or Serial.flush() else it doesn't print the whole message
341341
testPoint_HIGH; // testPoint set HIGH to track Deep Sleep period, cleared at startup()
@@ -405,7 +405,7 @@ void initWiFi() {
405405
memcpy((uint32_t*) &nv->rtcData.rstCount + 1, (uint32_t*) &nv->wss, sizeof(nv->wss)); // save a copy of it
406406
Serial.println(F("resuming WiFi"));
407407
}
408-
if (!(WiFi.mode(WIFI_RESUME, &nv->wss))) { // couldn't resume, or no valid saved WiFi state yet
408+
if (!(WiFi.resumeFromShutdown(&nv->wss))) { // couldn't resume, or no valid saved WiFi state yet
409409
/* Explicitly set the ESP8266 as a WiFi-client (STAtion mode), otherwise by default it
410410
would try to act as both a client and an access-point and could cause network issues
411411
with other WiFi devices on your network. */

0 commit comments

Comments
 (0)