Skip to content

Commit 01be423

Browse files
FRASTMfpistm
authored andcommitted
example(basic): send calendar packet instead of 0xdeadbeef
Modifying the basic.ino to format the Tx packet with the current calendar Date and Time Get the RTC instance initialized in MIX mode Signed-off-by: Francois Ramu <francois.ramu@st.com>
1 parent 8153e61 commit 01be423

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

examples/Basic/Basic.ino

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* This is a very basic example that demonstrates how to configure the
33
* library, join the network, send regular packets and print any
44
* downlink packets received.
5+
* This example is using the RTC in MIX (binary and BCD) mode
56
*
67
* Revised BSD License - https://spdx.org/licenses/BSD-3-Clause.html
78
*/
@@ -12,8 +13,10 @@ STM32LoRaWAN modem;
1213
static const unsigned long TX_INTERVAL = 60000; /* ms */
1314
unsigned long last_tx = 0;
1415

15-
void setup()
16-
{
16+
/* Get the rtc object */
17+
STM32RTC& rtc = STM32RTC::getInstance();
18+
19+
void setup() {
1720
Serial.begin(115200);
1821
Serial.println("Start");
1922
modem.begin(EU868);
@@ -27,17 +30,25 @@ void setup()
2730
Serial.println("Joined");
2831
} else {
2932
Serial.println("Join failed");
30-
while (true) /* infinite loop */;
33+
while (true) /* infinite loop */
34+
;
3135
}
36+
37+
/* set the calendar */
38+
rtc.setTime(15, 30, 58);
39+
rtc.setDate(04, 07, 23);
3240
}
3341

34-
void send_packet()
35-
{
36-
uint8_t payload[] = {0xde, 0xad, 0xbe, 0xef};
42+
void send_packet() {
43+
char payload[27] = { 0 }; /* packet to be sent */
44+
/* prepare the Tx packet : get date and format string */
45+
sprintf(payload, "%02d/%02d/%04d - %02d:%02d:%02d",
46+
rtc.getMonth(), rtc.getDay(), 2000 + rtc.getYear(),
47+
rtc.getHours(), rtc.getMinutes(), rtc.getSeconds());
3748
modem.setPort(10);
3849
modem.beginPacket();
39-
modem.write(payload, sizeof(payload));
40-
if (modem.endPacket() == sizeof(payload)) {
50+
modem.write(payload, strlen(payload));
51+
if (modem.endPacket() == (int)strlen(payload)) {
4152
Serial.println("Sent packet");
4253
} else {
4354
Serial.println("Failed to send packet");
@@ -57,8 +68,7 @@ void send_packet()
5768
}
5869
}
5970

60-
void loop()
61-
{
71+
void loop() {
6272
if (!last_tx || millis() - last_tx > TX_INTERVAL) {
6373
send_packet();
6474
last_tx = millis();

0 commit comments

Comments
 (0)