Ein GPS Tracker für TTN Mapping
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
5.9KB

  1. #include <TinyGPS++.h>
  2. #include <SoftwareSerial.h>
  3. #include <lmic.h>
  4. #include <hal/hal.h>
  5. uint8_t coords[9];
  6. static const int RXPin = 0, TXPin = 1; // TXPin not used / dummy
  7. static const uint32_t GPSBaud = 9600;
  8. // The TinyGPS++ object
  9. TinyGPSPlus gps;
  10. // The serial connection to the GPS device
  11. SoftwareSerial ss(RXPin, TXPin);
  12. uint32_t timer = millis();
  13. // LoraWAN Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman
  14. // https://github.com/matthijskooijman/arduino-lmic
  15. // -------- LoRa PinMapping FeatherWing Octopus
  16. // THIS WORKS ONLY with the two additional diodes see wiring diagram
  17. const lmic_pinmap lmic_pins = {
  18. .nss = 2, // Connected to pin D
  19. .rxtx = LMIC_UNUSED_PIN, // For placeholder only, Do not connected on RFM92/RFM95
  20. .rst = LMIC_UNUSED_PIN, // Needed on RFM92/RFM95? (probably not) D0/GPIO16
  21. .dio = {
  22. 15, 15, LMIC_UNUSED_PIN }
  23. };
  24. // replace with YOUR DEVEUI here use LSB and copy hex to replace 0x00
  25. static const u1_t PROGMEM DEVEUI[8]={
  26. 0x00, 0xD0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  27. void os_getDevEui (u1_t* buf) {
  28. memcpy_P(buf, DEVEUI, 8);
  29. }
  30. // replace with YOUR APPEUI here use LSB and copy hex to replace 0x00
  31. static const u1_t PROGMEM APPEUI[8]={
  32. 0x00, 0x00, 0x00, 0xD0, 0x00, 0x00, 0x00, 0x00 };
  33. void os_getArtEui (u1_t* buf) {
  34. memcpy_P(buf, APPEUI, 8);
  35. }
  36. // replace with YOUR APPKEY here use MSB and copy hex to replace 0x00
  37. static const u1_t PROGMEM APPKEY[16]={
  38. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  39. void os_getDevKey (u1_t* buf) {
  40. memcpy_P(buf, APPKEY, 16);
  41. };
  42. volatile int LoRaWAN_Tx_Ready = 0; // Merker für ACK
  43. int LoRaWAN_Rx_Payload = 0 ;
  44. // -------- LoRa Event
  45. void onEvent (ev_t ev) {
  46. Serial.print(os_getTime());
  47. Serial.print(": ");
  48. switch(ev) {
  49. case EV_SCAN_TIMEOUT:
  50. Serial.println(F("EV_SCAN_TIMEOUT"));
  51. break;
  52. case EV_BEACON_FOUND:
  53. Serial.println(F("EV_BEACON_FOUND"));
  54. break;
  55. case EV_BEACON_MISSED:
  56. Serial.println(F("EV_BEACON_MISSED"));
  57. break;
  58. case EV_BEACON_TRACKED:
  59. Serial.println(F("EV_BEACON_TRACKED"));
  60. break;
  61. case EV_JOINING:
  62. Serial.println(F("EV_JOINING"));
  63. break;
  64. case EV_JOINED:
  65. Serial.println(F("EV_JOINED"));
  66. // Disable link check validation (automatically enabled
  67. // during join, but not supported by TTN at this time).
  68. LMIC_setLinkCheckMode(0);
  69. break;
  70. case EV_RFU1:
  71. Serial.println(F("EV_RFU1"));
  72. break;
  73. case EV_JOIN_FAILED:
  74. Serial.println(F("EV_JOIN_FAILED"));
  75. break;
  76. case EV_REJOIN_FAILED:
  77. Serial.println(F("EV_REJOIN_FAILED"));
  78. break;
  79. break;
  80. case EV_TXCOMPLETE:
  81. Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
  82. if (LMIC.txrxFlags & TXRX_ACK)
  83. Serial.println(F("Received ack"));
  84. if (LMIC.dataLen) {
  85. Serial.println(F("Received "));
  86. Serial.println(LMIC.dataLen);
  87. Serial.println(F(" bytes of payload"));
  88. LoRaWAN_Rx_Payload = 0;
  89. for (int i = 0;i<LMIC.dataLen;i++) {
  90. Serial.println(LMIC.frame[i+ LMIC.dataBeg],HEX);
  91. LoRaWAN_Rx_Payload = 256*LoRaWAN_Rx_Payload+LMIC.frame[i+ LMIC.dataBeg];
  92. }
  93. }
  94. LoRaWAN_Tx_Ready = 1;
  95. // Schedule next transmission
  96. //os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
  97. break;
  98. case EV_LOST_TSYNC:
  99. Serial.println(F("EV_LOST_TSYNC"));
  100. break;
  101. case EV_RESET:
  102. Serial.println(F("EV_RESET"));
  103. break;
  104. case EV_RXCOMPLETE:
  105. // data received in ping slot
  106. Serial.println(F("EV_RXCOMPLETE"));
  107. break;
  108. case EV_LINK_DEAD:
  109. Serial.println(F("EV_LINK_DEAD"));
  110. break;
  111. case EV_LINK_ALIVE:
  112. Serial.println(F("EV_LINK_ALIVE"));
  113. break;
  114. default:
  115. Serial.println(F("Unknown event"));
  116. break;
  117. }
  118. }
  119. void setup(){ // Einmalige Initialisierung
  120. Serial.begin(115200);
  121. ss.begin(GPSBaud);
  122. // -- Initialisiere LoraWAN
  123. os_init(); // LMIC LoraWAN
  124. LMIC_reset(); // Reset the MAC state
  125. LMIC.txpow = 27; // Maximum TX power
  126. LMIC.datarate=DR_SF12; // Long Range
  127. LMIC.rps = updr2rps(LMIC.datarate);
  128. }
  129. void loop() { // Kontinuierliche Wiederholung
  130. while (ss.available() > 0)
  131. if (gps.encode(ss.read()))
  132. {
  133. if (gps.location.isValid()) {
  134. Serial.print(gps.altitude.meters(), 6);
  135. Serial.print(F(","));
  136. Serial.print(gps.location.lat(), 6);
  137. Serial.print(F(","));
  138. Serial.print(gps.location.lng(), 6);
  139. Serial.print(F(","));
  140. Serial.println(gps.location.isValid(), 6);
  141. if (LMIC.opmode & (1 << 7)) {
  142. Serial.println(F("OP_TXRXPEND, not sending"));
  143. }
  144. else {
  145. // Prepare upstream data transmission at the next possible time.
  146. LoRaWAN_Tx_Ready = 0;
  147. int32_t lati = gps.location.lat() * 10000;
  148. int32_t lon = gps.location.lng() * 10000;
  149. int32_t altitudeGPS = gps.altitude.meters(); // int16_t
  150. int8_t hdopGPS = gps.hdop.value();
  151. Serial.println(lati);
  152. Serial.println(lon);
  153. Serial.println(altitudeGPS);
  154. Serial.println(hdopGPS);
  155. // Pad 2 int32_t to 6 8uint_t, big endian (24 bit each, having 11 meter precision)
  156. coords[0] = lati;
  157. coords[1] = lati >> 8;
  158. coords[2] = lati >> 16;
  159. coords[3] = lon;
  160. coords[4] = lon >> 8;
  161. coords[5] = lon >> 16;
  162. coords[6] = altitudeGPS;
  163. coords[7] = altitudeGPS >> 8;
  164. coords[8] = hdopGPS;
  165. LMIC_setTxData2(1, (uint8_t*) coords, sizeof(coords), 0);
  166. Serial.println(F("Packet queued"));
  167. while(LoRaWAN_Tx_Ready==0) {
  168. yield();
  169. os_runloop_once();
  170. }; // Warte bis gesendet
  171. //}
  172. // Blockende
  173. Serial.print(F("Waiting "));
  174. for (int i = 0;i<120;i++) {
  175. Serial.print(F("."));
  176. delay(1000); // for test only - this results in 10 sec transmit, change to much high value to be conform with the duty cycle !!!
  177. }
  178. Serial.println(" done.");
  179. }
  180. } else {
  181. Serial.println("no GPS fix");
  182. }
  183. }
  184. }