Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 6 additions & 2 deletions docs/guides/wifi.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,12 @@ wifi_manager.set_power_save(True) # sleeps between beacons: saves battery
wifi_manager.set_power_save(None) # back to the default
```

While Bluetooth is on, power saving stays on: an ESP32 shares its antenna
between the two by putting Wi-Fi to sleep.
The same goes with Bluetooth on. ESP-IDF shares the radio between Wi-Fi and
Bluetooth in time slices, with power saving or without. On a classic ESP32
running as a Bluetooth speaker, with power saving on it lost 83% of pings,
and its page took up to 11 s to load or didn't load at all. With it off, it
lost no pings, the page loaded in under 2 s every time, and the music played
just as cleanly.

For a change that lasts only until the next join, set the radio directly,
on CircuitPython or on MicroPython with OmniPython's `wifi`:
Expand Down
33 changes: 5 additions & 28 deletions lib/wifi_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@
with a battery fitted (its ``battery`` role reads a voltage).
``set_power_save(True)`` or ``set_power_save(False)`` overrides that and is
remembered with the networks; ``set_power_save(None)`` goes back to the
default, and ``power_save()`` says which applies. It is left on while
Bluetooth runs, which shares the antenna by putting Wi-Fi to sleep.
default, and ``power_save()`` says which applies. Bluetooth doesn't change
the choice: while it runs, the radio still shares its time between the two
whether Wi-Fi may sleep or not.

``connect(ntp=True)`` sets the clock from NTP once a network is joined.
``connect(wait=False)`` starts the join and returns at once, for an app that
Expand Down Expand Up @@ -512,39 +513,15 @@ def _apply_power_save():
import wifi

on = power_save()
if not on and _bluetooth_on():
return # Bluetooth needs the Wi-Fi side to sleep to share the antenna
# The same with Bluetooth running: ESP-IDF's coexistence gives Wi-Fi
# and Bluetooth their own time slices, with power saving or without.
mode = wifi.PowerManagement.MIN if on else wifi.PowerManagement.NONE
if wifi.radio.power_management != mode:
wifi.radio.power_management = mode
except Exception as exc:
print("wifi_manager: can't set power saving:", exc)


def _bluetooth_on():
"""True when this board's Bluetooth radio is running."""
try:
import bluetooth

if bluetooth.BLE().active():
return True
except Exception:
pass # no BLE in this firmware
try:
import btclassic

if btclassic.Bluetooth().active():
return True
except Exception:
pass # no Bluetooth Classic in this firmware
try:
import _bleio # CircuitPython

return bool(_bleio.adapter.enabled)
except Exception:
return False


def _has_battery():
"""True when the board's ``battery`` role reads a fitted battery."""
try:
Expand Down
9 changes: 7 additions & 2 deletions tests/test_wifi_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,13 +506,18 @@ def test_a_battery_role_with_no_battery_fitted_keeps_it_awake(self):
self._connect(wlan)
self.assertEqual(wlan.pm, 0)

def test_bluetooth_on_leaves_the_radio_to_sleep(self):
def test_bluetooth_on_still_keeps_it_awake(self):
# A board on a cable with Bluetooth running: an ESP32 Bluetooth
# speaker serving a page. Coexistence shares the radio either way.
ble = types.ModuleType("bluetooth")
ble.BLE = lambda: types.SimpleNamespace(active=lambda: True)
sys.modules["bluetooth"] = ble
classic = types.ModuleType("btclassic")
classic.Bluetooth = lambda: types.SimpleNamespace(active=lambda: True)
sys.modules["btclassic"] = classic
wlan = FakeWLAN()
self.assertEqual(self._connect(wlan), "10.0.0.77")
self.assertEqual((wlan.pm, wlan.pm_sets), (1, 0))
self.assertEqual(wlan.pm, 0)

def test_the_choice_is_remembered_and_beats_the_default(self):
wlan = FakeWLAN()
Expand Down
Loading