Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 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. #include <DHT.h>;
  36. #include <CayenneLPP.h>
  37.  
  38. //Variables
  39. int chk;
  40. float hum; //Stores humidity value
  41. float temp; //Stores temperature value
  42.  
  43. DHT dht(9, DHT22);
  44.  
  45. CayenneLPP lpp(51);
  46.  
  47. // This EUI must be in little-endian format, so least-significant-byte
  48. // first. When copying an EUI from ttnctl output, this means to reverse
  49. // the bytes. For TTN issued EUIs the last bytes should be 0xD5, 0xB3,
  50. // 0x70.
  51. static const u1_t PROGMEM APPEUI[8]={ 0x6A, 0xA2, 0x00, 0xD0, 0x7E, 0xD5, 0xB3, 0x70 };
  52. void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8);}
  53.  
  54. // This should also be in little endian format, see above.
  55. static const u1_t PROGMEM DEVEUI[8]={ 0xB9, 0x93, 0x64, 0xA5, 0x67, 0x69, 0x5D, 0x00 };
  56. void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8);}
  57.  
  58. // This key should be in big endian format (or, since it is not really a
  59. // number but a block of memory, endianness does not really apply). In
  60. // practice, a key taken from ttnctl can be copied as-is.
  61. // The key shown here is the semtech default key.
  62. static const u1_t PROGMEM APPKEY[16] = { 0xFC, 0x64, 0x56, 0x92, 0x1A, 0xEB, 0x37, 0x56, 0x37, 0x68, 0x0F, 0x7C, 0x3E, 0x98, 0x92, 0xAB };
  63. void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16);}
  64.  
  65. //static uint8_t mydata[];// = "Hello, world!";
  66. static osjob_t sendjob;
  67.  
  68. // Schedule TX every this many seconds (might become longer due to duty
  69. // cycle limitations).
  70. const unsigned TX_INTERVAL = 60;
  71.  
  72. // Pin mapping
  73. const lmic_pinmap lmic_pins = {
  74. .nss = 10,
  75. .rxtx = LMIC_UNUSED_PIN,
  76. .rst = 5,
  77. .dio = {2, 7, 8},
  78. };
  79.  
  80. void onEvent (ev_t ev) {
  81. Serial.print(os_getTime());
  82. Serial.print(": ");
  83. switch(ev) {
  84. case EV_SCAN_TIMEOUT:
  85. Serial.println(F("EV_SCAN_TIMEOUT"));
  86. break;
  87. case EV_BEACON_FOUND:
  88. Serial.println(F("EV_BEACON_FOUND"));
  89. break;
  90. case EV_BEACON_MISSED:
  91. Serial.println(F("EV_BEACON_MISSED"));
  92. break;
  93. case EV_BEACON_TRACKED:
  94. Serial.println(F("EV_BEACON_TRACKED"));
  95. break;
  96. case EV_JOINING:
  97. Serial.println(F("EV_JOINING"));
  98. break;
  99. case EV_JOINED:
  100. Serial.println(F("EV_JOINED"));
  101.  
  102. // Disable link check validation (automatically enabled
  103. // during join, but not supported by TTN at this time).
  104. LMIC_setLinkCheckMode(0);
  105. break;
  106. case EV_RFU1:
  107. Serial.println(F("EV_RFU1"));
  108. break;
  109. case EV_JOIN_FAILED:
  110. Serial.println(F("EV_JOIN_FAILED"));
  111. break;
  112. case EV_REJOIN_FAILED:
  113. Serial.println(F("EV_REJOIN_FAILED"));
  114. break;
  115. break;
  116. case EV_TXCOMPLETE:
  117. Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
  118. if (LMIC.txrxFlags & TXRX_ACK)
  119. Serial.println(F("Received ack"));
  120. if (LMIC.dataLen) {
  121. Serial.println(F("Received "));
  122. Serial.println(LMIC.dataLen);
  123. Serial.println(F(" bytes of payload"));
  124. }
  125. // Schedule next transmission
  126. os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
  127. break;
  128. case EV_LOST_TSYNC:
  129. Serial.println(F("EV_LOST_TSYNC"));
  130. break;
  131. case EV_RESET:
  132. Serial.println(F("EV_RESET"));
  133. break;
  134. case EV_RXCOMPLETE:
  135. // data received in ping slot
  136. Serial.println(F("EV_RXCOMPLETE"));
  137. break;
  138. case EV_LINK_DEAD:
  139. Serial.println(F("EV_LINK_DEAD"));
  140. break;
  141. case EV_LINK_ALIVE:
  142. Serial.println(F("EV_LINK_ALIVE"));
  143. break;
  144. default:
  145. Serial.println(F("Unknown event"));
  146. break;
  147. }
  148. }
  149.  
  150. void do_send(osjob_t* j){
  151. // Check if there is not a current TX/RX job running
  152. if (LMIC.opmode & OP_TXRXPEND) {
  153. Serial.println(F("OP_TXRXPEND, not sending"));
  154. } else {
  155. hum = dht.readHumidity();
  156. temp= dht.readTemperature();
  157.  
  158. lpp.reset();
  159. lpp.addTemperature(1, temp);
  160. lpp.addRelativeHumidity(3, hum);
  161.  
  162. // Prepare upstream data transmission at the next possible time.
  163. LMIC_setTxData2(1, lpp.getBuffer(), lpp.getSize(), 0);
  164. Serial.println(F("Packet queued"));
  165. }
  166. // Next TX is scheduled after TX_COMPLETE event.
  167. }
  168.  
  169. void setup() {
  170. Serial.begin(115200);
  171. Serial.println(F("Starting"));
  172.  
  173. dht.begin();
  174.  
  175. #ifdef VCC_ENABLE
  176. // For Pinoccio Scout boards
  177. pinMode(VCC_ENABLE, OUTPUT);
  178. digitalWrite(VCC_ENABLE, HIGH);
  179. delay(1000);
  180. #endif
  181.  
  182. // LMIC init
  183. os_init();
  184. // Reset the MAC state. Session and pending data transfers will be discarded.
  185. LMIC_reset();
  186.  
  187. // Start job (sending automatically starts OTAA too)
  188. do_send(&sendjob);
  189. }
  190.  
  191. void loop() {
  192. os_runloop_once();
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement