diff --git a/mini-beieli-node.ino b/mini-beieli-node.ino index 3974482..37a446f 100644 --- a/mini-beieli-node.ino +++ b/mini-beieli-node.ino @@ -35,7 +35,9 @@ using namespace McciCatena; | | MANIFEST CONSTANTS & TYPEDEFS | - \****************************************************************************/ +\****************************************************************************/ + +constexpr uint8_t kUplinkPort = 2; /* how long do we wait between transmissions? (in seconds) */ enum { @@ -84,7 +86,7 @@ unsigned gTxCycleCount; // forwards static void settleDoneCb(osjob_t* pSendJob); static void warmupDoneCb(osjob_t* pSendJob); -static void txFailedDoneCb(osjob_t* pSendJob); +static void txNotProvisionedCb(osjob_t *pSendJob); static void sleepDoneCb(osjob_t* pSendJob); static Arduino_LoRaWAN::SendBufferCbFn sendBufferDoneCb; static Arduino_LoRaWAN::ReceivePortBufferCbFn receiveMessage; @@ -251,11 +253,6 @@ void setup(void) setup_bme280(); //setup_scales(); - /* for 4451, we need wider tolerances, it seems */ -#if defined(ARDUINO_ARCH_STM32) - LMIC_setClockError(MAX_CLOCK_ERROR * 10 / 100); -#endif - setup_flash(); setup_uplink(); } @@ -366,9 +363,6 @@ void setup_platform(void) flags = 0; } - // Swisscom temporary fix... - //LMIC_setDrTxpow(DR_SF10,14); - //LMIC_setAdrMode(1); } void setup_bme280(void) @@ -431,9 +425,18 @@ void setup_flash(void) void setup_uplink(void) { - // Do an unjoin, so every reboot will trigger a join - LMIC_unjoin(); +#if defined(_mcci_arduino_version) && _mcci_arduino_version >= _mcci_arduino_version_calc(2,4,0,90) && \ + defined(CATENA_ARDUINO_PLATFORM_VERSION_CALC) && CATENA_ARDUINO_PLATFORM_VERSION >= CATENA_ARDUINO_PLATFORM_VERSION_CALC(0,17,0,10) + LMIC_setClockError(5*65536/100); +#else + LMIC_setClockError(10*65536/100); +#endif + // Do an unjoin, so every reboot will trigger a join + gCatena.SafePrintf("Do an unjoin...\n"); + LMIC_unjoin(); + LMIC_setLinkCheckMode(1); + /* trigger a join by sending the first packet */ if (!(gCatena.GetOperatingFlags() & static_cast(gCatena.OPERATING_FLAGS::fManufacturingTest))) { if (!gLoRaWAN.IsProvisioned()) @@ -768,12 +771,12 @@ void startSendingUplink(bool firstTime) #ifdef DEBUG gCatena.SafePrintf("SendBuffer firstTime\n"); #endif - gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed); + gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort); } else { #ifdef DEBUG gCatena.SafePrintf("SendBuffer not firstTime\n"); #endif - gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed); + gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort); } ClearLoraData(); } @@ -785,27 +788,37 @@ static void sendBufferDoneCb( osjobcb_t pFn; gLed.Set(LedPattern::Settling); - if (!fStatus) { - gCatena.SafePrintf("send buffer failed\n"); - pFn = txFailedDoneCb; - } - else { - pFn = settleDoneCb; + + pFn = settleDoneCb; + if (! fStatus) + { + if (!gLoRaWAN.IsProvisioned()) + { + // we'll talk about it at the callback. + pFn = txNotProvisionedCb; + + // but prevent join attempts now. + gLoRaWAN.Shutdown(); + } + else + gCatena.SafePrintf("send buffer failed\n"); } + os_setTimedCallback( &sensorJob, os_getTime() + sec2osticks(CATCFG_T_SETTLE), - pFn); -} - -static void txFailedDoneCb( - osjob_t* pSendJob) -{ - gCatena.SafePrintf("not provisioned, idling\n"); - gLoRaWAN.Shutdown(); - gLed.Set(LedPattern::NotProvisioned); + pFn + ); } +static void txNotProvisionedCb( + osjob_t *pSendJob + ) + { + gCatena.SafePrintf("LoRaWAN not provisioned yet. Use the commands to set it up.\n"); + gLoRaWAN.Shutdown(); + gLed.Set(LedPattern::NotProvisioned); + } static void settleDoneCb( @@ -814,13 +827,13 @@ static void settleDoneCb( #ifdef DEBUG gCatena.SafePrintf("settleDoneCb\n"); #endif -// Terry vv - gCatena.SafePrintf("LMIC.rxDelay: %i\n",LMIC.rxDelay); - gCatena.SafePrintf("LMIC.dn2Dr: %i\n",LMIC.dn2Dr); - gCatena.SafePrintf("LMIC.dn2Freq: %i\n",LMIC.dn2Freq); - gCatena.SafePrintf("LMIC.rx1DrOffset: %i\n",LMIC.rx1DrOffset); + // Terry vv + gCatena.SafePrintf("LMIC.rxDelay: %i\n", LMIC.rxDelay); + gCatena.SafePrintf("LMIC.dn2Dr: %i\n", LMIC.dn2Dr); + gCatena.SafePrintf("LMIC.dn2Freq: %i\n", LMIC.dn2Freq); + gCatena.SafePrintf("LMIC.rx1DrOffset: %i\n", LMIC.rx1DrOffset); -// Terry ^^ + // Terry ^^ sleepDoneCb(pSendJob); @@ -849,25 +862,25 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage #ifdef DEBUG gCatena.SafePrintf("receiveMessage was called!!!\n"); #endif -// Terry vv - if (port == 0) - { - gCatena.SafePrintf("MAC message:"); - for (unsigned i = 0; i < LMIC.dataBeg; ++i) - { - gCatena.SafePrintf(" %02x", LMIC.frame[i]); - } - return; - } + // Terry vv + if (port == 0) + { + gCatena.SafePrintf("MAC message:"); + for (unsigned i = 0; i < LMIC.dataBeg; ++i) + { + gCatena.SafePrintf(" %02x", LMIC.frame[i]); + } + return; + } - gCatena.SafePrintf("receiveMessage was called!!!!n"); - gCatena.SafePrintf("Port: %s\n",port); - gCatena.SafePrintf("LMIC.rxDelay: %i\n",LMIC.rxDelay); - gCatena.SafePrintf("LMIC.dn2Dr: %i\n",LMIC.dn2Dr); - gCatena.SafePrintf("LMIC.dn2Freq: %i\n",LMIC.dn2Freq); - gCatena.SafePrintf("LMIC.rx1DrOffset: %i\n",LMIC.rx1DrOffset); + gCatena.SafePrintf("receiveMessage was called!!!!n"); + gCatena.SafePrintf("Port: %s\n", port); + gCatena.SafePrintf("LMIC.rxDelay: %i\n", LMIC.rxDelay); + gCatena.SafePrintf("LMIC.dn2Dr: %i\n", LMIC.dn2Dr); + gCatena.SafePrintf("LMIC.dn2Freq: %i\n", LMIC.dn2Freq); + gCatena.SafePrintf("LMIC.rx1DrOffset: %i\n", LMIC.rx1DrOffset); -// Terry ^^ + // Terry ^^ if (! (port == 1 && 2 <= nMessage && nMessage <= 3)) {