|
| 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 | +extern STM32RTC rtc; |
| 17 | + |
| 18 | +char payload[27]; /* packet to be sent */ |
| 19 | + |
| 20 | +bool connected = false; /* LoraWan connection ready */ |
| 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 | + /* rtc is already init'd in MIX mode by the LoraWan */ |
| 36 | + rtc.setTime(21, 51, 20); |
| 37 | + rtc.setDate(2, 3, 4, 5); |
| 38 | + |
| 39 | + // Configure join method by (un)commenting the right method |
| 40 | + // call, and fill in credentials in that method call. |
| 41 | + connected = modem.joinOTAA(/* AppEui */ "0000000000000000", /* AppKey */ "00000000000000000000000000000000", /* DevEui */ "0000000000000000"); |
| 42 | + //connected = modem.joinABP(/* DevAddr */ "00000000", /* NwkSKey */ "00000000000000000000000000000000", /* AppSKey */ "00000000000000000000000000000000"); |
| 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 loop() { |
| 60 | + Serial.print("Alarm Match: "); |
| 61 | + Serial.print(alarmMatch_counter); |
| 62 | + Serial.println(" times."); |
| 63 | + Serial.flush(); |
| 64 | + digitalWrite(LED_BUILTIN, HIGH); |
| 65 | + LowPower.deepSleep(); |
| 66 | + digitalWrite(LED_BUILTIN, LOW); |
| 67 | + LowPower.deepSleep(); |
| 68 | + |
| 69 | + /* Alarm has matched 2 times before reching this point */ |
| 70 | + if (connected) { |
| 71 | + send_packet(); |
| 72 | + } else { |
| 73 | + Serial.printf("Alarm at %02d:%02d:%02d\r\n", |
| 74 | + rtc.getHours(), rtc.getMinutes(), rtc.getSeconds()); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +void send_packet() |
| 79 | +{ |
| 80 | + /* prepare the Tx packet : get date and format string */ |
| 81 | + sprintf((char *)payload, "%02d/%02d/%04d - %02d:%02d:%02d", |
| 82 | + rtc.getMonth(), rtc.getDay(), 2000 + rtc.getYear(), |
| 83 | + rtc.getHours(), rtc.getMinutes(), rtc.getSeconds()); |
| 84 | + |
| 85 | + modem.setPort(10); |
| 86 | + modem.beginPacket(); |
| 87 | + modem.write(payload, sizeof(payload)); |
| 88 | + if (modem.endPacket() == sizeof(payload)) { |
| 89 | + Serial.println("Sent packet"); |
| 90 | + } else { |
| 91 | + Serial.println("Failed to send packet"); |
| 92 | + } |
| 93 | + |
| 94 | + if (modem.available()) { |
| 95 | + Serial.print("Received packet on port "); |
| 96 | + Serial.print(modem.getDownlinkPort()); |
| 97 | + Serial.print(":"); |
| 98 | + while (modem.available()) { |
| 99 | + uint8_t b = modem.read(); |
| 100 | + Serial.print(" "); |
| 101 | + Serial.print(b >> 4, HEX); |
| 102 | + Serial.print(b & 0xF, HEX); |
| 103 | + } |
| 104 | + Serial.println(); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +void alarmMatch(void* data) |
| 109 | +{ |
| 110 | + // This function will be called once on device wakeup |
| 111 | + // You can do some little operations here (like changing variables which will be used in the loop) |
| 112 | + // Remember to avoid calling delay() and long running functions since this functions executes in interrupt context |
| 113 | + uint32_t epoc; |
| 114 | + uint32_t epoc_ms; |
| 115 | + uint32_t sec = 0; |
| 116 | + uint32_t _millis = 1000; |
| 117 | + |
| 118 | + if (data != NULL) { |
| 119 | + _millis = *(uint32_t*)data; |
| 120 | + } |
| 121 | + |
| 122 | + sec = _millis / 1000; |
| 123 | + |
| 124 | + _millis = _millis % 1000; |
| 125 | + epoc = rtc.getEpoch(&epoc_ms); |
| 126 | + |
| 127 | + //Update epoch_ms - might need to add a second to epoch |
| 128 | + epoc_ms += _millis; |
| 129 | + if (epoc_ms >= 1000) { |
| 130 | + sec ++; |
| 131 | + epoc_ms -= 1000; |
| 132 | + } |
| 133 | + alarmMatch_counter++; |
| 134 | + // set the alarm again |
| 135 | + rtc.setAlarmEpoch(epoc + sec, STM32RTC::MATCH_SS, epoc_ms); |
| 136 | +} |
0 commit comments