Skip to content

Commit 338fc43

Browse files
authored
[OTA] Free memory by deleting BT tasks when launching OTA (#1573)
* [OTA] Free memory by deleting BT taskswhen launching OTA and restart if fail Also factorize restart function for ESP
1 parent d1e9e8b commit 338fc43

File tree

2 files changed

+21
-48
lines changed

2 files changed

+21
-48
lines changed

main/ZgatewayBT.ino

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -801,12 +801,11 @@ void stopProcessing() {
801801
Log.notice(F("Stop BLE processing" CR));
802802
ProcessLock = true;
803803
delay(BTConfig.scanDuration < 2000 ? BTConfig.scanDuration : 2000);
804-
}
805-
806-
void startProcessing() {
807-
Log.notice(F("Start BLE processing" CR));
808-
ProcessLock = false;
809-
vTaskResume(xCoreTaskHandle);
804+
//Suspending and deleting tasks to free memory for OTA operations
805+
vTaskSuspend(xCoreTaskHandle);
806+
vTaskDelete(xCoreTaskHandle);
807+
vTaskSuspend(xProcBLETaskHandle);
808+
vTaskDelete(xProcBLETaskHandle);
810809
}
811810

812811
void coreTask(void* pvParameters) {
@@ -847,7 +846,6 @@ void coreTask(void* pvParameters) {
847846
}
848847
} else {
849848
Log.trace(F("BLE core task canceled by processLock" CR));
850-
vTaskSuspend(xCoreTaskHandle);
851849
}
852850
}
853851
}

main/main.ino

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,10 +1068,8 @@ void setOTA() {
10681068
last_ota_activity_millis = 0;
10691069
ErrorIndicatorOFF();
10701070
SendReceiveIndicatorOFF();
1071-
# if defined(ZgatewayBT) && defined(ESP32)
1072-
startProcessing();
1073-
# endif
10741071
lpDisplayPrint("OTA done");
1072+
ESPRestart();
10751073
});
10761074
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
10771075
Log.trace(F("Progress: %u%%\r" CR), (progress / (total / 100)));
@@ -1081,9 +1079,6 @@ void setOTA() {
10811079
last_ota_activity_millis = millis();
10821080
ErrorIndicatorOFF();
10831081
SendReceiveIndicatorOFF();
1084-
# if defined(ZgatewayBT) && defined(ESP32)
1085-
startProcessing();
1086-
# endif
10871082
Serial.printf("Error[%u]: ", error);
10881083
if (error == OTA_AUTH_ERROR)
10891084
Log.error(F("Auth Failed" CR));
@@ -1095,6 +1090,7 @@ void setOTA() {
10951090
Log.error(F("Receive Failed" CR));
10961091
else if (error == OTA_END_ERROR)
10971092
Log.error(F("End Failed" CR));
1093+
ESPRestart();
10981094
});
10991095
ArduinoOTA.begin();
11001096
}
@@ -1148,6 +1144,14 @@ void setupTLS(bool self_signed, uint8_t index) {
11481144
}
11491145
#endif
11501146

1147+
void ESPRestart() {
1148+
#if defined(ESP32)
1149+
ESP.restart();
1150+
#elif defined(ESP8266)
1151+
ESP.reset();
1152+
#endif
1153+
}
1154+
11511155
#if defined(ESPWifiManualSetup)
11521156
void setup_wifi() {
11531157
WiFi.mode(WIFI_STA);
@@ -1814,9 +1818,6 @@ void syncNTP() {
18141818

18151819
if (count >= 60) {
18161820
Log.error(F("Unable to update - invalid time" CR));
1817-
# if defined(ZgatewayBT) && defined(ESP32)
1818-
startProcessing();
1819-
# endif
18201821
return;
18211822
}
18221823
}
@@ -2276,20 +2277,14 @@ void MQTTHttpsFWUpdate(char* topicOri, JsonObject& HttpsFwUpdateData) {
22762277
# ifndef ESPWifiManualSetup
22772278
saveMqttConfig();
22782279
# endif
2279-
# ifdef ESP32
2280-
ESP.restart();
2281-
# elif ESP8266
2282-
ESP.reset();
2283-
# endif
2280+
ESPRestart();
22842281
break;
22852282
}
22862283

22872284
SendReceiveIndicatorOFF();
22882285
ErrorIndicatorOFF();
22892286

2290-
# if defined(ZgatewayBT) && defined(ESP32)
2291-
startProcessing();
2292-
# endif
2287+
ESPRestart();
22932288
}
22942289
}
22952290
}
@@ -2303,11 +2298,7 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
23032298
const char* cmd = SYSdata["cmd"];
23042299
Log.notice(F("Command: %s" CR), cmd);
23052300
if (strstr(cmd, restartCmd) != NULL) { //restart
2306-
# if defined(ESP8266)
2307-
ESP.reset();
2308-
# else
2309-
ESP.restart();
2310-
# endif
2301+
ESPRestart();
23112302
} else if (strstr(cmd, eraseCmd) != NULL) { //erase and restart
23122303
# ifndef ESPWifiManualSetup
23132304
setup_wifimanager(true);
@@ -2340,17 +2331,8 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
23402331
# if defined(ESP32) && (defined(WifiGMode) || defined(WifiPower))
23412332
setESP32WifiPorotocolTxPower();
23422333
# endif
2343-
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
2344-
# if defined(ESP8266)
2345-
ESP.reset();
2346-
# else
2347-
ESP.restart();
2348-
# endif
2349-
}
23502334
}
2351-
# if defined(ZgatewayBT) && defined(ESP32)
2352-
startProcessing();
2353-
# endif
2335+
ESPRestart();
23542336
}
23552337

23562338
# ifdef MQTTsetMQTT
@@ -2442,9 +2424,7 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
24422424
}
24432425
connectMQTT();
24442426
}
2445-
# if defined(ZgatewayBT) && defined(ESP32)
2446-
startProcessing();
2447-
# endif
2427+
ESPRestart();
24482428
}
24492429
# endif
24502430
if (SYSdata.containsKey("mqtt_topic") || SYSdata.containsKey("gateway_name")) {
@@ -2517,10 +2497,5 @@ String toString(uint32_t input) {
25172497
*/
25182498
void watchdogReboot(byte reason) {
25192499
Log.warning(F("Rebooting for reason code %d" CR), reason);
2520-
#if defined(ESP32)
2521-
ESP.restart();
2522-
#elif defined(ESP8266)
2523-
ESP.reset();
2524-
#else // Insert other architectures here
2525-
#endif
2500+
ESPRestart();
25262501
}

0 commit comments

Comments
 (0)