handle join during sleep

This commit is contained in:
cyberman54 2020-12-28 18:31:31 +01:00
parent 19045654e7
commit a3249203d0
2 changed files with 11 additions and 34 deletions

View File

@ -284,14 +284,10 @@ esp_err_t lmic_init(void) {
// Pass OTA parameters to LMIC_setSession // Pass OTA parameters to LMIC_setSession
#else #else
// load saved session from RTC, if we have one // load saved session from RTC, if we have one
if (RTC_runmode == RUNMODE_WAKEUP) { if (RTC_runmode == RUNMODE_WAKEUP)
LoadLMICFromRTC(); LoadLMICFromRTC();
} if (!LMIC_startJoining())
// otherwise start join procedure if not already joined ESP_LOGI(TAG, "Already joined");
else {
if (!LMIC_startJoining())
ESP_LOGI(TAG, "Already joined");
}
#endif #endif
// start lmic loop task // start lmic loop task

View File

@ -84,24 +84,15 @@ void do_after_reset(void) {
void enter_deepsleep(const uint64_t wakeup_sec = 60, void enter_deepsleep(const uint64_t wakeup_sec = 60,
gpio_num_t wakeup_gpio = GPIO_NUM_MAX) { gpio_num_t wakeup_gpio = GPIO_NUM_MAX) {
// don't go to sleep while unjoined ESP_LOGI(TAG, "Preparing to sleep...");
#if (HAS_LORA)
if (!LMIC.devaddr) {
ESP_LOGI(TAG, "Can't go to sleep while joining");
return;
}
#endif
RTC_runmode = RUNMODE_SLEEP;
int i; int i;
// validate wake up pin, if we have // validate wake up pin, if we have
if (!GPIO_IS_VALID_GPIO(wakeup_gpio)) if (!GPIO_IS_VALID_GPIO(wakeup_gpio))
wakeup_gpio = GPIO_NUM_MAX; wakeup_gpio = GPIO_NUM_MAX;
ESP_LOGI(TAG, "Preparing to sleep...");
RTC_runmode = RUNMODE_SLEEP;
// stop further enqueuing of senddata and MAC processing // stop further enqueuing of senddata and MAC processing
sendTimer.detach(); 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 // wait a while (max 100 sec) to clear send queues
ESP_LOGI(TAG, "Waiting until send queues are empty..."); ESP_LOGI(TAG, "Waiting until send queues are empty...");
for (i = 10; i > 0; i--) { for (i = 100; i > 0; i--) {
if (!allQueuesEmtpy()) if (!allQueuesEmtpy())
vTaskDelay(pdMS_TO_TICKS(10000)); vTaskDelay(pdMS_TO_TICKS(1000));
else else
break; break;
} }
if (i == 0)
goto Error;
// shutdown LMIC safely, waiting max 100 sec // shutdown LMIC safely, waiting max 100 sec
#if (HAS_LORA) #if (HAS_LORA)
ESP_LOGI(TAG, "Waiting until LMIC is idle..."); 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) || if ((LMIC.opmode & OP_TXRXPEND) ||
os_queryTimeCriticalJobs(sec2osticks(wakeup_sec))) os_queryTimeCriticalJobs(sec2osticks(wakeup_sec)))
vTaskDelay(pdMS_TO_TICKS(10000)); vTaskDelay(pdMS_TO_TICKS(1000));
else else
break; break;
} }
if (i == 0)
goto Error;
#endif // (HAS_LORA) #endif // (HAS_LORA)
// shutdown MQTT safely // shutdown MQTT safely
@ -165,10 +152,8 @@ void enter_deepsleep(const uint64_t wakeup_sec = 60,
else else
break; break;
} }
if (i == 0)
goto Error;
// save LMIC state to RTC RAM // save LMIC state to RTC RAM
#if (HAS_LORA) #if (HAS_LORA)
SaveLMICToRTC(wakeup_sec); SaveLMICToRTC(wakeup_sec);
#endif // (HAS_LORA) #endif // (HAS_LORA)
@ -207,10 +192,6 @@ void enter_deepsleep(const uint64_t wakeup_sec = 60,
RTC_millis += millis(); RTC_millis += millis();
ESP_LOGI(TAG, "Going to sleep, good bye."); ESP_LOGI(TAG, "Going to sleep, good bye.");
esp_deep_sleep_start(); 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()); } unsigned long long uptime() { return (RTC_millis + millis()); }