Advertisement
Guest User

Adafruit Feather Control LED

a guest
Aug 3rd, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. /*******************************************************************************
  2. * Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman
  3. *
  4. * Permission is hereby granted, free of charge, to anyone
  5. * obtaining a copy of this document and accompanying files,
  6. * to do whatever they want with them without any restriction,
  7. * including, but not limited to, copying, modification and redistribution.
  8. * NO WARRANTY OF ANY KIND IS PROVIDED.
  9. *
  10. * This example sends a valid LoRaWAN packet with payload "Hello,
  11. * world!", using frequency and encryption settings matching those of
  12. * the The Things Network.
  13. *
  14. * This uses OTAA (Over-the-air activation), where where a DevEUI and
  15. * application key is configured, which are used in an over-the-air
  16. * activation procedure where a DevAddr and session keys are
  17. * assigned/generated for use with all further communication.
  18. *
  19. * Note: LoRaWAN per sub-band duty-cycle limitation is enforced (1% in
  20. * g1, 0.1% in g2), but not the TTN fair usage policy (which is probably
  21. * violated by this sketch when left running for longer)!
  22.  
  23. * To use this sketch, first register your application and device with
  24. * the things network, to set or generate an AppEUI, DevEUI and AppKey.
  25. * Multiple devices can use the same AppEUI, but each device has its own
  26. * DevEUI and AppKey.
  27. *
  28. * Do not forget to define the radio type correctly in config.h.
  29. *
  30. *******************************************************************************/
  31.  
  32. #include <lmic.h>
  33. #include <hal/hal.h>
  34. #include <SPI.h>
  35.  
  36. // This EUI must be in little-endian format, so least-significant-byte
  37. // first. When copying an EUI from ttnctl output, this means to reverse
  38. // the bytes. For TTN issued EUIs the last bytes should be 0xD5, 0xB3,
  39. // 0x70.
  40. static const u1_t PROGMEM APPEUI[8]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  41. void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8);}
  42.  
  43. // This should also be in little endian format, see above.
  44. static const u1_t PROGMEM DEVEUI[8]={0x07, 0xc5, 0xd8, 0xa0, 0xb4, 0xf6, 0xa4, 0xd1};
  45. void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8);}
  46.  
  47. // This key should be in big endian format (or, since it is not really a
  48. // number but a block of memory, endianness does not really apply). In
  49. // practice, a key taken from ttnctl can be copied as-is.
  50. // The key shown here is the semtech default key.
  51. static const u1_t PROGMEM APPKEY[16] = {0xdb, 0xd7, 0xf1, 0x43, 0xfd, 0x54, 0x4c, 0x61, 0x89, 0x96, 0xb8, 0x08, 0x60, 0x9e, 0x4e, 0x32};
  52. void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16);}
  53.  
  54. static uint8_t mydata[] = "Hello, world!";
  55. static osjob_t sendjob;
  56.  
  57. // Schedule TX every this many seconds (might become longer due to duty
  58. // cycle limitations).
  59. const unsigned TX_INTERVAL = 30;
  60.  
  61. // Pin mapping
  62. const lmic_pinmap lmic_pins = {
  63. .nss = 8,
  64. .rxtx = LMIC_UNUSED_PIN,
  65. .rst = 4,
  66. .dio = {7, 6, LMIC_UNUSED_PIN},
  67. };
  68.  
  69. //------ Added ----------------
  70. #define LED_YELLOW 3
  71. #define LED_GREEN 2
  72. //-----------------------------
  73.  
  74. void onEvent (ev_t ev) {
  75. Serial.print(os_getTime());
  76. Serial.print(": ");
  77. switch(ev) {
  78. case EV_SCAN_TIMEOUT:
  79. Serial.println(F("EV_SCAN_TIMEOUT"));
  80. break;
  81. case EV_BEACON_FOUND:
  82. Serial.println(F("EV_BEACON_FOUND"));
  83. break;
  84. case EV_BEACON_MISSED:
  85. Serial.println(F("EV_BEACON_MISSED"));
  86. break;
  87. case EV_BEACON_TRACKED:
  88. Serial.println(F("EV_BEACON_TRACKED"));
  89. break;
  90. case EV_JOINING:
  91. Serial.println(F("EV_JOINING"));
  92. break;
  93. case EV_JOINED:
  94. Serial.println(F("EV_JOINED"));
  95.  
  96. // Disable link check validation (automatically enabled
  97. // during join, but not supported by TTN at this time).
  98. LMIC_setLinkCheckMode(0);
  99. break;
  100. case EV_RFU1:
  101. Serial.println(F("EV_RFU1"));
  102. break;
  103. case EV_JOIN_FAILED:
  104. Serial.println(F("EV_JOIN_FAILED"));
  105. break;
  106. case EV_REJOIN_FAILED:
  107. Serial.println(F("EV_REJOIN_FAILED"));
  108. break;
  109. break;
  110. case EV_TXCOMPLETE:
  111. Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
  112. if (LMIC.txrxFlags & TXRX_ACK)
  113. Serial.println(F("Received ack"));
  114. if (LMIC.dataLen) {
  115. Serial.print(F("Received "));
  116. Serial.print(LMIC.dataLen);
  117. Serial.println(F(" bytes of payload"));
  118.  
  119. //------ Added ----------------
  120. if (LMIC.dataLen == 1) {
  121. uint8_t result = LMIC.frame[LMIC.dataBeg + 0];
  122. if (result == 0) {
  123. Serial.println("RESULT 0");
  124. digitalWrite(LED_YELLOW, LOW);
  125. digitalWrite(LED_GREEN, LOW);
  126. }
  127. if (result == 1) {
  128. Serial.println("RESULT 1");
  129. digitalWrite(LED_YELLOW, HIGH);
  130. digitalWrite(LED_GREEN, LOW);
  131. }
  132. if (result == 2) {
  133. Serial.println("RESULT 2");
  134. digitalWrite(LED_YELLOW, LOW);
  135. digitalWrite(LED_GREEN, HIGH);
  136. }
  137. if (result == 3) {
  138. Serial.println("RESULT 3");
  139. digitalWrite(LED_YELLOW, HIGH);
  140. digitalWrite(LED_GREEN, HIGH);
  141. }
  142. }
  143. }
  144.  
  145. // Schedule next transmission
  146. os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
  147. break;
  148. case EV_LOST_TSYNC:
  149. Serial.println(F("EV_LOST_TSYNC"));
  150. break;
  151. case EV_RESET:
  152. Serial.println(F("EV_RESET"));
  153. break;
  154. case EV_RXCOMPLETE:
  155. // data received in ping slot
  156. Serial.println(F("EV_RXCOMPLETE"));
  157. break;
  158. case EV_LINK_DEAD:
  159. Serial.println(F("EV_LINK_DEAD"));
  160. break;
  161. case EV_LINK_ALIVE:
  162. Serial.println(F("EV_LINK_ALIVE"));
  163. break;
  164. default:
  165. Serial.println(F("Unknown event"));
  166. break;
  167. }
  168. }
  169.  
  170. void do_send(osjob_t* j){
  171. // Check if there is not a current TX/RX job running
  172. if (LMIC.opmode & OP_TXRXPEND) {
  173. Serial.println(F("OP_TXRXPEND, not sending"));
  174. } else {
  175. // Prepare upstream data transmission at the next possible time.
  176. LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0);
  177. Serial.println(F("Packet queued"));
  178. }
  179. // Next TX is scheduled after TX_COMPLETE event.
  180. }
  181.  
  182. void setup() {
  183. Serial.begin(9600);
  184. Serial.println(F("Starting"));
  185.  
  186. //------ Added ----------------
  187. pinMode(LED_YELLOW, OUTPUT);
  188. pinMode(LED_GREEN, OUTPUT);
  189. //-----------------------------
  190.  
  191. #ifdef VCC_ENABLE
  192. // For Pinoccio Scout boards
  193. pinMode(VCC_ENABLE, OUTPUT);
  194. digitalWrite(VCC_ENABLE, HIGH);
  195. delay(1000);
  196. #endif
  197.  
  198. // LMIC init
  199. os_init();
  200. // Reset the MAC state. Session and pending data transfers will be discarded.
  201. LMIC_reset();
  202.  
  203. // Start job (sending automatically starts OTAA too)
  204. do_send(&sendjob);
  205. }
  206.  
  207. void loop() {
  208. os_runloop_once();
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement