Skip to content

Commit

Permalink
STM32duinoLoraWan sketch going to low power stop mode and sending packet
Browse files Browse the repository at this point in the history
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
FRASTM committed Oct 6, 2023
1 parent 2a18b4b commit 6fd1dd6
Showing 1 changed file with 151 additions and 0 deletions.
151 changes: 151 additions & 0 deletions examples/LowPowerLoraWan/LowPowerLoraWan.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/**
* 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 */


/* 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);

// Configure join method by (un)commenting the right method
// call, and fill in credentials in that method call.
bool connected = modem.joinOTAA(/* AppEui */ "0000000000000000", /* AppKey */ "00000000000000000000000000000000", /* DevEui */ "0000000000000000");
//connected = modem.joinABP(/* DevAddr */ "00000000", /* NwkSKey */ "00000000000000000000000000000000", /* AppSKey */ "00000000000000000000000000000000");

/* set the calendar */
rtc.setTime(15, 30, 58);
rtc.setDate(04, 07, 23);

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. deepSleeping now ! ");
Serial.flush();
LowPower.deepSleep();

background_work();
/* After sleeping for atime, send a packet over LoRaWan */
send_packet();

}

0 comments on commit 6fd1dd6

Please sign in to comment.