forked from stm32duino/STM32LoRaWAN
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STM32duinoLoraWan sketch going to low power stop mode and sending packet
This example is configuring the stm32WL for LoraWan connection and low power mode. After LoraWan is connected, it periodically (atime value in milliseconds) goes to low power mode (stop 2) and wakes Up to send a packet Signed-off-by: Francois Ramu <[email protected]>
- Loading branch information
Showing
1 changed file
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/** | ||
* This is an example that demonstrates how to configure the | ||
* library, join the network, send regular packets and print any | ||
* downlink packets received when the lowpower mode is enabled. | ||
* This example is using the RTC in MIX (binary and BCD) mode | ||
* and the lowPower mode to set an Alarm (ALARM_A) before going to sleep | ||
* When alarm Wakes up the system, it send a packet over the LoraWan | ||
* | ||
* Revised BSD License - https://spdx.org/licenses/BSD-3-Clause.html | ||
*/ | ||
#include <STM32LoRaWAN.h> | ||
#include <STM32LowPower.h> | ||
#include <STM32RTC.h> | ||
|
||
STM32LoRaWAN modem; | ||
/* Get the rtc object */ | ||
STM32RTC& rtc = STM32RTC::getInstance(); | ||
|
||
char payload[27]; /* packet to be sent */ | ||
|
||
bool connected = false; /* LoraWan connection ready */ | ||
|
||
/* Change this value to set alarm match offset in millisecond */ | ||
static uint32_t atime = 4000; | ||
|
||
// Declare it volatile since it's incremented inside an interrupt | ||
volatile int alarmMatch_counter = 0; | ||
|
||
void setup() { | ||
pinMode(LED_BUILTIN, OUTPUT); | ||
|
||
Serial.begin(115200); | ||
Serial.println("Start"); | ||
modem.begin(EU868); | ||
|
||
/* rtc is already init'd in MIX mode by the LoraWan */ | ||
rtc.setTime(21, 51, 20); | ||
rtc.setDate(2, 3, 4, 5); | ||
|
||
// Configure join method by (un)commenting the right method | ||
// call, and fill in credentials in that method call. | ||
connected = modem.joinOTAA(/* AppEui */ "0000000000000000", /* AppKey */ "00000000000000000000000000000000", /* DevEui */ "0000000000000000"); | ||
//connected = modem.joinABP(/* DevAddr */ "00000000", /* NwkSKey */ "00000000000000000000000000000000", /* AppSKey */ "00000000000000000000000000000000"); | ||
|
||
if (connected) { | ||
Serial.println("Joined"); | ||
} else { | ||
Serial.println("Join failed"); | ||
while (true); /* infinite loop */ | ||
} | ||
|
||
// Configure low power | ||
LowPower.begin(); | ||
LowPower.enableWakeupFrom(&rtc, alarmMatch, &atime); | ||
|
||
// Configure first alarm in 6 second then it will be done in the rtc callback | ||
rtc.setAlarmEpoch(rtc.getEpoch() + 6); | ||
} | ||
|
||
void send_packet() | ||
{ | ||
char payload[27] = { 0 }; /* packet to be sent */ | ||
/* prepare the Tx packet : get date and format string */ | ||
sprintf(payload, "%02d/%02d/%04d - %02d:%02d:%02d", | ||
rtc.getMonth(), rtc.getDay(), 2000 + rtc.getYear(), | ||
rtc.getHours(), rtc.getMinutes(), rtc.getSeconds()); | ||
|
||
modem.setPort(10); | ||
modem.beginPacket(); | ||
modem.write(payload, strlen(payload)); | ||
if (modem.endPacket() == (int)strlen(payload)) { | ||
Serial.println("Queued packet"); | ||
} else { | ||
Serial.println("Failed to send packet"); | ||
} | ||
|
||
wait_for_idle(); | ||
Serial.println("Sent packet"); | ||
|
||
if (modem.available()) { | ||
Serial.print("Received packet on port "); | ||
Serial.print(modem.getDownlinkPort()); | ||
Serial.print(":"); | ||
while (modem.available()) { | ||
uint8_t b = modem.read(); | ||
Serial.print(" "); | ||
Serial.print(b >> 4, HEX); | ||
Serial.print(b & 0xF, HEX); | ||
} | ||
Serial.println(); | ||
} | ||
} | ||
|
||
void background_work() | ||
{ | ||
/* some work to do here */ | ||
digitalToggle(LED_BUILTIN); | ||
} | ||
|
||
void wait_for_idle() | ||
{ | ||
while (modem.busy()) { | ||
// Call maintain() so the lora library can do any | ||
// pending background work too. | ||
modem.maintain(); | ||
background_work(); | ||
} | ||
} | ||
|
||
void alarmMatch(void* data) | ||
{ | ||
// This function will be called once on device wakeup | ||
// You can do some little operations here (like changing variables which will be used in the loop) | ||
// Remember to avoid calling delay() and long running functions since this functions executes in interrupt context | ||
uint32_t epoc; | ||
uint32_t epoc_ms; | ||
uint32_t sec = 0; | ||
uint32_t _millis = 1000; | ||
|
||
if (data != NULL) { | ||
_millis = *(uint32_t*)data; | ||
} | ||
|
||
sec = _millis / 1000; | ||
|
||
_millis = _millis % 1000; | ||
epoc = rtc.getEpoch(&epoc_ms); | ||
|
||
//Update epoch_ms - might need to add a second to epoch | ||
epoc_ms += _millis; | ||
if (epoc_ms >= 1000) { | ||
sec ++; | ||
epoc_ms -= 1000; | ||
} | ||
alarmMatch_counter++; | ||
// set the alarm again | ||
rtc.setAlarmEpoch(epoc + sec, STM32RTC::MATCH_SS, epoc_ms); | ||
} | ||
|
||
void loop() { | ||
Serial.print(" Alarm Match: "); | ||
Serial.print(alarmMatch_counter); | ||
Serial.println(" times. Sleeping now ! "); | ||
Serial.flush(); | ||
LowPower.deepSleep(); | ||
|
||
background_work(); | ||
/* After sleeping for atime, send a packet over LoRaWan */ | ||
send_packet(); | ||
|
||
} | ||
|