-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
ports/esp32-esp8266: Add support for set/get the wifi power saving mode. #8993
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
Conversation
This sounds similar to functionality in the esp module. Please could you explain the relationship between the two. |
@peterhinch Good pickup - thanks Peter. I was not aware this functionality was available in the esp module (ESP8266 only). I confess that I find it odd that this one wifi option is set in the esp module and everything else is set in WLAN.config(). It is hardly surprising that I and other users have overlooked this till now. Nonetheless, precedent is an important factor. I guess I could add support for the ESP32 to ports/esp32/modesp.c. However, I really do think users are going to continue to overlook this feature if it is not in WLAN.config(). Thoughts? |
I think settings should be consolidated to But we also need to standardise on naming for setting the power saving mode. In the CYW43 driver it uses |
@dpgeorge Ok - I can add a commit renaming the option to "pm" to be consistent with the cyw43 driver. I am assuming it is too difficult to attempt to standardise the values (and constant names) for those arguments as the semantics are likely to vary considerably across devices (even between the esp32 and esp8266). |
I think the constants need to go inside the WLAN class itself. Because if there are multiple NICs then they could all have different options/values for the power saving configuration. I would suggest naming the constants |
Ok. Shall do. @dpgeorge : This may start to create a consistency issue with respect to many existing constants in the network module which would by the same logic belong in the WLAN class. I'm very happy that we can start with just this set of constants (which I agree makes sense in the light of wider network adaptor support in micropython) - just flagging that it will seem a bit arbitrary to users which constants are in network and which in WLAN(). |
a10101a
to
1a93ea6
Compare
I have rebased against master and pushed a commit to implement (as requested):
for esp32 and esp8266. Update - and squashed commits. |
11cb259
to
595f4d4
Compare
Whoops - I forgot to rename the PS_ constants on esp32. Fixed now. |
595f4d4
to
67b8547
Compare
67b8547
to
bc15dc4
Compare
Any updates on this one? |
No updates. Just waiting for further review. |
FWIW I patched this and it worked great; my ping times go from 100ms to 2-3ms. Can you update the first comment to show the current configuration/code example, it should be: import network |
@dakoner - Glad it worked for you and thanks for the tips to fix the docs. Done. |
This is just what I want and searched, because WebREPL is unbearably slow in the current situation. I wonder why it's still not committed... |
d09e762
to
98e31b8
Compare
Rebased against master to resolve conflicts. |
Does this PR allow wifi/bt modems to be completely switched off as described as "modem sleep" here? |
This looks like the PS_MIN_MODEM mode described here. That wifi ps mode is automatically enabled when the esp32 connects to a wifi access point. You can use this PR to doable this power saving mode or select the high power savings of PS_MAX_MODEM. Note that in these modes, the radio is only turned off intermittently (set by the access points DTIM interval). |
Thanks @glenn20 I will test this out with a power meter. Reading further I think what I need to call is esp_wifi_stop(); |
You can see the results of some of my experiments with optimising wifi power consumption. In particular, the wifi graphs show clearly the steps in power consumption in PS_MIN_MODEM mode as the radio is toggled on and off in sync with the Access Point's DTIM timing. |
Wow some great work there Glenn, the optimisations are impressive. |
The ESP32 implements only the wifi modem-sleep mode as described at ESP32 Wifi Power Saving Mode:
which "conceptually" map to the modes in the cyw43 driver. (Note that the ESP32 automatically enters However, looking at the docs for The signature for the "pm" config option in this PR is compatible with the cyw43 driver, BUT the meaning of the values of the arg are different. Are you thinking to find a common base naming convention for a few constants that map to the port/device specific settings (eg. Also, we could consider encoding the "listen interval" into the "pm" argument like |
Aside: The cyw43 docs suggest that DEFAULT_PM saves more power than AGGRESSIVE_PM, which is not what I "expected" from the naming convention. |
At the very least, yes.
I would like to do this. Eg there could be a few options like
In most cases users would just do |
Yes, the naming of the default constant is poor. We can change that. |
I like this. You get a simple, portable method for setting common wifi PM settings and an extension api for tuning (which may be supported by only some drivers and may have different signatures and semantics on different drivers). (Pauses to think....) However, is this the cleanest method? If a
|
Yes, I like that idea. |
Ok. Just need a naming convention for portable constants. I suggest a "none", "default" and 2-3 power level settings, eg: PM_NONE and PM_DEFAULT as well as something like:
PM_NONE - disable power saving Let me know what you like. I'll update this PR with performance/powersave/default/none but can change at any time. I'll also investigate supporting the "listen interval". If I get the time this weekend, I'll test with the PPK2. |
47593ed
to
03bd089
Compare
Now supports: WLAN.config(pm=WLAN.PM_NONE) # WIFI_PS_NONE
WLAN.config(pm=WLAN.PM_DEFAULT) # WIFI_PS_MIN_MODEM
WLAN.config(pm=WLAN.PM_PERFORMANCE) # WIFI_PS_MIN_MODEM
WLAN.config(pm=WLAN.PM_POWERSAVE) # WIFI_PS_MAX_MODEM
WLAN.config(pm=(WLAN.PM_POWERSAVE, listen_interval)) # WIFI_PS_MAX_MODEM and set listen_address parameter in sta config. Happy to change-up constant names or anything else. It might be helpful to have standardised named for 3 PM_ levels (this scheme only has 2). |
06b85f9
to
771dedc
Compare
9cced64
to
b1fff6a
Compare
b1fff6a
to
4f56f71
Compare
For esp32 and esp8266 this commit adds: - a 'pm' option to WLAN.config() to set/get the wifi power saving mode; and - PM_NONE, PM_PERFORMANCE and PM_POWERSAVE constants to the WLAN class. This API should be general enough to use with all WLAN drivers. Documentation is also added.
4f56f71
to
1093dea
Compare
Thanks, that looks better. I squashed and rebased this PR. |
Merged! A concise PR is always best. |
Thank you for fixing this! This one line: replaces my entire "workaround" on ESP8266 for ESP-NOW:
I noticed, that while you ping ESP8266, it can receive ESP-NOW messages, but once you stop, power saving mode kicks in and packets be gone. |
You can simplify that one line to:
|
That does not work anymore - since network.WLAN was replaced by a function to return the WLAN instances rather than being a class in the network module. I usually use: sta = network.WLAN(network.STA_IF)
sta.config(pm=sta.PM_NONE) |
Oh, you're right! And that's been there since the beginning, originating on esp8266. That should definitely be fixed, to match other ports and network classes. |
See #11465. |
Disable warnings during REPL autocomplete
This PR adds support for setting and getting the wifi power saving mode.
Adds the
pm
option toWLAN.config()
to support setting/getting the wifi power saving mode.Also adds the
PM_NONE
,PM_MIN_MODEM
andPM_MAX_MODEM
constants to theWLAN
class.Example:
This is especially useful for controlling power consumption on battery powered devices and is also required by PR #6515 (ESPNow support).