Skip to content

Commit 6fd1dd6

Browse files
committed
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 <francois.ramu@st.com>
1 parent 2a18b4b commit 6fd1dd6

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/**
2+
* This is an example that demonstrates how to configure the
3+
* library, join the network, send regular packets and print any
4+
* downlink packets received when the lowpower mode is enabled.
5+
* This example is using the RTC in MIX (binary and BCD) mode
6+
* and the lowPower mode to set an Alarm (ALARM_A) before going to sleep
7+
* When alarm Wakes up the system, it send a packet over the LoraWan
8+
*
9+
* Revised BSD License - https://spdx.org/licenses/BSD-3-Clause.html
10+
*/
11+
#include <STM32LoRaWAN.h>
12+
#include <STM32LowPower.h>
13+
#include <STM32RTC.h>
14+
15+
STM32LoRaWAN modem;
16+
/* Get the rtc object */
17+
STM32RTC& rtc = STM32RTC::getInstance();
18+
19+
char payload[27]; /* packet to be sent */
20+
21+
22+
/* Change this value to set alarm match offset in millisecond */
23+
static uint32_t atime = 4000;
24+
25+
// Declare it volatile since it's incremented inside an interrupt
26+
volatile int alarmMatch_counter = 0;
27+
28+
void setup() {
29+
pinMode(LED_BUILTIN, OUTPUT);
30+
31+
Serial.begin(115200);
32+
Serial.println("Start");
33+
modem.begin(EU868);
34+
35+
// Configure join method by (un)commenting the right method
36+
// call, and fill in credentials in that method call.
37+
bool connected = modem.joinOTAA(/* AppEui */ "0000000000000000", /* AppKey */ "00000000000000000000000000000000", /* DevEui */ "0000000000000000");
38+
//connected = modem.joinABP(/* DevAddr */ "00000000", /* NwkSKey */ "00000000000000000000000000000000", /* AppSKey */ "00000000000000000000000000000000");
39+
40+
/* set the calendar */
41+
rtc.setTime(15, 30, 58);
42+
rtc.setDate(04, 07, 23);
43+
44+
if (connected) {
45+
Serial.println("Joined");
46+
} else {
47+
Serial.println("Join failed");
48+
while (true); /* infinite loop */
49+
}
50+
51+
// Configure low power
52+
LowPower.begin();
53+
LowPower.enableWakeupFrom(&rtc, alarmMatch, &atime);
54+
55+
// Configure first alarm in 6 second then it will be done in the rtc callback
56+
rtc.setAlarmEpoch(rtc.getEpoch() + 6);
57+
}
58+
59+
void send_packet()
60+
{
61+
char payload[27] = { 0 }; /* packet to be sent */
62+
/* prepare the Tx packet : get date and format string */
63+
sprintf(payload, "%02d/%02d/%04d - %02d:%02d:%02d",
64+
rtc.getMonth(), rtc.getDay(), 2000 + rtc.getYear(),
65+
rtc.getHours(), rtc.getMinutes(), rtc.getSeconds());
66+
67+
modem.setPort(10);
68+
modem.beginPacket();
69+
modem.write(payload, strlen(payload));
70+
if (modem.endPacket() == (int)strlen(payload)) {
71+
Serial.println("Queued packet");
72+
} else {
73+
Serial.println("Failed to send packet");
74+
}
75+
76+
wait_for_idle();
77+
Serial.println("Sent packet");
78+
79+
if (modem.available()) {
80+
Serial.print("Received packet on port ");
81+
Serial.print(modem.getDownlinkPort());
82+
Serial.print(":");
83+
while (modem.available()) {
84+
uint8_t b = modem.read();
85+
Serial.print(" ");
86+
Serial.print(b >> 4, HEX);
87+
Serial.print(b & 0xF, HEX);
88+
}
89+
Serial.println();
90+
}
91+
}
92+
93+
void background_work()
94+
{
95+
/* some work to do here */
96+
digitalToggle(LED_BUILTIN);
97+
}
98+
99+
void wait_for_idle()
100+
{
101+
while (modem.busy()) {
102+
// Call maintain() so the lora library can do any
103+
// pending background work too.
104+
modem.maintain();
105+
background_work();
106+
}
107+
}
108+
109+
void alarmMatch(void* data)
110+
{
111+
// This function will be called once on device wakeup
112+
// You can do some little operations here (like changing variables which will be used in the loop)
113+
// Remember to avoid calling delay() and long running functions since this functions executes in interrupt context
114+
uint32_t epoc;
115+
uint32_t epoc_ms;
116+
uint32_t sec = 0;
117+
uint32_t _millis = 1000;
118+
119+
if (data != NULL) {
120+
_millis = *(uint32_t*)data;
121+
}
122+
123+
sec = _millis / 1000;
124+
125+
_millis = _millis % 1000;
126+
epoc = rtc.getEpoch(&epoc_ms);
127+
128+
//Update epoch_ms - might need to add a second to epoch
129+
epoc_ms += _millis;
130+
if (epoc_ms >= 1000) {
131+
sec ++;
132+
epoc_ms -= 1000;
133+
}
134+
alarmMatch_counter++;
135+
// set the alarm again
136+
rtc.setAlarmEpoch(epoc + sec, STM32RTC::MATCH_SS, epoc_ms);
137+
}
138+
139+
void loop() {
140+
Serial.print(" Alarm Match: ");
141+
Serial.print(alarmMatch_counter);
142+
Serial.println(" times. deepSleeping now ! ");
143+
Serial.flush();
144+
LowPower.deepSleep();
145+
146+
background_work();
147+
/* After sleeping for atime, send a packet over LoRaWan */
148+
send_packet();
149+
150+
}
151+

0 commit comments

Comments
 (0)