|
| 1 | +/* |
| 2 | + * LowPowerBasic |
| 3 | + * |
| 4 | + * This is a basic example that demonstrates how to configure the |
| 5 | + * library, join the network, then go to Low Power deep Sleep (STOP mode) |
| 6 | + * and on WakeUp, send regular packets with current RTC calendar value. |
| 7 | + * Then It goes to LowPower again for a period (TX_INTERVAL milliseconds) |
| 8 | + * |
| 9 | + * This example is using the RTC in MIX (binary and BCD) mode |
| 10 | + * and the LowPower library |
| 11 | + * |
| 12 | + * Revised BSD License - https://spdx.org/licenses/BSD-3-Clause.html |
| 13 | + */ |
| 14 | +#include <STM32LoRaWAN.h> |
| 15 | +#include <STM32LowPower.h> |
| 16 | +#include <STM32RTC.h> |
| 17 | + |
| 18 | +STM32LoRaWAN modem; |
| 19 | +/* Get the rtc object */ |
| 20 | +STM32RTC& rtc = STM32RTC::getInstance(); |
| 21 | + |
| 22 | +/* Change this value to set alarm match offset in millisecond */ |
| 23 | +static const unsigned long TX_INTERVAL = 60000; /* ms */ |
| 24 | + |
| 25 | +// Declare it volatile since it's incremented inside an interrupt |
| 26 | +volatile int alarmMatch_counter = 0; |
| 27 | + |
| 28 | +/* callback function once the Alarm A matched */ |
| 29 | +void alarmMatch(void* data) |
| 30 | +{ |
| 31 | + UNUSED(data); |
| 32 | + alarmMatch_counter++; |
| 33 | +} |
| 34 | + |
| 35 | +void background_work() |
| 36 | +{ |
| 37 | + /* Toggle LED */ |
| 38 | + digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); |
| 39 | + |
| 40 | + Serial.print(" Alarm Match: "); |
| 41 | + Serial.print(alarmMatch_counter); |
| 42 | + Serial.println(" times. deepSleeping now ! "); |
| 43 | + Serial.flush(); /* Flush before going to sleep */ |
| 44 | +} |
| 45 | + |
| 46 | +void setup() { |
| 47 | + pinMode(LED_BUILTIN, OUTPUT); |
| 48 | + |
| 49 | + Serial.begin(115200); |
| 50 | + Serial.println("Start"); |
| 51 | + modem.begin(EU868); |
| 52 | + |
| 53 | + // Configure join method by (un)commenting the right method |
| 54 | + // call, and fill in credentials in that method call. |
| 55 | + |
| 56 | + bool connected = modem.joinOTAA(/* AppEui */ "0000000000000000", /* AppKey */ "00000000000000000000000000000000", /* DevEui */ "0000000000000000"); |
| 57 | + //bool connected = modem.joinABP(/* DevAddr */ "00000000", /* NwkSKey */ "00000000000000000000000000000000", /* AppSKey */ "00000000000000000000000000000000"); |
| 58 | + |
| 59 | + |
| 60 | + if (connected) { |
| 61 | + Serial.println("Joined"); |
| 62 | + } else { |
| 63 | + Serial.println("Join failed"); |
| 64 | + while (true); /* infinite loop */ |
| 65 | + } |
| 66 | + |
| 67 | + /* set the calendar */ |
| 68 | + rtc.setTime(15, 30, 58); |
| 69 | + rtc.setDate(4, 9, 23); |
| 70 | + |
| 71 | + // Configure low power |
| 72 | + LowPower.begin(); |
| 73 | + LowPower.enableWakeupFrom(&rtc, alarmMatch); |
| 74 | + |
| 75 | +} |
| 76 | + |
| 77 | +void send_packet() { |
| 78 | + char payload[27] = { 0 }; /* packet to be sent */ |
| 79 | + /* prepare the Tx packet : get date and format string */ |
| 80 | + sprintf(payload, "%02d/%02d/%04d - %02d:%02d:%02d", |
| 81 | + rtc.getMonth(), rtc.getDay(), 2000 + rtc.getYear(), |
| 82 | + rtc.getHours(), rtc.getMinutes(), rtc.getSeconds()); |
| 83 | + modem.setPort(10); |
| 84 | + modem.beginPacket(); |
| 85 | + modem.write(payload, strlen(payload)); |
| 86 | +Serial.println(payload); |
| 87 | + if (modem.endPacket() == (int)strlen(payload)) { |
| 88 | + Serial.println("Sent packet"); |
| 89 | + } else { |
| 90 | + Serial.println("Failed to send packet"); |
| 91 | + } |
| 92 | + |
| 93 | + if (modem.available()) { |
| 94 | + Serial.print("Received packet on port "); |
| 95 | + Serial.print(modem.getDownlinkPort()); |
| 96 | + Serial.print(":"); |
| 97 | + while (modem.available()) { |
| 98 | + uint8_t b = modem.read(); |
| 99 | + Serial.print(" "); |
| 100 | + Serial.print(b >> 4, HEX); |
| 101 | + Serial.print(b & 0xF, HEX); |
| 102 | + } |
| 103 | + Serial.println(); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +void loop() |
| 108 | +{ |
| 109 | + |
| 110 | + background_work(); |
| 111 | + |
| 112 | + LowPower.deepSleep(TX_INTERVAL); |
| 113 | + |
| 114 | + send_packet(); |
| 115 | + |
| 116 | +} |
0 commit comments