|
| 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 | +static const unsigned long TX_INTERVAL = 60000; /* ms */ |
| 26 | +unsigned long last_tx = 0; |
| 27 | + |
| 28 | +// Declare it volatile since it's incremented inside an interrupt |
| 29 | +volatile int alarmMatch_counter = 0; |
| 30 | + |
| 31 | +void setup() { |
| 32 | + pinMode(LED_BUILTIN, OUTPUT); |
| 33 | + |
| 34 | + Serial.begin(115200); |
| 35 | + Serial.println("Start"); |
| 36 | + modem.begin(EU868); |
| 37 | + |
| 38 | + /* rtc is already init'd in MIX mode by the LoraWan */ |
| 39 | + rtc.setTime(21, 51, 20); |
| 40 | + rtc.setDate(2, 3, 4, 5); |
| 41 | + |
| 42 | + // Configure join method by (un)commenting the right method |
| 43 | + // call, and fill in credentials in that method call. |
| 44 | + connected = modem.joinOTAA(/* AppEui */ "0000000000000000", /* AppKey */ "00000000000000000000000000000000", /* DevEui */ "0000000000000000"); |
| 45 | + //connected = modem.joinABP(/* DevAddr */ "00000000", /* NwkSKey */ "00000000000000000000000000000000", /* AppSKey */ "00000000000000000000000000000000"); |
| 46 | + |
| 47 | + if (connected) { |
| 48 | + Serial.println("Joined"); |
| 49 | + } else { |
| 50 | + Serial.println("Join failed"); |
| 51 | + while (true) /* infinite loop */; |
| 52 | + } |
| 53 | + |
| 54 | + // Configure low power |
| 55 | + LowPower.begin(); |
| 56 | + LowPower.enableWakeupFrom(&rtc, alarmMatch, &atime); |
| 57 | + |
| 58 | + // Configure first alarm in 6 second then it will be done in the rtc callback |
| 59 | + rtc.setAlarmEpoch(rtc.getEpoch() + 6); |
| 60 | +} |
| 61 | + |
| 62 | +void send_packet() |
| 63 | +{ |
| 64 | + /* prepare the Tx packet : get date and format string */ |
| 65 | + sprintf((char *)payload, "%02d/%02d/%04d - %02d:%02d:%02d", |
| 66 | + rtc.getMonth(), rtc.getDay(), 2000 + rtc.getYear(), |
| 67 | + rtc.getHours(), rtc.getMinutes(), rtc.getSeconds()); |
| 68 | + |
| 69 | + modem.setPort(10); |
| 70 | + modem.beginPacket(); |
| 71 | + modem.write(payload, sizeof(payload)); |
| 72 | + if (modem.endPacket() == sizeof(payload)) { |
| 73 | + Serial.println("Sent packet"); |
| 74 | + } else { |
| 75 | + Serial.println("Failed to send packet"); |
| 76 | + } |
| 77 | + |
| 78 | + if (modem.available()) { |
| 79 | + Serial.print("Received packet on port "); |
| 80 | + Serial.print(modem.getDownlinkPort()); |
| 81 | + Serial.print(":"); |
| 82 | + while (modem.available()) { |
| 83 | + uint8_t b = modem.read(); |
| 84 | + Serial.print(" "); |
| 85 | + Serial.print(b >> 4, HEX); |
| 86 | + Serial.print(b & 0xF, HEX); |
| 87 | + } |
| 88 | + Serial.println(); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +void alarmMatch(void* data) |
| 93 | +{ |
| 94 | + // This function will be called once on device wakeup |
| 95 | + // You can do some little operations here (like changing variables which will be used in the loop) |
| 96 | + // Remember to avoid calling delay() and long running functions since this functions executes in interrupt context |
| 97 | + uint32_t epoc; |
| 98 | + uint32_t epoc_ms; |
| 99 | + uint32_t sec = 0; |
| 100 | + uint32_t _millis = 1000; |
| 101 | + |
| 102 | + if (data != NULL) { |
| 103 | + _millis = *(uint32_t*)data; |
| 104 | + } |
| 105 | + |
| 106 | + sec = _millis / 1000; |
| 107 | + |
| 108 | + _millis = _millis % 1000; |
| 109 | + epoc = rtc.getEpoch(&epoc_ms); |
| 110 | + |
| 111 | + //Update epoch_ms - might need to add a second to epoch |
| 112 | + epoc_ms += _millis; |
| 113 | + if (epoc_ms >= 1000) { |
| 114 | + sec ++; |
| 115 | + epoc_ms -= 1000; |
| 116 | + } |
| 117 | + alarmMatch_counter++; |
| 118 | + // set the alarm again |
| 119 | + rtc.setAlarmEpoch(epoc + sec, STM32RTC::MATCH_SS, epoc_ms); |
| 120 | +} |
| 121 | + |
| 122 | +void loop() |
| 123 | +{ |
| 124 | + if (!last_tx || millis() - last_tx > TX_INTERVAL) { |
| 125 | + send_packet(); |
| 126 | + last_tx = millis(); |
| 127 | + } |
| 128 | + |
| 129 | + Serial.print(last_tx); |
| 130 | + Serial.print(" Alarm Match: "); |
| 131 | + Serial.print(alarmMatch_counter); |
| 132 | + Serial.println(" times."); |
| 133 | + Serial.flush(); |
| 134 | + digitalWrite(LED_BUILTIN, HIGH); |
| 135 | + LowPower.deepSleep(); |
| 136 | + digitalWrite(LED_BUILTIN, LOW); |
| 137 | + LowPower.deepSleep(); |
| 138 | + |
| 139 | +} |
| 140 | + |
0 commit comments