diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index a0c702ebc3..64082e6dcf 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -174,7 +174,8 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) { } // Identify if the gateway is enabled or not and stop start accordingly if (BTdata.containsKey("enabled") && BTdata["enabled"] == false && BTConfig.enabled == true) { - stopProcessing(); + // Stop the gateway but without deinit to enable a future BT restart + stopProcessing(false); } else if (BTdata.containsKey("enabled") && BTdata["enabled"] == true && BTConfig.enabled == false) { BTProcessLock = false; setupBTTasksAndBLE(); @@ -790,7 +791,7 @@ void BLEconnect() { void BLEconnect() {} # endif -void stopProcessing() { +void stopProcessing(bool deinit) { if (BTConfig.enabled) { BTProcessLock = true; // We stop the scan @@ -809,6 +810,9 @@ void stopProcessing() { vTaskDelete(xProcBLETaskHandle); xSemaphoreGive(semaphoreBLEOperation); } + // Using deinit to free memory, should only be used if we are going to restart the gateway + if (deinit) + BLEDevice::deinit(true); } Log.notice(F("BLE gateway stopped, free heap: %d" CR), ESP.getFreeHeap()); } diff --git a/main/config_BT.h b/main/config_BT.h index 774fe2831d..9d90ecd5fd 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -29,7 +29,7 @@ extern void setupBT(); extern void XtoBT(const char* topicOri, JsonObject& RFdata); extern void launchBTDiscovery(bool overrideDiscovery); -extern void stopProcessing(); +extern void stopProcessing(bool deinit); extern String stateBTMeasures(bool); #ifdef ESP32 diff --git a/main/main.ino b/main/main.ino index 48b231ec31..4958e43155 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1662,7 +1662,7 @@ void setOTA() { #ifdef ESP32 ProcessLock = true; # ifdef ZgatewayBT - stopProcessing(); + stopProcessing(true); # endif #endif lpDisplayPrint("OTA in progress"); @@ -2455,7 +2455,7 @@ void sleep() { # endif Log.trace(F("Deactivating ESP32 components" CR)); # ifdef ZgatewayBT - stopProcessing(); + stopProcessing(true); ProcessLock = true; # endif # pragma GCC diagnostic push @@ -3199,10 +3199,10 @@ void MQTTHttpsFWUpdate(const char* topicOri, JsonObject& HttpsFwUpdateData) { # ifdef ESP32 ProcessLock = true; # ifdef ZgatewayBT - stopProcessing(); + stopProcessing(true); # endif # endif - Log.warning(F("Starting firmware update" CR)); + Log.warning(F("Starting firmware update with %d freeHeap" CR), ESP.getFreeHeap()); gatewayState = GatewayState::REMOTE_OTA_IN_PROGRESS; StaticJsonDocument jsondata; @@ -3244,11 +3244,9 @@ void MQTTHttpsFWUpdate(const char* topicOri, JsonObject& HttpsFwUpdateData) { mqtt.reset(); mqttSetupPending = true; update_client = *static_cast(eClient.get()); - } else -# endif - { - TheengsUtils::syncNTP(); } +# endif + TheengsUtils::syncNTP(); # ifdef ESP32 update_client.setCACert(ota_cert.c_str()); @@ -3381,7 +3379,7 @@ void XtoSYS(const char* topicOri, JsonObject& SYSdata) { // json object decoding #ifdef ESP32 ProcessLock = true; # ifdef ZgatewayBT - stopProcessing(); + stopProcessing(true); # endif #endif String prev_ssid = WiFi.SSID(); @@ -3527,7 +3525,7 @@ void XtoSYS(const char* topicOri, JsonObject& SYSdata) { // json object decoding # ifdef ESP32 ProcessLock = true; # ifdef ZgatewayBT - stopProcessing(); + stopProcessing(true); # endif # endif