From 189889489b4c6307b681f727bbe6ce714706c89d Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Tue, 17 Aug 2021 16:58:21 -0700 Subject: [PATCH] disable bluetooth while using wifi (esp32 drops networks otherwise) --- geeksville-private/TODO.md | 5 ++++- src/main.cpp | 5 +++-- src/mesh/http/WiFiAPClient.cpp | 33 ++++++++------------------------- src/mesh/http/WiFiAPClient.h | 4 +++- src/nimble/BluetoothUtil.cpp | 28 +++++----------------------- src/wifi-stubs.cpp | 4 +++- 6 files changed, 26 insertions(+), 53 deletions(-) diff --git a/geeksville-private/TODO.md b/geeksville-private/TODO.md index 6d36d09db..e4916490f 100644 --- a/geeksville-private/TODO.md +++ b/geeksville-private/TODO.md @@ -2,8 +2,11 @@ You probably don't care about this section - skip to the next one. -* send debug info 'in-band' +* disable bluetooth while wifi is active +* failed adding https service * fix wifi connections for mqtt +* send debug info 'in-band' + * usb lora dongle from pine64 * turn on watchdog reset if app hangs on nrf52 or esp32 * list portduino on platformio diff --git a/src/main.cpp b/src/main.cpp index 3e845e22d..e28b7fd91 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -337,9 +337,10 @@ void setup() digitalWrite(RESET_OLED, 1); #endif + bool forceSoftAP = 0; + #ifdef BUTTON_PIN #ifndef NO_ESP32 - bool forceSoftAP = 0; // If the button is connected to GPIO 12, don't enable the ability to use // meshtasticAdmin on the device. @@ -536,10 +537,10 @@ void setup() } #endif -#ifndef NO_ESP32 // Initialize Wifi initWifi(forceSoftAP); +#ifndef NO_ESP32 // Start web server thread. webServerThread = new WebServerThread(); #endif diff --git a/src/mesh/http/WiFiAPClient.cpp b/src/mesh/http/WiFiAPClient.cpp index ed4cd3558..0d8caa3b3 100644 --- a/src/mesh/http/WiFiAPClient.cpp +++ b/src/mesh/http/WiFiAPClient.cpp @@ -81,15 +81,6 @@ bool isWifiAvailable() const char *wifiName = radioConfig.preferences.wifi_ssid; - // strcpy(radioConfig.preferences.wifi_ssid, "meshtastic"); - // strcpy(radioConfig.preferences.wifi_password, "meshtastic!"); - - // strcpy(radioConfig.preferences.wifi_ssid, "meshtasticAdmin"); - // strcpy(radioConfig.preferences.wifi_password, "12345678"); - - // radioConfig.preferences.wifi_ap_mode = true; - // radioConfig.preferences.wifi_ap_mode = false; - if (*wifiName) { return true; } else { @@ -119,26 +110,16 @@ void deinitWifi() } // Startup WiFi -void initWifi(bool forceSoftAP) +bool initWifi(bool forceSoftAP) { - - if (forceSoftAP) { - // do nothing - // DEBUG_MSG("----- Forcing SoftAP\n"); - } else { - if (isWifiAvailable() == 0) { - return; - } - } - forcedSoftAP = forceSoftAP; - createSSLCert(); - - if (radioConfig.has_preferences || forceSoftAP) { + if ((radioConfig.has_preferences && radioConfig.preferences.wifi_ssid) || forceSoftAP) { const char *wifiName = radioConfig.preferences.wifi_ssid; const char *wifiPsw = radioConfig.preferences.wifi_password; + createSSLCert(); + if (!*wifiPsw) // Treat empty password as no password wifiPsw = NULL; @@ -215,9 +196,11 @@ void initWifi(bool forceSoftAP) DEBUG_MSG("mDNS Host: Meshtastic.local\n"); MDNS.addService("http", "tcp", 80); MDNS.addService("https", "tcp", 443); - - } else + return true; + } else { DEBUG_MSG("Not using WIFI\n"); + return false; + } } // Called by the Espressif SDK to diff --git a/src/mesh/http/WiFiAPClient.h b/src/mesh/http/WiFiAPClient.h index cb27bb4a8..acbbdb19b 100644 --- a/src/mesh/http/WiFiAPClient.h +++ b/src/mesh/http/WiFiAPClient.h @@ -9,7 +9,9 @@ #include #endif -void initWifi(bool forceSoftAP); +/// @return true if wifi is now in use +bool initWifi(bool forceSoftAP); + void deinitWifi(); bool isWifiAvailable(); diff --git a/src/nimble/BluetoothUtil.cpp b/src/nimble/BluetoothUtil.cpp index 0618c73e6..d36541e57 100644 --- a/src/nimble/BluetoothUtil.cpp +++ b/src/nimble/BluetoothUtil.cpp @@ -539,7 +539,6 @@ void reinitBluetooth() } bool bluetoothOn; -bool firstTime = 1; // Enable/disable bluetooth. void setBluetoothEnable(bool on) @@ -549,32 +548,15 @@ void setBluetoothEnable(bool on) bluetoothOn = on; if (on) { - Serial.printf("Pre BT: %u heap size\n", ESP.getFreeHeap()); - // ESP_ERROR_CHECK( heap_trace_start(HEAP_TRACE_LEAKS) ); - reinitBluetooth(); - - // Don't try to reconnect wifi before bluetooth is configured. - // WiFi is initialized from main.cpp in setup() . - if (firstTime) { - firstTime = 0; - } else { -#ifndef NO_ESP32 - initWifi(0); -#endif + if (!initWifi(0)) // if we are using wifi, don't turn on bluetooth also + { + Serial.printf("Pre BT: %u heap size\n", ESP.getFreeHeap()); + // ESP_ERROR_CHECK( heap_trace_start(HEAP_TRACE_LEAKS) ); + reinitBluetooth(); } } else { - - /* - // If WiFi is in use, disable shutting down the radio. - if (isWifiAvailable()) { - return; - } - */ - // shutdown wifi -#ifndef NO_ESP32 deinitWifi(); -#endif // We have to totally teardown our bluetooth objects to prevent leaks deinitBLE(); diff --git a/src/wifi-stubs.cpp b/src/wifi-stubs.cpp index 3d110c723..87470c528 100644 --- a/src/wifi-stubs.cpp +++ b/src/wifi-stubs.cpp @@ -5,7 +5,9 @@ //#include "mesh/wifi/WiFiAPClient.h" -void initWifi(bool forceSoftAP) {} +bool initWifi(bool forceSoftAP) { + return false; +} void deinitWifi() {}