From 6d3053d8adeba11c440e68c5a88eed5675f0560a Mon Sep 17 00:00:00 2001 From: Joerg Lehmann Date: Mon, 1 Jun 2020 12:57:54 +0200 Subject: [PATCH] try to make it more stable... --- mini-beieli-node.ino | 118 ++++++++++++++++++++++++++++++------------- mini_beieli_node.h | 2 +- 2 files changed, 85 insertions(+), 35 deletions(-) diff --git a/mini-beieli-node.ino b/mini-beieli-node.ino index d70b91a..db681d5 100644 --- a/mini-beieli-node.ino +++ b/mini-beieli-node.ino @@ -143,8 +143,6 @@ bool fFlash; bool fUsbPower; // have we printed the sleep info? -bool g_fPrintedSleeping = false; - // the job that's used to synchronize us with the LMIC code static osjob_t iterationJob; static osjob_t sendJob; @@ -690,15 +688,15 @@ void StartNewIteration() { gLed.Set(LedPattern::TwoShort); } - // Loop while sending is in progress, timeout just in case after 300 seconds + // Loop sending is in progress, timeout just in case after 300 seconds long start_time = millis(); if (config_data.debug_level > 0) { gCatena.SafePrintf("waiting while send is in progress\n"); } while (send_in_progress && ((millis() - start_time) < 300000)) { - os_runloop_once(); - delay(10); + gCatena.poll(); + yield(); } // handle timeout... if (send_in_progress) { @@ -726,33 +724,11 @@ void StartNewIteration() { sleep_time_sec = 5; } - // for the first iterations, we set the sleep time to 10 seconds only... + // for the first iterations, we set the sleep time to 120 seconds only... if (iteration <= INIT_PACKETS) { - sleep_time_sec = 10; + sleep_time_sec = 120; } - // Before we go to sleep, we'd like to be sure that this is safe! - if (config_data.debug_level > 0) { - gCatena.SafePrintf("We wait until is is safe to go to sleep...\n"); - } - long loopCount = 0; - long prevPrint; - while(os_queryTimeCriticalJobs(ms2osticks(8000)) != 0) - { - loopCount++; - os_runloop_once(); - if(millis() - prevPrint > 1000) { - prevPrint = millis(); - if (config_data.debug_level > 0) { - gCatena.SafePrintf("LMIC.opmode: %#x\n", LMIC.opmode); - } - } - } - if (config_data.debug_level > 0) { - gCatena.SafePrintf("Now it is safe to go to sleep\n"); - } - delay(10); - if (config_data.debug_level > 0) { gCatena.SafePrintf("now going to sleep for %d seconds...\n", sleep_time_sec); if (fUsbPower) { @@ -819,9 +795,6 @@ void startSendingUplink(bool firstTime) fConfirmed = true; } - os_runloop_once(); - //gCatena.poll(); - if (firstTime) { if (config_data.debug_level > 0) { gCatena.SafePrintf("SendBuffer firstTime\n"); @@ -846,8 +819,6 @@ static void sendBufferDoneCb( { osjobcb_t pFn; - send_in_progress = false; - if (config_data.debug_level > 1) { gLed.Set(LedPattern::Settling); gCatena.SafePrintf("LMIC.opmode in sendBufferDoneCb: %#x\n", LMIC.opmode); @@ -889,6 +860,70 @@ static void txNotProvisionedCb( } } +bool checkDeepSleep(void) +{ + return !fUsbPower; +} + +void doSleepAlert(const bool fDeepSleep) +{ + if (config_data.debug_level > 0) { + gCatena.SafePrintf("We wait until is is safe to go to sleep...\n"); + } + + for (int i = 0; i <= 15; i++) { + long prevPrint = millis(); + while (os_queryTimeCriticalJobs(ms2osticks(2000)) != 0) + { + gCatena.poll(); + yield(); + if (millis() - prevPrint > 1000) { + prevPrint = millis(); + if (config_data.debug_level > 0) { + gCatena.SafePrintf("LMIC.opmode: %#x in loop %d\n", LMIC.opmode, i); + } + } + } + } + + // we wait an extra five seconds... + uint32_t tNow = millis(); + while (uint32_t(millis() - tNow) < 5000) + { + gCatena.poll(); + yield(); + } + + + if (config_data.debug_level > 0) { + gCatena.SafePrintf("Now it is safe to go to sleep\n"); + } + +} + +void updateSleepCounters(void) +{ + // update the sleep parameters + if (gTxCycleCount > 1) + { + // values greater than one are decremented and ultimately reset to default. + --gTxCycleCount; + } + else if (gTxCycleCount == 1) + { + // it's now one (otherwise we couldn't be here.) + if (config_data.debug_level > 0) { + gCatena.SafePrintf("resetting tx cycle to default: %u\n", CATCFG_T_CYCLE); + } + gTxCycleCount = 0; + gTxCycle = CATCFG_T_CYCLE; + } + else + { + // it's zero. Leave it alone. + } +} + static void settleDoneCb( osjob_t* pSendJob) @@ -896,6 +931,21 @@ static void settleDoneCb( if (config_data.debug_level > 0) { gCatena.SafePrintf("settleDoneCb - we are at the end of the callback chain!\n"); } + + const bool fDeepSleep = checkDeepSleep(); + + if (uint32_t(millis()) > gRebootMs) + { + // time to reboot + NVIC_SystemReset(); + } + + doSleepAlert(fDeepSleep); + + /* count what we're up to */ + updateSleepCounters(); + + send_in_progress = false; } void deepSleepPrepare(void) diff --git a/mini_beieli_node.h b/mini_beieli_node.h index bc06009..c9018b5 100644 --- a/mini_beieli_node.h +++ b/mini_beieli_node.h @@ -56,7 +56,7 @@ enum { | \****************************************************************************/ -static const int32_t fwVersion = 20200530; +static const int32_t fwVersion = 20200601; static const byte INIT_PACKAGE_INTERVAL = 100; // send an init package every 100 packages; static const byte MAX_VALUES_TO_SEND = 8;