From b4b87a8d0a88e0c7e84683fa88a3b57ecdbb1828 Mon Sep 17 00:00:00 2001 From: Roman Bazalevsky Date: Sat, 14 Nov 2020 13:09:48 +0300 Subject: [PATCH] Delay ESP32 Bluetooth initialization after WiFi init/set sleep mode. --- tasmota/xsns_52_ibeacon.ino | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/tasmota/xsns_52_ibeacon.ino b/tasmota/xsns_52_ibeacon.ino index 7ae8c4921..db5c5951e 100755 --- a/tasmota/xsns_52_ibeacon.ino +++ b/tasmota/xsns_52_ibeacon.ino @@ -275,13 +275,21 @@ void ESP32ResumeScanTask() { AddLog_P(LOG_LEVEL_DEBUG, PSTR("%s: Resumed scanner task"),"BLE"); } -#endif +void ESP32Init() { -void IBEACON_Init() { + if (TasmotaGlobal.global_state.wifi_down) { return; } -#ifdef USE_IBEACON_ESP32 + if (WiFi.getSleep() == false) { + if (0 == Settings.flag3.sleep_normal) { + AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: About to restart to put WiFi modem in sleep mode"),"BLE"); + Settings.flag3.sleep_normal = 1; // SetOption60 - Enable normal sleep instead of dynamic sleep + TasmotaGlobal.restart_flag = 2; + } + return; + } + + AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: Initializing Blueetooth..."),"BLE"); - ESP32BLE.mode.init = false; if (!ESP32BLE.mode.init) { NimBLEDevice::init(""); @@ -293,6 +301,17 @@ void IBEACON_Init() { IB_TIMEOUT_TIME=IB_TIMEOUT_INTERVAL; } +} + +#endif + +void IBEACON_Init() { + +#ifdef USE_IBEACON_ESP32 + + ESP32BLE.mode.init = false; + ESP32BLE.mode.runningScan = false; + #else hm17_found=0; @@ -320,6 +339,8 @@ void IBEACON_Init() { void esp32_every_second(void) { + if (!ESP32BLE.mode.init) { return; } + if (TasmotaGlobal.ota_state_flag) { if (ESP32BLE.mode.runningScan) { AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: Upgrade procedure started"),"BLE"); @@ -953,6 +974,13 @@ bool Xsns52(byte function) case FUNC_INIT: IBEACON_Init(); break; +#ifdef USE_IBEACON_ESP32 + case FUNC_EVERY_250_MSECOND: + if (!ESP32BLE.mode.init) { + ESP32Init(); + } + break; +#endif case FUNC_LOOP: IBEACON_loop(); break;