diff --git a/docs/guides/wifi.md b/docs/guides/wifi.md index d719ea74..1c187cd4 100644 --- a/docs/guides/wifi.md +++ b/docs/guides/wifi.md @@ -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`: diff --git a/lib/wifi_manager.py b/lib/wifi_manager.py index 10d11810..263e32a1 100644 --- a/lib/wifi_manager.py +++ b/lib/wifi_manager.py @@ -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 @@ -512,8 +513,8 @@ 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 @@ -521,30 +522,6 @@ def _apply_power_save(): 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: diff --git a/tests/test_wifi_manager.py b/tests/test_wifi_manager.py index adc7fc5a..5a340d58 100644 --- a/tests/test_wifi_manager.py +++ b/tests/test_wifi_manager.py @@ -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()