Using personalization mode for the Multitech mDot. Works out of the box with f.e. LORIOT (just copy in DevAddr and the Keys).
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_LoRa_Sleep_Example by
Diff: main.cpp
- Revision:
- 6:12850c60134e
- Parent:
- 5:9788d9d9875d
--- a/main.cpp Thu Mar 31 13:08:26 2016 +0000 +++ b/main.cpp Wed Jun 22 10:51:37 2016 +0000 @@ -12,6 +12,16 @@ static mDot* dot; +// This is where we keep state, every time button D6 is pressed we up it +// also we up it every time we send a package (in case you don't have a button) +static int8_t counter = 0; + +// Make sure to set this var every time you handle an interrupt! +// It's important so we know how we woke up... +static bool from_interrupt = false; + +static InterruptIn btn(PA_1); /* D6 */ + void parseKeys() { int32_t ret; @@ -66,7 +76,11 @@ } } -static int8_t counter = 0; +static void rise() { + counter++; + from_interrupt = true; +} + void send_data(void) { int32_t ret; @@ -88,6 +102,8 @@ int32_t ret; printf("Entering main()\r\n"); + btn.rise(&rise); + // get a mDot handle dot = mDot::getInstance(); @@ -130,7 +146,7 @@ // in the 915 (US) frequency band, spreading factors 7 - 10 are available // in the 868 (EU) frequency band, spreading factors 7 - 12 are available logInfo("setting TX spreading factor"); - if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { + if ((ret = dot->setTxDataRate(mDot::SF_7)) != mDot::MDOT_OK) { logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); } @@ -155,15 +171,23 @@ //******************************************* while (true) { - send_data(); + // when wake from interrupt (need to set this yourself), we don't send + // as duty cycle was not adhere'd yet... + if (!from_interrupt) { + send_data(); + } + from_interrupt = false; - uint32_t sleep_time = std::max((uint32_t)10000, (uint32_t)dot->getNextTxMs()) / 1000; + // get the next transmission frame (in whole seconds) + uint32_t sleep_time = ceil(static_cast<float>(dot->getNextTxMs()) / 1000.0f); logInfo("going to sleep for %d seconds", sleep_time); // go to deepsleep and wake up automatically sleep_time seconds later - dot->sleep(sleep_time, mDot::RTC_ALARM); + // third argument (false) means SLEEP instead of DEEP_SLEEP mode, + // keeps registers & allows wake on Interrupts + dot->sleep(sleep_time, mDot::RTC_ALARM, false); } - // go to deepsleep and wake up on rising edge of WKUP pin (PA0/XBEE_CTS/XBEE_DIO7) - // dot->sleep(0, mDot::INTERRUPT); + // also an idea: go to deepsleep and wake up on rising edge of WKUP pin (PA0/XBEE_CTS/XBEE_DIO7) + // dot->sleep(0, mDot::INTERRUPT, true); }