Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. #include <lmic.h>
  2. #include <hal/hal.h>
  3. #include <SPI.h>
  4.  
  5. // LoRaWAN NwkSKey, network session key
  6. // This is the default Semtech key, which is used by the prototype TTN
  7. // network initially.
  8. //ttn
  9. static const PROGMEM u1_t NWKSKEY[16] = { 0x11, 0xB5, 0x16, 0xC4, 0x6D, 0x2A, 0x78, 0x33, 0x02, 0x38, 0x46, 0xD4, 0x59, 0xC1, 0xEC, 0xA1 };
  10. // LoRaWAN AppSKey, application session key
  11. // This is the default Semtech key, which is used by the prototype TTN
  12. // network initially.
  13. //ttn
  14. static const u1_t PROGMEM APPSKEY[16] = { 0x87, 0x30, 0xE5, 0x28, 0xAD, 0x05, 0x54, 0x2E, 0x44, 0x6D, 0xFF, 0x4E, 0xA3, 0x4A, 0x07, 0xBF };
  15.  
  16. //
  17. // LoRaWAN end-device address (DevAddr)
  18. // See http://thethingsnetwork.org/wiki/AddressSpace
  19. // ttn
  20. static const u4_t DEVADDR = 0x26011F54;
  21.  
  22.  
  23. // These callbacks are only used in over-the-air activation, so they are
  24. // left empty here (we cannot leave them out completely unless
  25. // DISABLE_JOIN is set in config.h, otherwise the linker will complain).
  26. void os_getArtEui (u1_t* buf) { }
  27. void os_getDevEui (u1_t* buf) { }
  28. void os_getDevKey (u1_t* buf) { }
  29.  
  30. static uint8_t mydata[] = "DEVADDR";
  31. static osjob_t initjob,sendjob,blinkjob;
  32.  
  33. // Schedule TX every this many seconds (might become longer due to duty
  34. // cycle limitations).
  35. const unsigned TX_INTERVAL = 20;
  36.  
  37. // Pin mapping
  38. const lmic_pinmap lmic_pins = {
  39. .nss = 10,
  40. .rxtx = LMIC_UNUSED_PIN,
  41. .rst = 9,
  42. .dio = {2, 6, 7},
  43. };
  44. void do_send(osjob_t* j){
  45. // Check if there is not a current TX/RX job running
  46. if (LMIC.opmode & OP_TXRXPEND) {
  47. Serial.println("OP_TXRXPEND, not sending");
  48. } else {
  49. // Prepare upstream data transmission at the next possible time.
  50. LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0);
  51. Serial.println("Packet queued");
  52. Serial.println(LMIC.freq);
  53. }
  54. // Next TX is scheduled after TX_COMPLETE event.
  55. }
  56.  
  57. void onEvent (ev_t ev) {
  58. Serial.print(os_getTime());
  59. Serial.print(": ");
  60. Serial.println(ev);
  61. switch(ev) {
  62. case EV_SCAN_TIMEOUT:
  63. Serial.println("EV_SCAN_TIMEOUT");
  64. break;
  65. case EV_BEACON_FOUND:
  66. Serial.println("EV_BEACON_FOUND");
  67. break;
  68. case EV_BEACON_MISSED:
  69. Serial.println("EV_BEACON_MISSED");
  70. break;
  71. case EV_BEACON_TRACKED:
  72. Serial.println("EV_BEACON_TRACKED");
  73. break;
  74. case EV_JOINING:
  75. Serial.println("EV_JOINING");
  76. break;
  77. case EV_JOINED:
  78. Serial.println("EV_JOINED");
  79. break;
  80. case EV_RFU1:
  81. Serial.println("EV_RFU1");
  82. break;
  83. case EV_JOIN_FAILED:
  84. Serial.println("EV_JOIN_FAILED");
  85. break;
  86. case EV_REJOIN_FAILED:
  87. Serial.println("EV_REJOIN_FAILED");
  88. break;
  89. case EV_TXCOMPLETE:
  90. Serial.println("EV_TXCOMPLETE (includes waiting for RX windows)");
  91. if(LMIC.dataLen) {
  92. // data received in rx slot after tx
  93. Serial.print("Data Received: ");
  94. Serial.write(LMIC.frame+LMIC.dataBeg, LMIC.dataLen);
  95. Serial.println();
  96. }
  97. // Schedule next transmission
  98. os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
  99. break;
  100. case EV_LOST_TSYNC:
  101. Serial.println("EV_LOST_TSYNC");
  102. break;
  103. case EV_RESET:
  104. Serial.println("EV_RESET");
  105. break;
  106. case EV_RXCOMPLETE:
  107. // data received in ping slot
  108. Serial.println("EV_RXCOMPLETE");
  109. break;
  110. case EV_LINK_DEAD:
  111. Serial.println("EV_LINK_DEAD");
  112. break;
  113. case EV_LINK_ALIVE:
  114. Serial.println("EV_LINK_ALIVE");
  115. break;
  116. default:
  117. Serial.println("Unknown event");
  118. break;
  119. }
  120. }
  121.  
  122. void setup() {
  123. Serial.begin(9600);
  124. while(!Serial);
  125. Serial.println("Starting");
  126. #ifdef VCC_ENABLE
  127. // For Pinoccio Scout boards
  128. pinMode(VCC_ENABLE, OUTPUT);
  129. digitalWrite(VCC_ENABLE, HIGH);
  130. delay(1000);
  131. #endif
  132.  
  133. // LMIC init
  134. os_init();
  135. // Reset the MAC state. Session and pending data transfers will be discarded.
  136. LMIC_reset();
  137. //LMIC_setClockError(MAX_CLOCK_ERROR * 1/100);
  138. // Set static session parameters. Instead of dynamically establishing a session
  139. // by joining the network, precomputed session parameters are be provided.
  140. #ifdef PROGMEM
  141. // On AVR, these values are stored in flash and only copied to RAM
  142. // once. Copy them to a temporary buffer here, LMIC_setSession will
  143. // copy them into a buffer of its own again.
  144. uint8_t appskey[sizeof(APPSKEY)];
  145. uint8_t nwkskey[sizeof(NWKSKEY)];
  146. memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
  147. memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
  148. LMIC_setSession (0x1, DEVADDR, nwkskey, appskey);
  149. #else
  150. // If not running an AVR with PROGMEM, just use the arrays directly
  151. LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY);
  152. #endif
  153.  
  154. // Disable link check validation
  155. LMIC_setLinkCheckMode(0);
  156.  
  157. // TTN uses SF9 for its RX2 window.
  158. LMIC.dn2Dr = DR_SF9;
  159.  
  160. // Set data rate and transmit power (note: txpow seems to be ignored by the library)
  161. LMIC_setDrTxpow(DR_SF7,14);
  162.  
  163. // Start job
  164. do_send(&sendjob);
  165. }
  166.  
  167. void loop() {
  168. os_runloop_once();
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement