|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman |
| 3 | + * Copyright (c) 2018 Terry Moore, MCCI |
| 4 | + * Copyright (c) 2025 Tristan Webber, Shrunk Innovation Labs. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to anyone |
| 7 | + * obtaining a copy of this document and accompanying files, |
| 8 | + * to do whatever they want with them without any restriction, |
| 9 | + * including, but not limited to, copying, modification and redistribution. |
| 10 | + * NO WARRANTY OF ANY KIND IS PROVIDED. |
| 11 | + * |
| 12 | + * This example demonstrates how to manually configure a board |
| 13 | + * requiring advanced initialisation (https://github.com/mcci-catena/arduino-lmic/blob/master/doc/HOWTO-Manually-Configure.md#advanced-initialization). |
| 14 | + * This is required for boards that need some level of configuration |
| 15 | + * beyond a simple pinmap. For example, for modems wired to |
| 16 | + * directly control the RfSwitch, or to define the 'BUSY' pin for |
| 17 | + * SX126x series modems. |
| 18 | + * |
| 19 | + * If you find yourself needing to do a configuration in this way |
| 20 | + * for a commercially available development board, consider making |
| 21 | + * a PR to integrate your board to the library. |
| 22 | + * |
| 23 | + * Here, a Heltec Wireless Stick Lite V3 is configured manually |
| 24 | + * by creating a new class derived from |
| 25 | + * `Arduino_LMIC::HalConfiguration_t` to override default methods |
| 26 | + * of the base class and ensure proper operation of the SX1262 |
| 27 | + * modem. |
| 28 | + * |
| 29 | + * This example is otherwise identical to the `ttn-otaa` example. |
| 30 | + * |
| 31 | + * Note: LoRaWAN per sub-band duty-cycle limitation is enforced (1% in |
| 32 | + * g1, 0.1% in g2), but not the TTN fair usage policy (which is probably |
| 33 | + * violated by this sketch when left running for longer)! |
| 34 | +
|
| 35 | + * To use this sketch, first register your application and device with |
| 36 | + * the things network, to set or generate an AppEUI, DevEUI and AppKey. |
| 37 | + * Multiple devices can use the same AppEUI, but each device has its own |
| 38 | + * DevEUI and AppKey. |
| 39 | + * |
| 40 | + * Do not forget to define the radio type correctly in |
| 41 | + * arduino-lmic/project_config/lmic_project_config.h or from your BOARDS.txt. |
| 42 | + * |
| 43 | + *******************************************************************************/ |
| 44 | + |
| 45 | +#include <lmic.h> |
| 46 | +#include <hal/hal.h> |
| 47 | +#include <SPI.h> |
| 48 | + |
| 49 | +// |
| 50 | +// For normal use, we require that you edit the sketch to replace FILLMEIN |
| 51 | +// with values assigned by the TTN console. However, for regression tests, |
| 52 | +// we want to be able to compile these scripts. The regression tests define |
| 53 | +// COMPILE_REGRESSION_TEST, and in that case we define FILLMEIN to a non- |
| 54 | +// working but innocuous value. |
| 55 | +// |
| 56 | +#ifdef COMPILE_REGRESSION_TEST |
| 57 | +# define FILLMEIN 0 |
| 58 | +#else |
| 59 | +# warning "You must replace the values marked FILLMEIN with real values from the TTN control panel!" |
| 60 | +# define FILLMEIN (#dont edit this, edit the lines that use FILLMEIN) |
| 61 | +#endif |
| 62 | + |
| 63 | +// This EUI must be in little-endian format, so least-significant-byte |
| 64 | +// first. When copying an EUI from ttnctl output, this means to reverse |
| 65 | +// the bytes. For TTN issued EUIs the last bytes should be 0xD5, 0xB3, |
| 66 | +// 0x70. |
| 67 | +static const u1_t PROGMEM APPEUI[8]={ FILLMEIN }; |
| 68 | +void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8);} |
| 69 | + |
| 70 | +// This should also be in little endian format, see above. |
| 71 | +static const u1_t PROGMEM DEVEUI[8]={ FILLMEIN }; |
| 72 | +void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8);} |
| 73 | + |
| 74 | +// This key should be in big endian format (or, since it is not really a |
| 75 | +// number but a block of memory, endianness does not really apply). In |
| 76 | +// practice, a key taken from ttnctl can be copied as-is. |
| 77 | +static const u1_t PROGMEM APPKEY[16] = { FILLMEIN }; |
| 78 | +void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16);} |
| 79 | + |
| 80 | +static uint8_t mydata[] = "Hello, world!"; |
| 81 | +static osjob_t sendjob; |
| 82 | + |
| 83 | +// Schedule TX every this many seconds (might become longer due to duty |
| 84 | +// cycle limitations). |
| 85 | +const unsigned TX_INTERVAL = 60; |
| 86 | + |
| 87 | +// Advanced HalConfiguration |
| 88 | +// Example is for a Heltec Wireless Stick Lite V3 |
| 89 | +class cHalConfiguration_t: public Arduino_LMIC::HalConfiguration_t |
| 90 | +{ |
| 91 | +public: |
| 92 | + // All SX126x series modems need the busy pin to be defined |
| 93 | + // by overriding the `queryBusyPin()` method. |
| 94 | + virtual u1_t queryBusyPin(void) override { return 13; }; |
| 95 | + |
| 96 | + // SX126x series modems can be wired to use a DC-DC or LDO |
| 97 | + // voltage regulator. Configure DC-DC by overriding this method |
| 98 | + virtual bool queryUsingDcdc(void) override { return true; }; |
| 99 | + |
| 100 | + // SX126x series modems can be wired to so that DIO2 |
| 101 | + // controls an external RF switch. |
| 102 | + virtual bool queryUsingDIO2AsRfSwitch(void) override { return true; }; |
| 103 | + |
| 104 | + // Some modems switch a TCXO using DIO3. Configure by |
| 105 | + // overriding this method. |
| 106 | + virtual bool queryUsingDIO3AsTCXOSwitch(void) override { return true; }; |
| 107 | +}; |
| 108 | + |
| 109 | +cHalConfiguration_t myConfig; |
| 110 | + |
| 111 | +// Pin mapping |
| 112 | +const lmic_pinmap lmic_pins = { |
| 113 | + .nss = 8, |
| 114 | + .rxtx = LMIC_UNUSED_PIN, |
| 115 | + .rst = 12, |
| 116 | + .dio = {14, LMIC_UNUSED_PIN, LMIC_UNUSED_PIN}, |
| 117 | + .rxtx_rx_active = 0, |
| 118 | + .rssi_cal = 10, |
| 119 | + .spi_freq = 8000000, |
| 120 | + // Advanced configurations are passed to the pinmap via pConfig |
| 121 | + .pConfig = &myConfig, |
| 122 | +}; |
| 123 | + |
| 124 | +void printHex2(unsigned v) { |
| 125 | + v &= 0xff; |
| 126 | + if (v < 16) |
| 127 | + Serial.print('0'); |
| 128 | + Serial.print(v, HEX); |
| 129 | +} |
| 130 | + |
| 131 | +void onEvent (ev_t ev) { |
| 132 | + Serial.print(os_getTime()); |
| 133 | + Serial.print(": "); |
| 134 | + switch(ev) { |
| 135 | + case EV_SCAN_TIMEOUT: |
| 136 | + Serial.println(F("EV_SCAN_TIMEOUT")); |
| 137 | + break; |
| 138 | + case EV_BEACON_FOUND: |
| 139 | + Serial.println(F("EV_BEACON_FOUND")); |
| 140 | + break; |
| 141 | + case EV_BEACON_MISSED: |
| 142 | + Serial.println(F("EV_BEACON_MISSED")); |
| 143 | + break; |
| 144 | + case EV_BEACON_TRACKED: |
| 145 | + Serial.println(F("EV_BEACON_TRACKED")); |
| 146 | + break; |
| 147 | + case EV_JOINING: |
| 148 | + Serial.println(F("EV_JOINING")); |
| 149 | + break; |
| 150 | + case EV_JOINED: |
| 151 | + Serial.println(F("EV_JOINED")); |
| 152 | + { |
| 153 | + u4_t netid = 0; |
| 154 | + devaddr_t devaddr = 0; |
| 155 | + u1_t nwkKey[16]; |
| 156 | + u1_t artKey[16]; |
| 157 | + LMIC_getSessionKeys(&netid, &devaddr, nwkKey, artKey); |
| 158 | + Serial.print("netid: "); |
| 159 | + Serial.println(netid, DEC); |
| 160 | + Serial.print("devaddr: "); |
| 161 | + Serial.println(devaddr, HEX); |
| 162 | + Serial.print("AppSKey: "); |
| 163 | + for (size_t i=0; i<sizeof(artKey); ++i) { |
| 164 | + if (i != 0) |
| 165 | + Serial.print("-"); |
| 166 | + printHex2(artKey[i]); |
| 167 | + } |
| 168 | + Serial.println(""); |
| 169 | + Serial.print("NwkSKey: "); |
| 170 | + for (size_t i=0; i<sizeof(nwkKey); ++i) { |
| 171 | + if (i != 0) |
| 172 | + Serial.print("-"); |
| 173 | + printHex2(nwkKey[i]); |
| 174 | + } |
| 175 | + Serial.println(); |
| 176 | + } |
| 177 | + // Disable link check validation (automatically enabled |
| 178 | + // during join, but because slow data rates change max TX |
| 179 | + // size, we don't use it in this example. |
| 180 | + LMIC_setLinkCheckMode(0); |
| 181 | + break; |
| 182 | + /* |
| 183 | + || This event is defined but not used in the code. No |
| 184 | + || point in wasting codespace on it. |
| 185 | + || |
| 186 | + || case EV_RFU1: |
| 187 | + || Serial.println(F("EV_RFU1")); |
| 188 | + || break; |
| 189 | + */ |
| 190 | + case EV_JOIN_FAILED: |
| 191 | + Serial.println(F("EV_JOIN_FAILED")); |
| 192 | + break; |
| 193 | + case EV_REJOIN_FAILED: |
| 194 | + Serial.println(F("EV_REJOIN_FAILED")); |
| 195 | + break; |
| 196 | + case EV_TXCOMPLETE: |
| 197 | + Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)")); |
| 198 | + if (LMIC.txrxFlags & TXRX_ACK) |
| 199 | + Serial.println(F("Received ack")); |
| 200 | + if (LMIC.dataLen) { |
| 201 | + Serial.print(F("Received ")); |
| 202 | + Serial.print(LMIC.dataLen); |
| 203 | + Serial.println(F(" bytes of payload")); |
| 204 | + } |
| 205 | + // Schedule next transmission |
| 206 | + os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send); |
| 207 | + break; |
| 208 | + case EV_LOST_TSYNC: |
| 209 | + Serial.println(F("EV_LOST_TSYNC")); |
| 210 | + break; |
| 211 | + case EV_RESET: |
| 212 | + Serial.println(F("EV_RESET")); |
| 213 | + break; |
| 214 | + case EV_RXCOMPLETE: |
| 215 | + // data received in ping slot |
| 216 | + Serial.println(F("EV_RXCOMPLETE")); |
| 217 | + break; |
| 218 | + case EV_LINK_DEAD: |
| 219 | + Serial.println(F("EV_LINK_DEAD")); |
| 220 | + break; |
| 221 | + case EV_LINK_ALIVE: |
| 222 | + Serial.println(F("EV_LINK_ALIVE")); |
| 223 | + break; |
| 224 | + /* |
| 225 | + || This event is defined but not used in the code. No |
| 226 | + || point in wasting codespace on it. |
| 227 | + || |
| 228 | + || case EV_SCAN_FOUND: |
| 229 | + || Serial.println(F("EV_SCAN_FOUND")); |
| 230 | + || break; |
| 231 | + */ |
| 232 | + case EV_TXSTART: |
| 233 | + Serial.println(F("EV_TXSTART")); |
| 234 | + break; |
| 235 | + case EV_TXCANCELED: |
| 236 | + Serial.println(F("EV_TXCANCELED")); |
| 237 | + break; |
| 238 | + case EV_RXSTART: |
| 239 | + /* do not print anything -- it wrecks timing */ |
| 240 | + break; |
| 241 | + case EV_JOIN_TXCOMPLETE: |
| 242 | + Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept")); |
| 243 | + break; |
| 244 | + |
| 245 | + default: |
| 246 | + Serial.print(F("Unknown event: ")); |
| 247 | + Serial.println((unsigned) ev); |
| 248 | + break; |
| 249 | + } |
| 250 | +} |
| 251 | + |
| 252 | +void do_send(osjob_t* j){ |
| 253 | + // Check if there is not a current TX/RX job running |
| 254 | + if (LMIC.opmode & OP_TXRXPEND) { |
| 255 | + Serial.println(F("OP_TXRXPEND, not sending")); |
| 256 | + } else { |
| 257 | + // Prepare upstream data transmission at the next possible time. |
| 258 | + LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0); |
| 259 | + Serial.println(F("Packet queued")); |
| 260 | + } |
| 261 | + // Next TX is scheduled after TX_COMPLETE event. |
| 262 | +} |
| 263 | + |
| 264 | +void setup() { |
| 265 | + Serial.begin(9600); |
| 266 | + Serial.println(F("Starting")); |
| 267 | + |
| 268 | + #ifdef VCC_ENABLE |
| 269 | + // For Pinoccio Scout boards |
| 270 | + pinMode(VCC_ENABLE, OUTPUT); |
| 271 | + digitalWrite(VCC_ENABLE, HIGH); |
| 272 | + delay(1000); |
| 273 | + #endif |
| 274 | + |
| 275 | + // LMIC init |
| 276 | + os_init(); |
| 277 | + // Reset the MAC state. Session and pending data transfers will be discarded. |
| 278 | + LMIC_reset(); |
| 279 | + |
| 280 | + // Start job (sending automatically starts OTAA too) |
| 281 | + do_send(&sendjob); |
| 282 | +} |
| 283 | + |
| 284 | +void loop() { |
| 285 | + os_runloop_once(); |
| 286 | +} |
0 commit comments