Skip to content

Commit

Permalink
STM32duinoLoraWan new example going to sleep and sending packet
Browse files Browse the repository at this point in the history
Modifying the basic.ino to add sleeping period
After LoraWan is connected, it periodically goes to sleep
and send a packet on device wakeUp

Signed-off-by: Francois Ramu <[email protected]>
  • Loading branch information
FRASTM committed Sep 12, 2023
1 parent 9366b63 commit ac4a06c
Showing 1 changed file with 137 additions and 0 deletions.
137 changes: 137 additions & 0 deletions examples/LowPowerLoraWan/LowPowerLoraWan.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* 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;
unsigned long last_tx = 0;
char calendar[27]; /* packet to be sent */

bool connected = false; /* LoraWan connection ready */
/* Get the rtc object */
extern STM32RTC &rtc;

/* Change this value to set alarm match offset in millisecond */
static uint32_t atime = 4000;

void setup() {
pinMode(LED_BUILTIN, OUTPUT);

Serial.begin(115200);
while (!Serial) {}

// Configure low power
LowPower.begin();
LowPower.enableWakeupFrom(&rtc, alarmMatch, &atime);

Serial.println("Start");
modem.begin(EU868);

// rtc is configured by the modem
rtc.setTime(14, 30, 50);
rtc.setDate(1, 2, 3, 4);

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

if (connected) {
Serial.println("Joined");
} else {
Serial.println("Join failed");
while (true) /* infinite loop */;
}

digitalWrite(LED_BUILTIN, HIGH);

// Configure first alarm in 2 seconds then it will be done in the rtc callback
rtc.setAlarmEpoch(rtc.getEpoch() + 8);
Serial.print("set alarm at ");
Serial.println((uint32_t)rtc.getAlarmEpoch(STM32RTC::ALARM_A));
}

void loop()
{
Serial.flush();
digitalWrite(LED_BUILTIN, HIGH);
LowPower.deepSleep();
digitalWrite(LED_BUILTIN, LOW);
LowPower.deepSleep();
}

void send_packet(char *payload)
{
modem.setPort(10);
modem.beginPacket();
modem.write(payload, sizeof(payload));
if (modem.endPacket() == sizeof(payload)) {
Serial.println("Sent packet");
} else {
Serial.println("Failed to send 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 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;
}

/* prepare the Tx packet : get calendar and format string */
sprintf((char *)calendar, "%02d/%02d/%04d - %02d:%02d:%02d",
rtc.getMonth(), rtc.getDay(), 2000 + rtc.getYear(),
rtc.getHours(), rtc.getMinutes(), rtc.getSeconds());

if (connected) {
/* Send a packet on LoraWan */
send_packet(calendar);
}

Serial.print("Alarm Match at ");
Serial.println(calendar);

// set the alarm again
rtc.setAlarmEpoch(epoc + sec, STM32RTC::MATCH_SS, epoc_ms);
}

0 comments on commit ac4a06c

Please sign in to comment.