|
| 1 | +/* |
| 2 | + Starting and restarting BT is a problem |
| 3 | + https://github.com/espressif/arduino-esp32/issues/2718 |
| 4 | +
|
| 5 | + To work around the bug without modifying the core we create our own btStop() function with |
| 6 | + the patch from github |
| 7 | +
|
| 8 | + Heap readings: |
| 9 | + 238148 - Sketch start |
| 10 | + 140932 - BT start |
| 11 | + 238148 - BT end |
| 12 | + 186916 - WiFi start |
| 13 | + 223224 - WiFi end - There is a ~15k RAM leak here |
| 14 | + 126016 - BT re-start |
| 15 | +*/ |
| 16 | + |
| 17 | +#include <WiFi.h> |
| 18 | +WiFiClient caster; |
| 19 | + |
| 20 | +#include "BluetoothSerial.h" |
| 21 | +BluetoothSerial SerialBT; |
| 22 | + |
| 23 | +#include "esp_bt.h" |
| 24 | +#include "esp_wifi.h" |
| 25 | + |
| 26 | +void setup() { |
| 27 | + Serial.begin(115200); |
| 28 | + delay(100); |
| 29 | + |
| 30 | + Serial.print("ESP32 SDK: "); |
| 31 | + Serial.println(ESP.getSdkVersion()); |
| 32 | + Serial.println("Press the button to select the next mode"); |
| 33 | + |
| 34 | + Serial.printf("freeHeap: %d\n\r", ESP.getFreeHeap()); |
| 35 | +} |
| 36 | + |
| 37 | +void loop() { |
| 38 | + |
| 39 | + if (Serial.available()) |
| 40 | + { |
| 41 | + byte incoming = Serial.read(); |
| 42 | + |
| 43 | + Serial.println(); |
| 44 | + Serial.println("1) Start Bluetooth"); |
| 45 | + Serial.println("2) End Bluetooth"); |
| 46 | + Serial.println("3) Start Wifi"); |
| 47 | + Serial.println("4) End Wifi"); |
| 48 | + |
| 49 | + if (incoming == '1') |
| 50 | + { |
| 51 | + if (SerialBT.begin("Surveyor Name") == false) |
| 52 | + Serial.println("Failed BT start"); |
| 53 | + else |
| 54 | + { |
| 55 | + Serial.print("Bluetooth broadcasting as: "); |
| 56 | + Serial.println("Surveyor Name"); |
| 57 | + } |
| 58 | + } |
| 59 | + else if (incoming == '2') |
| 60 | + { |
| 61 | + Serial.println(F("Stopping Bluetooth")); |
| 62 | + customBTstop(); //Gracefully turn off Bluetooth so we can turn it back on if needed |
| 63 | + } |
| 64 | + else if (incoming == '3') |
| 65 | + { |
| 66 | + wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); |
| 67 | + esp_wifi_init(&wifi_init_config); //Restart WiFi resources |
| 68 | + |
| 69 | + Serial.printf("Connecting to local WiFi: %s\n", "sparkfun-guest"); |
| 70 | + WiFi.begin("TRex", "parachutes"); |
| 71 | + //WiFi.begin("sparkfun-guest", "sparkfun6333"); |
| 72 | + } |
| 73 | + else if (incoming == '4') |
| 74 | + { |
| 75 | + Serial.println("Stopping Wifi"); |
| 76 | + caster.stop(); |
| 77 | + WiFi.mode(WIFI_OFF); |
| 78 | + esp_wifi_deinit(); //Free all resources |
| 79 | + } |
| 80 | + |
| 81 | + Serial.printf("freeHeap: %d\n\r", ESP.getFreeHeap()); |
| 82 | + } |
| 83 | + |
| 84 | + delay(250); |
| 85 | +} |
| 86 | + |
| 87 | +//Starting and restarting BT is a problem. See issue: https://github.com/espressif/arduino-esp32/issues/2718 |
| 88 | +//To work around the bug without modifying the core we create our own btStop() function with |
| 89 | +//the patch from github |
| 90 | +bool customBTstop() { |
| 91 | + |
| 92 | + SerialBT.flush(); //Complete any transfers |
| 93 | + SerialBT.disconnect(); //Drop any clients |
| 94 | + SerialBT.end(); //SerialBT.end() will release significant RAM (~100k!) but a SerialBT.start will crash. |
| 95 | + //The follow code releases the BT hardware so that it can be restarted with a SerialBT.begin |
| 96 | + |
| 97 | + if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) { |
| 101 | + if (esp_bt_controller_disable()) { |
| 102 | + log_e("BT Disable failed"); |
| 103 | + return false; |
| 104 | + } |
| 105 | + while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED); |
| 106 | + } |
| 107 | + if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) |
| 108 | + { |
| 109 | + log_i("inited"); |
| 110 | + if (esp_bt_controller_deinit()) |
| 111 | + { |
| 112 | + log_e("BT deint failed"); |
| 113 | + return false; |
| 114 | + } |
| 115 | + while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) |
| 116 | + ; |
| 117 | + return true; |
| 118 | + } |
| 119 | + log_e("BT Stop failed"); |
| 120 | + return false; |
| 121 | +} |
0 commit comments