|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman |
| 3 | + * Copyright (c) 2018 Terry Moore, MCCI |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to anyone |
| 6 | + * obtaining a copy of this document and accompanying files, |
| 7 | + * to do whatever they want with them without any restriction, |
| 8 | + * including, but not limited to, copying, modification and redistribution. |
| 9 | + * NO WARRANTY OF ANY KIND IS PROVIDED. |
| 10 | + * |
| 11 | + * This example sends a valid LoRaWAN packet with payload "Hello, |
| 12 | + * world!", using frequency and encryption settings matching those of |
| 13 | + * the The Things Network. |
| 14 | + * |
| 15 | + * This uses ABP (Activation-by-personalisation), where a DevAddr and |
| 16 | + * Session keys are preconfigured (unlike OTAA, where a DevEUI and |
| 17 | + * application key is configured, while the DevAddr and session keys are |
| 18 | + * assigned/generated in the over-the-air-activation procedure). |
| 19 | + * |
| 20 | + * Note: LoRaWAN per sub-band duty-cycle limitation is enforced (1% in |
| 21 | + * g1, 0.1% in g2), but not the TTN fair usage policy (which is probably |
| 22 | + * violated by this sketch when left running for longer)! |
| 23 | + * |
| 24 | + * To use this sketch, first register your application and device with |
| 25 | + * the things network, to set or generate a DevAddr, NwkSKey and |
| 26 | + * AppSKey. Each device should have their own unique values for these |
| 27 | + * fields. |
| 28 | + * |
| 29 | + * Do not forget to define the radio type correctly in |
| 30 | + * arduino-lmic/project_config/lmic_project_config.h or from your BOARDS.txt. |
| 31 | + * |
| 32 | + *******************************************************************************/ |
| 33 | + |
| 34 | + // References: |
| 35 | + // [feather] adafruit-feather-m0-radio-with-lora-module.pdf |
| 36 | + |
| 37 | +#include <lmic.h> |
| 38 | +#include <hal/hal.h> |
| 39 | +#include <SPI.h> |
| 40 | + |
| 41 | +// |
| 42 | +// For normal use, we require that you edit the sketch to replace FILLMEIN |
| 43 | +// with values assigned by the TTN console. However, for regression tests, |
| 44 | +// we want to be able to compile these scripts. The regression tests define |
| 45 | +// COMPILE_REGRESSION_TEST, and in that case we define FILLMEIN to a non- |
| 46 | +// working but innocuous value. |
| 47 | +// |
| 48 | +#ifdef COMPILE_REGRESSION_TEST |
| 49 | +# define FILLMEIN 0 |
| 50 | +#else |
| 51 | +# warning "You must replace the values marked FILLMEIN with real values from the TTN control panel!" |
| 52 | +# define FILLMEIN (#dont edit this, edit the lines that use FILLMEIN) |
| 53 | +#endif |
| 54 | + |
| 55 | +// LoRaWAN NwkSKey, network session key |
| 56 | +// This should be in big-endian (aka msb). |
| 57 | +static const PROGMEM u1_t NWKSKEY[16] = { FILLMEIN }; |
| 58 | + |
| 59 | +// LoRaWAN AppSKey, application session key |
| 60 | +// This should also be in big-endian (aka msb). |
| 61 | +static const u1_t PROGMEM APPSKEY[16] = { FILLMEIN }; |
| 62 | + |
| 63 | +// LoRaWAN end-device address (DevAddr) |
| 64 | +// See http://thethingsnetwork.org/wiki/AddressSpace |
| 65 | +// The library converts the address to network byte order as needed, so this should be in big-endian (aka msb) too. |
| 66 | +static const u4_t DEVADDR = FILLMEIN ; // <-- Change this address for every node! |
| 67 | + |
| 68 | +// These callbacks are only used in over-the-air activation, so they are |
| 69 | +// left empty here (we cannot leave them out completely unless |
| 70 | +// DISABLE_JOIN is set in arduino-lmic/project_config/lmic_project_config.h, |
| 71 | +// otherwise the linker will complain). |
| 72 | +void os_getArtEui (u1_t* buf) { } |
| 73 | +void os_getDevEui (u1_t* buf) { } |
| 74 | +void os_getDevKey (u1_t* buf) { } |
| 75 | + |
| 76 | +static uint8_t mydata[] = "Hello, world!"; |
| 77 | +static osjob_t sendjob; |
| 78 | + |
| 79 | +// Schedule TX every this many seconds (might become longer due to duty |
| 80 | +// cycle limitations). |
| 81 | +const unsigned TX_INTERVAL = 60; |
| 82 | + |
| 83 | +// Pin mapping |
| 84 | +// Adapted for Feather M0 per p.10 of [feather] |
| 85 | +const lmic_pinmap lmic_pins = { |
| 86 | + .nss = 8, // chip select on feather (rf95module) CS |
| 87 | + .rxtx = LMIC_UNUSED_PIN, |
| 88 | + .rst = 4, // reset pin |
| 89 | + .dio = {6, 5, LMIC_UNUSED_PIN}, // assumes external jumpers [feather_lora_jumper] |
| 90 | + // DIO1 is on JP1-1: is io1 - we connect to GPO6 |
| 91 | + // DIO1 is on JP5-3: is D2 - we connect to GPO5 |
| 92 | +}; |
| 93 | + |
| 94 | +void onEvent (ev_t ev) { |
| 95 | + Serial.print(os_getTime()); |
| 96 | + Serial.print(": "); |
| 97 | + switch(ev) { |
| 98 | + case EV_SCAN_TIMEOUT: |
| 99 | + Serial.println(F("EV_SCAN_TIMEOUT")); |
| 100 | + break; |
| 101 | + case EV_BEACON_FOUND: |
| 102 | + Serial.println(F("EV_BEACON_FOUND")); |
| 103 | + break; |
| 104 | + case EV_BEACON_MISSED: |
| 105 | + Serial.println(F("EV_BEACON_MISSED")); |
| 106 | + break; |
| 107 | + case EV_BEACON_TRACKED: |
| 108 | + Serial.println(F("EV_BEACON_TRACKED")); |
| 109 | + break; |
| 110 | + case EV_JOINING: |
| 111 | + Serial.println(F("EV_JOINING")); |
| 112 | + break; |
| 113 | + case EV_JOINED: |
| 114 | + Serial.println(F("EV_JOINED")); |
| 115 | + break; |
| 116 | + /* |
| 117 | + || This event is defined but not used in the code. No |
| 118 | + || point in wasting codespace on it. |
| 119 | + || |
| 120 | + || case EV_RFU1: |
| 121 | + || Serial.println(F("EV_RFU1")); |
| 122 | + || break; |
| 123 | + */ |
| 124 | + case EV_JOIN_FAILED: |
| 125 | + Serial.println(F("EV_JOIN_FAILED")); |
| 126 | + break; |
| 127 | + case EV_REJOIN_FAILED: |
| 128 | + Serial.println(F("EV_REJOIN_FAILED")); |
| 129 | + break; |
| 130 | + case EV_TXCOMPLETE: |
| 131 | + Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)")); |
| 132 | + if (LMIC.txrxFlags & TXRX_ACK) |
| 133 | + Serial.println(F("Received ack")); |
| 134 | + if (LMIC.dataLen) { |
| 135 | + Serial.println(F("Received ")); |
| 136 | + Serial.println(LMIC.dataLen); |
| 137 | + Serial.println(F(" bytes of payload")); |
| 138 | + } |
| 139 | + // Schedule next transmission |
| 140 | + os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send); |
| 141 | + break; |
| 142 | + case EV_LOST_TSYNC: |
| 143 | + Serial.println(F("EV_LOST_TSYNC")); |
| 144 | + break; |
| 145 | + case EV_RESET: |
| 146 | + Serial.println(F("EV_RESET")); |
| 147 | + break; |
| 148 | + case EV_RXCOMPLETE: |
| 149 | + // data received in ping slot |
| 150 | + Serial.println(F("EV_RXCOMPLETE")); |
| 151 | + break; |
| 152 | + case EV_LINK_DEAD: |
| 153 | + Serial.println(F("EV_LINK_DEAD")); |
| 154 | + break; |
| 155 | + case EV_LINK_ALIVE: |
| 156 | + Serial.println(F("EV_LINK_ALIVE")); |
| 157 | + break; |
| 158 | + /* |
| 159 | + || This event is defined but not used in the code. No |
| 160 | + || point in wasting codespace on it. |
| 161 | + || |
| 162 | + || case EV_SCAN_FOUND: |
| 163 | + || Serial.println(F("EV_SCAN_FOUND")); |
| 164 | + || break; |
| 165 | + */ |
| 166 | + case EV_TXSTART: |
| 167 | + Serial.println(F("EV_TXSTART")); |
| 168 | + break; |
| 169 | + case EV_TXCANCELED: |
| 170 | + Serial.println(F("EV_TXCANCELED")); |
| 171 | + break; |
| 172 | + case EV_RXSTART: |
| 173 | + /* do not print anything -- it wrecks timing */ |
| 174 | + break; |
| 175 | + case EV_JOIN_TXCOMPLETE: |
| 176 | + Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept")); |
| 177 | + break; |
| 178 | + default: |
| 179 | + Serial.print(F("Unknown event: ")); |
| 180 | + Serial.println((unsigned) ev); |
| 181 | + break; |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +void do_send(osjob_t* j){ |
| 186 | + // Check if there is not a current TX/RX job running |
| 187 | + if (LMIC.opmode & OP_TXRXPEND) { |
| 188 | + Serial.println(F("OP_TXRXPEND, not sending")); |
| 189 | + } else { |
| 190 | + // Prepare upstream data transmission at the next possible time. |
| 191 | + LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0); |
| 192 | + Serial.println(F("Packet queued")); |
| 193 | + } |
| 194 | + // Next TX is scheduled after TX_COMPLETE event. |
| 195 | +} |
| 196 | + |
| 197 | +void setup() { |
| 198 | +// pinMode(13, OUTPUT); |
| 199 | + while (!Serial); // wait for Serial to be initialized |
| 200 | + Serial.begin(115200); |
| 201 | + delay(100); // per sample code on RF_95 test |
| 202 | + Serial.println(F("Starting")); |
| 203 | + |
| 204 | + #ifdef VCC_ENABLE |
| 205 | + // For Pinoccio Scout boards |
| 206 | + pinMode(VCC_ENABLE, OUTPUT); |
| 207 | + digitalWrite(VCC_ENABLE, HIGH); |
| 208 | + delay(1000); |
| 209 | + #endif |
| 210 | + |
| 211 | + // LMIC init |
| 212 | + os_init(); |
| 213 | + // Reset the MAC state. Session and pending data transfers will be discarded. |
| 214 | + LMIC_reset(); |
| 215 | + |
| 216 | + // Set static session parameters. Instead of dynamically establishing a session |
| 217 | + // by joining the network, precomputed session parameters are be provided. |
| 218 | + #ifdef PROGMEM |
| 219 | + // On AVR, these values are stored in flash and only copied to RAM |
| 220 | + // once. Copy them to a temporary buffer here, LMIC_setSession will |
| 221 | + // copy them into a buffer of its own again. |
| 222 | + uint8_t appskey[sizeof(APPSKEY)]; |
| 223 | + uint8_t nwkskey[sizeof(NWKSKEY)]; |
| 224 | + memcpy_P(appskey, APPSKEY, sizeof(APPSKEY)); |
| 225 | + memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY)); |
| 226 | + LMIC_setSession (0x13, DEVADDR, nwkskey, appskey); |
| 227 | + #else |
| 228 | + // If not running an AVR with PROGMEM, just use the arrays directly |
| 229 | + LMIC_setSession (0x13, DEVADDR, NWKSKEY, APPSKEY); |
| 230 | + #endif |
| 231 | + |
| 232 | + #if defined(CFG_eu868) |
| 233 | + // Set up the channels used by the Things Network, which corresponds |
| 234 | + // to the defaults of most gateways. Without this, only three base |
| 235 | + // channels from the LoRaWAN specification are used, which certainly |
| 236 | + // works, so it is good for debugging, but can overload those |
| 237 | + // frequencies, so be sure to configure the full frequency range of |
| 238 | + // your network here (unless your network autoconfigures them). |
| 239 | + // Setting up channels should happen after LMIC_setSession, as that |
| 240 | + // configures the minimal channel set. The LMIC doesn't let you change |
| 241 | + // the three basic settings, but we show them here. |
| 242 | + LMIC_setupChannel(0, 868100000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band |
| 243 | + LMIC_setupChannel(1, 868300000, DR_RANGE_MAP(DR_SF12, DR_SF7B), BAND_CENTI); // g-band |
| 244 | + LMIC_setupChannel(2, 868500000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band |
| 245 | + LMIC_setupChannel(3, 867100000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band |
| 246 | + LMIC_setupChannel(4, 867300000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band |
| 247 | + LMIC_setupChannel(5, 867500000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band |
| 248 | + LMIC_setupChannel(6, 867700000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band |
| 249 | + LMIC_setupChannel(7, 867900000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band |
| 250 | + LMIC_setupChannel(8, 868800000, DR_RANGE_MAP(DR_FSK, DR_FSK), BAND_MILLI); // g2-band |
| 251 | + // TTN defines an additional channel at 869.525Mhz using SF9 for class B |
| 252 | + // devices' ping slots. LMIC does not have an easy way to define set this |
| 253 | + // frequency and support for class B is spotty and untested, so this |
| 254 | + // frequency is not configured here. |
| 255 | + #elif defined(CFG_us915) || defined(CFG_au915) |
| 256 | + // NA-US and AU channels 0-71 are configured automatically |
| 257 | + // but only one group of 8 should (a subband) should be active |
| 258 | + // TTN recommends the second sub band, 1 in a zero based count. |
| 259 | + // https://github.com/TheThingsNetwork/gateway-conf/blob/master/US-global_conf.json |
| 260 | + LMIC_selectSubBand(1); |
| 261 | + #elif defined(CFG_as923) |
| 262 | + // Set up the channels used in your country. Only two are defined by default, |
| 263 | + // and they cannot be changed. Use BAND_CENTI to indicate 1% duty cycle. |
| 264 | + // LMIC_setupChannel(0, 923200000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); |
| 265 | + // LMIC_setupChannel(1, 923400000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); |
| 266 | + |
| 267 | + // ... extra definitions for channels 2..n here |
| 268 | + #elif defined(CFG_kr920) |
| 269 | + // Set up the channels used in your country. Three are defined by default, |
| 270 | + // and they cannot be changed. Duty cycle doesn't matter, but is conventionally |
| 271 | + // BAND_MILLI. |
| 272 | + // LMIC_setupChannel(0, 922100000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_MILLI); |
| 273 | + // LMIC_setupChannel(1, 922300000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_MILLI); |
| 274 | + // LMIC_setupChannel(2, 922500000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_MILLI); |
| 275 | + |
| 276 | + // ... extra definitions for channels 3..n here. |
| 277 | + #elif defined(CFG_in866) |
| 278 | + // Set up the channels used in your country. Three are defined by default, |
| 279 | + // and they cannot be changed. Duty cycle doesn't matter, but is conventionally |
| 280 | + // BAND_MILLI. |
| 281 | + // LMIC_setupChannel(0, 865062500, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_MILLI); |
| 282 | + // LMIC_setupChannel(1, 865402500, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_MILLI); |
| 283 | + // LMIC_setupChannel(2, 865985000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_MILLI); |
| 284 | + |
| 285 | + // ... extra definitions for channels 3..n here. |
| 286 | + #else |
| 287 | + # error Region not supported |
| 288 | + #endif |
| 289 | + |
| 290 | + // Disable link check validation |
| 291 | + LMIC_setLinkCheckMode(0); |
| 292 | + |
| 293 | + // TTN uses SF9 for its RX2 window. |
| 294 | + LMIC.dn2Dr = DR_SF9; |
| 295 | + |
| 296 | + // Set data rate and transmit power for uplink |
| 297 | + LMIC_setDrTxpow(DR_SF7,14); |
| 298 | + |
| 299 | + // Start job |
| 300 | + do_send(&sendjob); |
| 301 | +} |
| 302 | + |
| 303 | +void loop() { |
| 304 | + unsigned long now; |
| 305 | + now = millis(); |
| 306 | + if ((now & 512) != 0) { |
| 307 | + digitalWrite(13, HIGH); |
| 308 | + } |
| 309 | + else { |
| 310 | + digitalWrite(13, LOW); |
| 311 | + } |
| 312 | + |
| 313 | + os_runloop_once(); |
| 314 | + |
| 315 | +} |
0 commit comments