Skip to content

Commit

Permalink
Handle OTA actively when MQTT is not connected (#1307)
Browse files Browse the repository at this point in the history
* Handle OTA actively when MQTT is not connected

Resolves #1292

* Fix compilation for targets not having OTA

* ArduinoOTA.handle() only with non-ESP targets

* OTA check before reboot: fix condition and add more comments


Signed-off-by: Sami Salonen <[email protected]>
  • Loading branch information
ssalonen authored Nov 13, 2022
1 parent 62553dc commit 3d589ff
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
7 changes: 7 additions & 0 deletions main/User_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,13 @@ int lowpowermode = DEFAULT_LOW_POWER_MODE;
#ifndef ota_port
# define ota_port 8266
#endif
// timeout for OTA activities
// OTA upload with no activity in this period is considered inactive
// As long as OTA upload is considered "active", we avoid rebooting e.g.
// in case of failures connecting to MQTT
#ifndef ota_timeout_millis
# define ota_timeout_millis 30000
#endif

/*-------------DEFINE PINs FOR STATUS LEDs----------------*/
#ifndef LED_SEND_RECEIVE
Expand Down
36 changes: 34 additions & 2 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ bool disc = true; // Auto discovery with Home Assistant convention
#endif
unsigned long timer_led_measures = 0;
static void* eClient = nullptr;
static unsigned long last_ota_activity_millis = 0;
#if defined(ESP8266) || defined(ESP32)
static bool mqtt_secure = MQTT_SECURE_DEFAULT;
static uint8_t mqtt_ss_index = MQTT_SECURE_SELF_SIGNED_INDEX_DEFAULT;
Expand Down Expand Up @@ -509,6 +510,18 @@ bool cmpToMainTopic(const char* topicOri, const char* toAdd) {
return true;
}

void delayWithOTA(long waitMillis) {
#if defined(ESP8266) || defined(ESP32)
long waitStep = 100;
for (long waitedMillis = 0; waitedMillis < waitMillis; waitedMillis += waitStep) {
ArduinoOTA.handle();
delay(waitStep);
}
#else
delay(waitMillis);
#endif
}

void connectMQTT() {
#ifndef ESPWifiManualSetup
# if defined(ESP8266) || defined(ESP32)
Expand Down Expand Up @@ -564,10 +577,25 @@ void connectMQTT() {
Log.warning(F("failed, ssl error code=%d" CR), ((WiFiClientSecure*)eClient)->getLastSSLError());
#endif
digitalWrite(LED_ERROR, LED_ERROR_ON);
delay(2000);
delayWithOTA(2000);
digitalWrite(LED_ERROR, !LED_ERROR_ON);
delay(5000);
delayWithOTA(5000);
if (failure_number_mqtt > maxRetryWatchDog) {
unsigned long millis_since_last_ota;
while (
// When
// ...incomplete OTA in progress
(last_ota_activity_millis != 0)
// ...AND last OTA activity fairly recently
&& ((millis_since_last_ota = millis() - last_ota_activity_millis) < ota_timeout_millis)) {
// ... We consider that OTA might be still active, and we sleep for a while, and giving
// OTA chance to proceed (ArduinoOTA.handle())
Log.warning(F("OTA might be still active (activity %d ms ago)" CR), millis_since_last_ota);
#if defined(ESP8266) || defined(ESP32)
ArduinoOTA.handle();
#endif
delay(100);
}
watchdogReboot(1);
}
}
Expand Down Expand Up @@ -875,13 +903,15 @@ void setOTA() {
Log.trace(F("Start OTA, lock other functions" CR));
digitalWrite(LED_SEND_RECEIVE, LED_SEND_RECEIVE_ON);
digitalWrite(LED_ERROR, LED_ERROR_ON);
last_ota_activity_millis = millis();
# if defined(ZgatewayBT) && defined(ESP32)
stopProcessing();
# endif
lpDisplayPrint("OTA in progress");
});
ArduinoOTA.onEnd([]() {
Log.trace(F("\nOTA done" CR));
last_ota_activity_millis = 0;
digitalWrite(LED_SEND_RECEIVE, !LED_SEND_RECEIVE_ON);
digitalWrite(LED_ERROR, !LED_ERROR_ON);
# if defined(ZgatewayBT) && defined(ESP32)
Expand All @@ -891,8 +921,10 @@ void setOTA() {
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Log.trace(F("Progress: %u%%\r" CR), (progress / (total / 100)));
last_ota_activity_millis = millis();
});
ArduinoOTA.onError([](ota_error_t error) {
last_ota_activity_millis = millis();
digitalWrite(LED_SEND_RECEIVE, !LED_SEND_RECEIVE_ON);
digitalWrite(LED_ERROR, !LED_ERROR_ON);
# if defined(ZgatewayBT) && defined(ESP32)
Expand Down

0 comments on commit 3d589ff

Please sign in to comment.