diff --git a/mini-beieli-node.ino b/mini-beieli-node.ino index 4863d85..0ce8238 100644 --- a/mini-beieli-node.ino +++ b/mini-beieli-node.ino @@ -93,6 +93,7 @@ LORA_data_first lora_data_first; CONFIG_data config_data; SENSOR_data last_sensor_reading; long iteration = 0; // what iteration number do we have, starts with 0 +bool send_in_progress = false; // generic timer long t_cur; @@ -226,7 +227,11 @@ void setup_platform(void) // set up the LED gLed.begin(); gCatena.registerObject(&gLed); - gLed.Set(LedPattern::FastFlash); + gLed.Set(LedPattern::Off); + + if (config_data.debug_level > 1) { + gLed.Set(LedPattern::FastFlash); + } // set up LoRaWAN gCatena.SafePrintf("LoRaWAN init: "); @@ -354,7 +359,9 @@ void setup_uplink(void) if (!gLoRaWAN.IsProvisioned()) gCatena.SafePrintf("%010d - LoRaWAN not provisioned yet. Use the commands to set it up.\n"); else { - gLed.Set(LedPattern::Joining); + if (config_data.debug_level > 1) { + gLed.Set(LedPattern::Joining); + } /* trigger a join by sending the first packet */ StartNewIteration(); @@ -481,14 +488,16 @@ uint8_t GetVBatValue(int millivolts) return res; } -void DoDeepSleep() +void DoDeepSleep(uint32_t sleep_time) { if (config_data.debug_level > 0) { gCatena.SafePrintf("%010d - now going to deep sleep\n", millis()); } // Prepare Deep Sleep - gLed.Set(LedPattern::Off); + if (config_data.debug_level > 1) { + gLed.Set(LedPattern::Off); + } Serial.end(); Wire.end(); @@ -497,7 +506,7 @@ void DoDeepSleep() gSPI2.end(); // Now sleeping... - gCatena.Sleep(CATCFG_T_INTERVAL); + gCatena.Sleep(sleep_time); // Recover from wakeup... Serial.begin(); @@ -589,6 +598,8 @@ void ReadSensors(SENSOR_data &sensor_data) { } void StartNewIteration() { + uint32_t wait_time; + // we increment the iteration counter iteration++; @@ -649,16 +660,24 @@ void StartNewIteration() { gCatena.SafePrintf("%010d - startSendingUplink(), my_position: %d, iteration: %d\n", millis(), my_position, iteration); } startSendingUplink(iteration == 1); - gLed.Set(LedPattern::TwoShort); + if (config_data.debug_level > 1) { + gLed.Set(LedPattern::TwoShort); + } - // If we have any critical jobs to complete in the next 8 seconds, do not sleep but process them - long loopCount = 0; - while (os_queryTimeCriticalJobs(ms2osticks(8000)) != 0) + // Loop while sending is in progress, timeout just in case after 300 seconds + long start_time = millis(); + if (config_data.debug_level > 0) { + gCatena.SafePrintf("%010d - waiting while send is in progress\n", millis()); + } + while (send_in_progress && ((millis() - start_time) < 300000)) { - loopCount++; gCatena.poll(); yield(); } + wait_time = (uint32_t)((millis() - start_time) / 1000); + if (config_data.debug_level > 0) { + gCatena.SafePrintf("%010d - end waiting, wait time was %d seconds\n", millis(), wait_time); + } } if (iteration > 1) { @@ -674,10 +693,18 @@ void StartNewIteration() { gCatena.SafePrintf("USB Power is off\n"); } //Serial.flush(); + if (config_data.debug_level > 1) { + gLed.Set(LedPattern::Sleeping); + } + } + uint32_t sleep_time_sec; + if ((wait_time + 2) < CATCFG_T_INTERVAL) { + sleep_time_sec = CATCFG_T_INTERVAL - wait_time - 2; + } else { + sleep_time_sec = 0; } - gLed.Set(LedPattern::Sleeping); if (!fUsbPower) { - DoDeepSleep(); + DoDeepSleep(sleep_time_sec); os_setTimedCallback( &iterationJob, os_getTime() + sec2osticks(2), @@ -687,19 +714,24 @@ void StartNewIteration() { gCatena.SafePrintf("%010d - light sleep; os_setTimedCallback for startNewIterationCb in %d...\n", millis(), CATCFG_T_INTERVAL); os_setTimedCallback( &iterationJob, - os_getTime() + sec2osticks(CATCFG_T_INTERVAL), + os_getTime() + sec2osticks(sleep_time_sec), startNewIterationCb); } } void startSendingUplink(bool firstTime) { - LedPattern savedLed = gLed.Set(LedPattern::Measuring); + LedPattern savedLed; + if (config_data.debug_level > 1) { + savedLed = gLed.Set(LedPattern::Measuring); + } - if (savedLed != LedPattern::Joining) - gLed.Set(LedPattern::Sending); - else - gLed.Set(LedPattern::Joining); + if (config_data.debug_level > 1) { + if (savedLed != LedPattern::Joining) + gLed.Set(LedPattern::Sending); + else + gLed.Set(LedPattern::Joining); + } bool fConfirmed = false; if (gCatena.GetOperatingFlags() & (1 << 16)) { @@ -714,11 +746,14 @@ void startSendingUplink(bool firstTime) gCatena.SafePrintf("%010d - SendBuffer firstTime\n", millis()); } gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort); + send_in_progress = true; + } else { if (config_data.debug_level > 0) { gCatena.SafePrintf("%010d - SendBuffer not firstTime\n", millis()); } gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort); + send_in_progress = true; } ClearLoraData(); } @@ -729,7 +764,9 @@ static void sendBufferDoneCb( { osjobcb_t pFn; - gLed.Set(LedPattern::Settling); + if (config_data.debug_level > 1) { + gLed.Set(LedPattern::Settling); + } pFn = settleDoneCb; if (! fStatus) @@ -762,7 +799,9 @@ static void txNotProvisionedCb( gCatena.SafePrintf("LoRaWAN not provisioned yet. Use the commands to set it up.\n"); } gLoRaWAN.Shutdown(); - gLed.Set(LedPattern::NotProvisioned); + if (config_data.debug_level > 1) { + gLed.Set(LedPattern::NotProvisioned); + } } @@ -787,7 +826,9 @@ static void settleDoneCb( static void sleepDoneCb(osjob_t* pJob) { - gLed.Set(LedPattern::WarmingUp); + if (config_data.debug_level > 1) { + gLed.Set(LedPattern::WarmingUp); + } if (config_data.debug_level > 0) { gCatena.SafePrintf("%010d - sleepDoneCb\n", millis()); @@ -804,6 +845,7 @@ static void warmupDoneCb(osjob_t* pJob) if (config_data.debug_level > 0) { gCatena.SafePrintf("%010d - warmupDoneCb\n", millis()); } + send_in_progress = false; } static void startNewIterationCb(osjob_t* pJob) diff --git a/mini_beieli_node.h b/mini_beieli_node.h index e80f553..6bf1a62 100644 --- a/mini_beieli_node.h +++ b/mini_beieli_node.h @@ -55,7 +55,7 @@ enum { | \****************************************************************************/ -static const int32_t fwVersion = 20191120; +static const int32_t fwVersion = 20191121; static const byte MAX_VALUES_TO_SEND = 8; //static const byte MAX_VALUES_TO_SEND = 1; // Testing static const uint8_t LORA_DATA_VERSION = 1; @@ -69,7 +69,7 @@ typedef struct { long cal_w2_0; // 4 Bytes, Wert Waegezelle 2 ohne Gewicht float cal_w1_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 1 float cal_w2_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 2 - byte debug_level; // 0 => no debugging, 1 => infos, 2 => error, 3 => highest level + byte debug_level; // 0 => no debugging, no led, 1 => infos, no led, 2 => infos, 3 => error, 4 => highest level byte fill[122]; } __attribute__((packed)) CONFIG_data;