Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING] Disable WiFi at boot by default #7902

Merged
merged 29 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ab16b8c
Disable WiFi at boot by default
d-a-v Mar 2, 2021
6310da8
+define WIFI_IS_OFF_AT_BOOT
d-a-v Mar 2, 2021
330b229
remove now useless example
d-a-v Mar 3, 2021
68d338e
Merge branch 'master' into WiFiOffAtBoot
d-a-v Mar 3, 2021
253bbc5
Merge branch 'master' into WiFiOffAtBoot
d-a-v Mar 3, 2021
5ead6a2
Merge branch 'WiFiOffAtBoot' of github.com:d-a-v/Arduino into WiFiOff…
d-a-v Mar 3, 2021
b2a53b7
mv enableWiFiAtBootTime() to core_esp8266_features.h
d-a-v Mar 3, 2021
7756427
Merge branch 'master' into WiFiOffAtBoot
d-a-v Mar 15, 2021
64ff4b0
Merge branch 'WiFiOffAtBoot' of github.com:d-a-v/Arduino into WiFiOff…
d-a-v Mar 15, 2021
2d4ca1d
sync with master
d-a-v Mar 15, 2021
8510670
Merge branch 'master' into WiFiOffAtBoot
earlephilhower Mar 15, 2021
1059beb
Merge branch 'master' into WiFiOffAtBoot
earlephilhower Mar 16, 2021
7fd4116
per @earlephilhower review: a file was missing
d-a-v Mar 16, 2021
c15cb5d
doc
d-a-v Mar 16, 2021
9d7eb2e
WiFi persistence is now false by default
d-a-v Mar 16, 2021
227e557
Merge branch 'master' into WiFiOffAtBoot
d-a-v Mar 16, 2021
eca1367
fix doc
d-a-v Mar 16, 2021
8b0d641
Merge branch 'WiFiOffAtBoot' of github.com:d-a-v/Arduino into WiFiOff…
d-a-v Mar 16, 2021
6d9ec26
ditto
d-a-v Mar 16, 2021
8ecc7f0
doc: remove sphinx warnings (fix links and formatting)
d-a-v Mar 16, 2021
8332c42
fix link name
d-a-v Mar 16, 2021
df8f8a7
fix doc
d-a-v Mar 18, 2021
20020c0
legacy: restore persistence
d-a-v Mar 18, 2021
4e5abc6
Merge branch 'master' into WiFiOffAtBoot
d-a-v Mar 31, 2021
2eb8bb1
undeprecate preinit()
d-a-v Mar 31, 2021
da5966b
move force modem up to when mode has changed (per @mcspr review)
d-a-v Mar 31, 2021
f0f1f55
do not wake up from sleep when mode if OFF
d-a-v Mar 31, 2021
77087f0
Merge branch 'master' into WiFiOffAtBoot
d-a-v Apr 9, 2021
5d12aa2
fix doc per review
d-a-v Apr 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cores/esp8266/core_esp8266_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#define CORE_HAS_UMM

#define WIFI_HAS_EVENT_CALLBACK
#define WIFI_IS_OFF_AT_BOOT

#include <stdlib.h> // malloc()
#include <stddef.h> // size_t
Expand Down Expand Up @@ -104,6 +105,8 @@ uint64_t micros64(void);
void delay(unsigned long);
void delayMicroseconds(unsigned int us);

void enableWiFiAtBootTime (void) __attribute__((noinline));

#if defined(F_CPU) || defined(CORE_MOCK)
#ifdef __cplusplus
constexpr
Expand Down
16 changes: 14 additions & 2 deletions cores/esp8266/core_esp8266_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,18 @@ extern "C" void app_entry (void)
extern "C" void preinit (void) __attribute__((weak));
extern "C" void preinit (void)
{
/* do nothing by default */
/* does nothing, kept for backward compatibility */
}

extern "C" void __disableWiFiAtBootTime (void) __attribute__((weak));
extern "C" void __disableWiFiAtBootTime (void)
{
// Starting from arduino core v3: wifi is disabled at boot time
// WiFi.begin() or WiFi.softAP() will wake WiFi up
wifi_set_opmode_current(0/*WIFI_OFF*/);
wifi_fpm_set_sleep_type(MODEM_SLEEP_T);
wifi_fpm_open();
wifi_fpm_do_sleep(0xFFFFFFF);
}

extern "C" void user_init(void) {
Expand Down Expand Up @@ -360,7 +371,8 @@ extern "C" void user_init(void) {
#if defined(MMU_IRAM_HEAP)
umm_init_iram();
#endif
preinit(); // Prior to C++ Dynamic Init (not related to above init() ). Meant to be user redefinable.
preinit(); // deprecated
__disableWiFiAtBootTime(); // default weak function disables WiFi
d-a-v marked this conversation as resolved.
Show resolved Hide resolved

ets_task(loop_task,
LOOP_TASK_PRIORITY, s_loop_queue,
Expand Down
3 changes: 2 additions & 1 deletion cores/esp8266/coredecls.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void esp_schedule();
void tune_timeshift64 (uint64_t now_us);
void disable_extra4k_at_link_time (void) __attribute__((noinline));
bool sntp_set_timezone_in_seconds(int32_t timezone);
void __disableWiFiAtBootTime (void) __attribute__((noinline));
void __real_system_restart_local() __attribute__((noreturn));

uint32_t sqrt32 (uint32_t n);
Expand All @@ -34,6 +35,6 @@ using TrivialCB = std::function<void()>;
void settimeofday_cb (const BoolCB& cb);
void settimeofday_cb (const TrivialCB& cb);

#endif
#endif // __cplusplus

#endif // __COREDECLS_H
20 changes: 20 additions & 0 deletions cores/esp8266/enable_wifi_at_boot_time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* empty wrappers to play with linker and reenable wifi at boot time
*/

#include "coredecls.h"

extern "C" void enableWiFiAtBootTime()
{
/*
* Called by user from anywhere, does nothing and allows overriding
* the core_esp8266_main.cpp's default disableWiFiAtBootTime() by the
* one below, at link time.
*/
}

extern "C" void __disableWiFiAtBootTime()
{
// overrides the default __disableWiFiAtBootTime:
// Does nothing: WiFi is enabled by default in nonos-sdk
}
27 changes: 27 additions & 0 deletions doc/esp8266wifi/generic-class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,33 @@ persistent

WiFi.persistent(persistent)

Starting from version 3 of this core, **persistence is disabled by default
and WiFi does not anymore automatically fires up at boot** (see PR `#7902 <https://github.com/esp8266/Arduino/pull/7902>`__).
d-a-v marked this conversation as resolved.
Show resolved Hide resolved

Previously, SDK was automatically starting WiFi at boot. This was probably
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest highlighting here that the prior to v3 persistent was defaulted to true, and so the wifi creds were always stored to flash when the WiFi was configured, which could lead to a reduced life of the wifi config flash sector, and hence the entire ESP. Starting with v3, persistent defaults to false, so creds aren't stored by the SDK in the flash sector and hence don't survive across reboots.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devyte I think this is not precisely correct. Settings that were once persisted, remain that way, so thereafter, WiFi.begin(), with no WiFi.persistent(true) in the sketch, as long as enableWiFiAtBootTime() is somewhere in the sketch, will suffice to auto-connect to the AP.

intended for the Espressif AT FW which is interactive and preserves WiFi
state accross reboots. This behavior is generally irrelevant with the
Arduino API because sketches start with ``WiFi.begin()`` or
``WiFi.softAP()``.

This change is harmless with standard sketches: Calls to ``WiFi.mode()`` do
enable radio as usual. It also smooths current spikes at boot and decreases
DHCP stress.

Legacy behavior can be restored by calling ``enableWiFiAtBoot()`` from
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To restore legacy behavior, in addition to calling enableWiFiAtBoot() the user has to call WiFi.persitent(true). This should be used with care, because use could lead to early wear of the wifi config flash sector and hence device failure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment about, WiFi.persistent(true) is only needed to change the flash credentials cache, not in a sketch that just wants to use whatever was persisted at any time prior to it.

Copy link
Collaborator Author

@d-a-v d-a-v Mar 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the user has to call WiFi.persitent(true).

True !
I will add it in enableWiFiAtBootTime()
(edit: added)

could lead to early wear of the wifi config flash sector

Given the fact that when persistence is enabled, user config is compared to stored config, I think that's OK.
What do you both think ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always thought this mode was very important !!!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the wifi is active there is a consumption of energy that it would be good to keep in case of not use, especially if you use batteries for their power supply. In case of sending data, as sometimes I do, it is good to write to the internal memory and then make only one connection per day and send all the data recorded in the preset intervals.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also good to test with a number of different ESP modules (ESP12E/F/S for example) to see if this also has an effect on deep sleep power consumption.
There have been a number of reports on deep sleep power consumption not being close to 60 uA on some nodes, with some reporting success with the most unclear assembly blobs possible and others claiming you need to do a short deep sleep first and then again call deep sleep to get true low power deep sleep and others reporting the WiFi will not start after deep sleep until you go into deep sleep again.

So I would not be surprised to see such topics pop up again if this is changed. (or this may be the final fix for it.... who knows ;) )

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you need deep sleep and low power, you need to set up a series of special things, such as early wifi init, persistent wifi creds, static IP, etc, and then your app will usually have something in setup() and an empty loop. Then you need to check power and optimize more. That's an entirely different and very specific use case compared to the basic "start wifi, connect to some router, wiggle some pins" sketch that newer users usually start with.
In other words, deep sleep needs a special kind of sketch unlike the typical Arduino style setup - loop case addressed by this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@Tech-TX Tech-TX Apr 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TD-er

Maybe also good to test with a number of different ESP modules (ESP12E/F/S for example) to see if this also has an effect on deep sleep power consumption.
There have been a number of reports on deep sleep power consumption not being close to 60 uA on some nodes, with some reporting success with the most unclear assembly blobs possible and others claiming you need to do a short deep sleep first and then again call deep sleep to get true low power deep sleep and others reporting the WiFi will not start after deep sleep until you go into deep sleep again.

So I would not be surprised to see such topics pop up again if this is changed. (or this may be the final fix for it.... who knows ;) )

I didn't measure any difference in the Deep Sleep current. I looked at startup current and different types of Sleep. The only expected difference with other boards/modules would be due to extra chips on the boards (LDO, USB chip, battery charger, etc.), You can get several uA of difference if your GPIO outputs go the opposite direction from the pullup/pulldown resistors on those pins, ~2uA per pin in that case or 6-8uA higher total, but you pretty much have to have the pullup/pulldown resistors either on the module or in your circuit or it won't boot to Flash. The only time I've seen people reporting WAY high Deep Sleep numbers was 'cos they had a power LED on their board. Oops. Yeah, that's your circuit, not the ESP's Deep Sleep you're seeing.

The people that needed to reset across Deep Sleep a second time to re-enable WiFi after ESP.deepSleep(uS, WAKE_RF_DISABLED) have a fix here. The only wart is that it ALWAYS does a full RF_CAL when you wake the modem that way. I guess it blanks out the stored values for the modem parameters when you do WAKE_RF_DISABLED, so it has to rebuild everything from scratch when it brings the modem up next.

As far as variable current during Deep Sleep, I've never seen it mentioned. Most likely they weren't in Deep Sleep, for whatever reason. The SDK stops WiFi and all timers before entering Sleep, and once it's asleep the CPU PLL screeches to a complete halt and the power drops to around 20uA, which is solely the RTC timer running. I get closer to 16uA since I'm frequently running at 3.0V instead of 3.3V. If someone has an MCVE that demonstrates variable Deep Sleep currents I can figure out what's going on. I have a decent set of tools built or bought now.

I have seen numerous threads about Deep Sleep Zombie mode where it never wakes properly, but the jury is still out on the actual cause(s). I vote for cheap Chinese Flash that doesn't always come up in full drive mode after the Flash has been slept prior to Deep Sleep, but Erriez thinks it's the NodeMCU reset circuit interfering. We could both be right: two different causes or more. Until I get one here I can dissect, my tools are useless at troubleshooting across the Internet.

anywhere in the code (it is a weak void function intended to play with the
linker).

.. code:: cpp

void setup () {
#ifdef WIFI_IS_OFF_AT_BOOT
enableWiFiAtBoot(); // can be called from anywhere with the same effect
#endif
....
}

When legacy behavior is restored thanks to this call,
ESP8266 is able to reconnect to the last used WiFi network or establishes the same Access Point upon power up or reset.
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.
Frequently calling these functions could cause wear on the flash memory (see issue `#1054 <https://github.com/esp8266/Arduino/issues/1054>`__).
Expand Down
1 change: 1 addition & 0 deletions doc/esp8266wifi/readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ WiFi Multi
Example:

.. code:: cpp

#include <ESP8266WiFiMulti.h>

ESP8266WiFiMulti wifiMulti;
Expand Down
5 changes: 2 additions & 3 deletions doc/faq/a01-espcomm_sync-failed.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ following three things right: 1. Module is provided with enough power,
2. GPIO0, GPIO15 and CH\_PD are connected using pull up / pull down
resistors, 3. Module is put into boot loader mode.

For specific details please refer to section on `Generic ESP8266
modules <../boards.rst#generic-esp8266-modules>`__. Example modules
without USB to serial converter on board are shown below.
For specific details please refer to section on `Generic ESP8266 module <../boards.rst#generic-esp8266-module>`__.
Example modules without USB to serial converter on board are shown below.

.. figure:: pictures/a01-example-boards-without-usb.png
:alt: Example ESP8266 modules without USB to serial converter
Expand Down
2 changes: 1 addition & 1 deletion doc/faq/a04-board-generic-is-unknown.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ follows:
Error compiling for board Generic ESP8266 Module.

Below is an example messages for
`WeMos <../boards.rst#wemos-d1-r2-mini>`__:
`WeMos <../boards.rst#lolin-wemos-d1-r2-mini>`__:

::

Expand Down
9 changes: 9 additions & 0 deletions doc/faq/readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,12 @@ will need to implement an additional (short) deep sleep using
``WAKE_RF_DEFAULT``.

Ref. `#3072 <https://github.com/esp8266/Arduino/issues/3072>`__

My WiFi was previously automatically connected right after booting
d-a-v marked this conversation as resolved.
Show resolved Hide resolved
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This was WiFi persistence. Starting from version 3 of this core, WiFi is
indeed off at boot and is powered on only when starting to be used with the
regular API.

Read more at `former WiFi persistent mode <../esp8266wifi/generic-class.rst#persistent>`__.
16 changes: 8 additions & 8 deletions doc/filesystem.rst
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,8 @@ Performs the same operation as ``info`` but allows for reporting greater than
4GB for filesystem size/used/etc. Should be used with the SD and SDFS
filesystems since most SD cards today are greater than 4GB in size.

setTimeCallback(time_t (*cb)(void))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
setTimeCallback(time_t (\*cb)(void))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: cpp

Expand Down Expand Up @@ -574,8 +574,8 @@ rewind

Resets the internal pointer to the start of the directory.

setTimeCallback(time_t (*cb)(void))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
setTimeCallback(time_t (\*cb)(void))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sets the time callback for any files accessed from this Dir object via openNextFile.
Note that the SD and SDFS filesystems only support a filesystem-wide callback and
Expand Down Expand Up @@ -693,7 +693,7 @@ Close the file. No other operations should be performed on *File* object
after ``close`` function was called.

openNextFile (compatibiity method, not recommended for new code)
~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: cpp

Expand All @@ -705,7 +705,7 @@ Opens the next file in the directory pointed to by the File. Only valid
when ``File.isDirectory() == true``.

rewindDirectory (compatibiity method, not recommended for new code)
~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: cpp

Expand All @@ -718,8 +718,8 @@ rewindDirectory (compatibiity method, not recommended for new code)
Resets the ``openNextFile`` pointer to the top of the directory. Only
valid when ``File.isDirectory() == true``.

setTimeCallback(time_t (*cb)(void))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
setTimeCallback(time_t (\*cb)(void))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sets the time callback for this specific file. Note that the SD and
SDFS filesystems only support a filesystem-wide callback and calls to
Expand Down
2 changes: 1 addition & 1 deletion doc/libraries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Some ESP-specific APIs related to deep sleep, RTC and flash memories are availab

``ESP.getHeapFragmentation()`` returns the fragmentation metric (0% is clean, more than ~50% is not harmless)

``ESP.getMaxFreeBlockSize()`` returns the largest contiguous free RAM block in the heap, useful for checking heap fragmentation. **NOTE:** Maximum ``malloc()``able block will be smaller due to memory manager overheads.
``ESP.getMaxFreeBlockSize()`` returns the largest contiguous free RAM block in the heap, useful for checking heap fragmentation. **NOTE:** Maximum ``malloc()`` -able block will be smaller due to memory manager overheads.

``ESP.getChipId()`` returns the ESP8266 chip ID as a 32-bit integer.

Expand Down
2 changes: 0 additions & 2 deletions doc/mmu.rst
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,3 @@ address range of IRAM or DRAM.
uint8_t mmu_set_uint8(void *p8, const uint8_t val);
uint16_t mmu_set_uint16(uint16_t *p16, const uint16_t val);
int16_t mmu_set_int16(int16_t *p16, const int16_t val);

::
2 changes: 1 addition & 1 deletion doc/ota_updates/readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ If signing is desired, sign the gzip compressed file *after* compression.
Updating apps in the field to support compression
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you have applications deployed in the field and wish to update them to support compressed OTA uploads, you will need to first recompile the application, then _upload the uncompressed `.bin` file once_. Attempting to upload a `gzip` compressed binary to a legacy app will result in the Updater rejecting the upload as it does not understand the `gzip` format. After this initial upload, which will include the new bootloader and `Updater` class with compression support, compressed updates can then be used.
If you have applications deployed in the field and wish to update them to support compressed OTA uploads, you will need to first recompile the application, then _upload the uncompressed `.bin` file once. Attempting to upload a `gzip` compressed binary to a legacy app will result in the Updater rejecting the upload as it does not understand the `gzip` format. After this initial upload, which will include the new bootloader and `Updater` class with compression support, compressed updates can then be used.


Safety
Expand Down

This file was deleted.

5 changes: 0 additions & 5 deletions libraries/ESP8266WiFi/examples/WiFiShutdown/WiFiShutdown.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ WiFiState state;
const char* ssid = STASSID;
const char* password = STAPSK;

void preinit(void) {
// Make sure, wifi stays off after boot.
ESP8266WiFiClass::preinitWiFiOff();
}

void setup() {
Serial.begin(74880);
//Serial.setDebugOutput(true); // If you need debug output
Expand Down
26 changes: 4 additions & 22 deletions libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ struct WiFiEventHandlerOpaque

static std::list<WiFiEventHandler> sCbEventList;

bool ESP8266WiFiGenericClass::_persistent = true;
bool ESP8266WiFiGenericClass::_persistent = false;
WiFiMode_t ESP8266WiFiGenericClass::_forceSleepLastMode = WIFI_OFF;

ESP8266WiFiGenericClass::ESP8266WiFiGenericClass()
Expand Down Expand Up @@ -419,7 +419,7 @@ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m, WiFiState* state) {
}

if (wifi_fpm_get_sleep_type() != NONE_SLEEP_T) {
// wifi may have been put asleep by ESP8266WiFiGenericClass::preinitWiFiOff
// wifi starts asleep by default
Copy link
Collaborator

@mcspr mcspr Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at esp32's ::mode:
https://github.com/espressif/arduino-esp32/blob/46d5afb17fb91965632dc5fef237117e1fe947fc/libraries/WiFi/src/WiFiGeneric.cpp#L532-L542

Should this be more in-line with that?
i.e.

  • remove this forced modem up, it happens even if we don't change the mode
  • getMode() checks for fpm -> returns null-mode when sleeping
  • mode() consecutively turns off the wifi when WIFI_OFF without an additional call to force sleep and turns it back on as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thanks !

I added 1) and 3).
I'm reticent about 2) because WiFi.mode(WIFI_OFF) will not put the modem to sleep, or puting modem to sleep does not necessarily switches the mode to WIFI_OFF. 2) should come with that no ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant exactly that, to do forcesleep with WiFi.mode(WIFI_OFF) and resume with any real mode.
Not noticed the special shutdown and resume modes though, I wonder if those should be separate methods instead and not a 'mode'?

Copy link
Collaborator Author

@d-a-v d-a-v Mar 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are indeed "fake" modes and described as such somewhere. There are two examples making use of them.
They are designed to import the deep sleep procedure into the core and allow users to not use random fw-snippets to put the chip to sleep (and wake-up fast)

Copy link
Collaborator

@mcspr mcspr Mar 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aaah, both are already implemented as methods. So examples could use those directly?

wifi_fpm_do_wakeup();
wifi_fpm_close();
}
Expand Down Expand Up @@ -855,25 +855,7 @@ bool ESP8266WiFiGenericClass::resumeFromShutdown (WiFiState* state)
return true;
}

//meant to be called from user-defined ::preinit()
void ESP8266WiFiGenericClass::preinitWiFiOff () {
// https://github.com/esp8266/Arduino/issues/2111#issuecomment-224251391
// WiFi.persistent(false);
// WiFi.mode(WIFI_OFF);
// WiFi.forceSleepBegin();

//WiFi.mode(WIFI_OFF) equivalent:
// datasheet:
// Set Wi-Fi working mode to Station mode, SoftAP
// or Station + SoftAP, and do not update flash
// (not persistent)
wifi_set_opmode_current(WIFI_OFF);

//WiFi.forceSleepBegin(/*default*/0) equivalent:
// sleep forever until wifi_fpm_do_wakeup() is called
wifi_fpm_set_sleep_type(MODEM_SLEEP_T);
wifi_fpm_open();
wifi_fpm_do_sleep(0xFFFFFFF);

// use WiFi.forceSleepWake() to wake WiFi up
// It was meant to be called from user-defined ::preinit()
// It is now deprecated by enableWiFiAtBootTime() and __disableWiFiAtBootTime()
}
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class ESP8266WiFiGenericClass {

static uint32_t shutdownCRC (const WiFiState* state);
static bool shutdownValidCRC (const WiFiState* state);
static void preinitWiFiOff (); //meant to be called in user-defined preinit()
static void preinitWiFiOff () __attribute__((deprecated("WiFi is off by default at boot, use enableWiFiAtBoot() for legacy behavior")));

protected:
static bool _persistent;
Expand Down
6 changes: 0 additions & 6 deletions libraries/esp8266/examples/SerialStress/SerialStress.ino
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ void error(const char* what) {
}
}

void preinit() {
// (no C++ in function)
// disable wifi
ESP8266WiFiClass::preinitWiFiOff();
}

void setup() {
pinMode(LED_BUILTIN, OUTPUT);

Expand Down
6 changes: 0 additions & 6 deletions libraries/esp8266/examples/UartDownload/UartDownload.ino
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,6 @@ void proxyEspSync() {
//
////////////////////////////////////////////////////////////////////////////////

void preinit() {
// (no C++ in function)
// disable wifi
ESP8266WiFiClass::preinitWiFiOff();
}

void setup() {
// For `proxyEspSync()` to work, the Serial.begin() speed needs to be
// 115200bps. This is the data rate used by esptool.py. It expects the Boot
Expand Down
Loading