From a3249203d0e5506b69b39be4b8ac63a6da85f5c0 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 28 Dec 2020 18:31:31 +0100 Subject: [PATCH] handle join during sleep --- src/lorawan.cpp | 10 +++------- src/reset.cpp | 35 ++++++++--------------------------- 2 files changed, 11 insertions(+), 34 deletions(-) diff --git a/src/lorawan.cpp b/src/lorawan.cpp index aa0e9ccd..440b4a47 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -284,14 +284,10 @@ esp_err_t lmic_init(void) { // Pass OTA parameters to LMIC_setSession #else // load saved session from RTC, if we have one - if (RTC_runmode == RUNMODE_WAKEUP) { + if (RTC_runmode == RUNMODE_WAKEUP) LoadLMICFromRTC(); - } - // otherwise start join procedure if not already joined - else { - if (!LMIC_startJoining()) - ESP_LOGI(TAG, "Already joined"); - } + if (!LMIC_startJoining()) + ESP_LOGI(TAG, "Already joined"); #endif // start lmic loop task diff --git a/src/reset.cpp b/src/reset.cpp index 998367b1..e5961d75 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -84,24 +84,15 @@ void do_after_reset(void) { void enter_deepsleep(const uint64_t wakeup_sec = 60, gpio_num_t wakeup_gpio = GPIO_NUM_MAX) { - // don't go to sleep while unjoined -#if (HAS_LORA) - if (!LMIC.devaddr) { - ESP_LOGI(TAG, "Can't go to sleep while joining"); - return; - } -#endif + ESP_LOGI(TAG, "Preparing to sleep..."); + RTC_runmode = RUNMODE_SLEEP; int i; // validate wake up pin, if we have if (!GPIO_IS_VALID_GPIO(wakeup_gpio)) wakeup_gpio = GPIO_NUM_MAX; - ESP_LOGI(TAG, "Preparing to sleep..."); - - RTC_runmode = RUNMODE_SLEEP; - // stop further enqueuing of senddata and MAC processing sendTimer.detach(); @@ -125,27 +116,23 @@ void enter_deepsleep(const uint64_t wakeup_sec = 60, // wait a while (max 100 sec) to clear send queues ESP_LOGI(TAG, "Waiting until send queues are empty..."); - for (i = 10; i > 0; i--) { + for (i = 100; i > 0; i--) { if (!allQueuesEmtpy()) - vTaskDelay(pdMS_TO_TICKS(10000)); + vTaskDelay(pdMS_TO_TICKS(1000)); else break; } - if (i == 0) - goto Error; - // shutdown LMIC safely, waiting max 100 sec + // shutdown LMIC safely, waiting max 100 sec #if (HAS_LORA) ESP_LOGI(TAG, "Waiting until LMIC is idle..."); - for (i = 10; i > 0; i--) { + for (i = 100; i > 0; i--) { if ((LMIC.opmode & OP_TXRXPEND) || os_queryTimeCriticalJobs(sec2osticks(wakeup_sec))) - vTaskDelay(pdMS_TO_TICKS(10000)); + vTaskDelay(pdMS_TO_TICKS(1000)); else break; } - if (i == 0) - goto Error; #endif // (HAS_LORA) // shutdown MQTT safely @@ -165,10 +152,8 @@ void enter_deepsleep(const uint64_t wakeup_sec = 60, else break; } - if (i == 0) - goto Error; - // save LMIC state to RTC RAM + // save LMIC state to RTC RAM #if (HAS_LORA) SaveLMICToRTC(wakeup_sec); #endif // (HAS_LORA) @@ -207,10 +192,6 @@ void enter_deepsleep(const uint64_t wakeup_sec = 60, RTC_millis += millis(); ESP_LOGI(TAG, "Going to sleep, good bye."); esp_deep_sleep_start(); - -Error: - ESP_LOGE(TAG, "Can't go to sleep. Resetting."); - do_reset(true); } unsigned long long uptime() { return (RTC_millis + millis()); } \ No newline at end of file