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.

255 lines
8.6KB

  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. const lmic_pinmap lmic_pins = {
  17. .nss = 2, // Connected to pin D
  18. .rxtx = LMIC_UNUSED_PIN, // For placeholder only, Do not connected on RFM92/RFM95
  19. .rst = LMIC_UNUSED_PIN, // Needed on RFM92/RFM95? (probably not) D0/GPIO16
  20. .dio = {
  21. 15, 15, LMIC_UNUSED_PIN }
  22. };
  23. // mapper3 ABP
  24. void os_getArtEui (u1_t* buf) {
  25. }
  26. void os_getDevEui (u1_t* buf) {
  27. }
  28. void os_getDevKey (u1_t* buf) {
  29. }
  30. //mapper-bb-006 please insert the keys in MSB Format
  31. // DEVADDR example: 260A1B2C
  32. static const u4_t DEVADDR = 0x260XXXXX;
  33. static const u1_t PROGMEM APPSKEY[16]={ XXX };
  34. static const u1_t PROGMEM NWKSKEY[16]={ XXX };
  35. volatile int LoRaWAN_Tx_Ready = 0; // Merker für ACK
  36. int LoRaWAN_Rx_Payload = 0 ;
  37. // -------- LoRa Event
  38. void onEvent (ev_t ev) {
  39. Serial.print(os_getTime());
  40. Serial.print(": ");
  41. switch(ev) {
  42. case EV_SCAN_TIMEOUT:
  43. Serial.println(F("EV_SCAN_TIMEOUT"));
  44. break;
  45. case EV_BEACON_FOUND:
  46. Serial.println(F("EV_BEACON_FOUND"));
  47. break;
  48. case EV_BEACON_MISSED:
  49. Serial.println(F("EV_BEACON_MISSED"));
  50. break;
  51. case EV_BEACON_TRACKED:
  52. Serial.println(F("EV_BEACON_TRACKED"));
  53. break;
  54. case EV_JOINING:
  55. Serial.println(F("EV_JOINING"));
  56. break;
  57. case EV_JOINED:
  58. Serial.println(F("EV_JOINED"));
  59. // Disable link check validation (automatically enabled
  60. // during join, but not supported by TTN at this time).
  61. LMIC_setLinkCheckMode(0);
  62. break;
  63. case EV_RFU1:
  64. Serial.println(F("EV_RFU1"));
  65. break;
  66. case EV_JOIN_FAILED:
  67. Serial.println(F("EV_JOIN_FAILED"));
  68. break;
  69. case EV_REJOIN_FAILED:
  70. Serial.println(F("EV_REJOIN_FAILED"));
  71. break;
  72. break;
  73. case EV_TXCOMPLETE:
  74. Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
  75. if (LMIC.txrxFlags & TXRX_ACK)
  76. Serial.println(F("Received ack"));
  77. if (LMIC.dataLen) {
  78. Serial.println(F("Received "));
  79. Serial.println(LMIC.dataLen);
  80. Serial.println(F(" bytes of payload"));
  81. LoRaWAN_Rx_Payload = 0;
  82. for (int i = 0;i<LMIC.dataLen;i++) {
  83. Serial.println(LMIC.frame[i+ LMIC.dataBeg],HEX);
  84. LoRaWAN_Rx_Payload = 256*LoRaWAN_Rx_Payload+LMIC.frame[i+ LMIC.dataBeg];
  85. }
  86. }
  87. LoRaWAN_Tx_Ready = 1;
  88. // Schedule next transmission
  89. //os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
  90. break;
  91. case EV_LOST_TSYNC:
  92. Serial.println(F("EV_LOST_TSYNC"));
  93. break;
  94. case EV_RESET:
  95. Serial.println(F("EV_RESET"));
  96. break;
  97. case EV_RXCOMPLETE:
  98. // data received in ping slot
  99. Serial.println(F("EV_RXCOMPLETE"));
  100. break;
  101. case EV_LINK_DEAD:
  102. Serial.println(F("EV_LINK_DEAD"));
  103. break;
  104. case EV_LINK_ALIVE:
  105. Serial.println(F("EV_LINK_ALIVE"));
  106. break;
  107. default:
  108. Serial.println(F("Unknown event"));
  109. break;
  110. }
  111. }
  112. void setup(){ // Einmalige Initialisierung
  113. Serial.begin(115200);
  114. ss.begin(GPSBaud);
  115. // -- Initialisiere LoraWAN
  116. os_init(); // LMIC LoraWAN
  117. LMIC_reset(); // Reset the MAC state
  118. LMIC.txpow = 27; // Maximum TX power
  119. LMIC.datarate=DR_SF12; // Long Range
  120. LMIC.rps = updr2rps(LMIC.datarate);
  121. // Set static session parameters. Instead of dynamically establishing a session
  122. // by joining the network, precomputed session parameters are be provided.
  123. uint8_t appskey[sizeof(APPSKEY)];
  124. uint8_t nwkskey[sizeof(NWKSKEY)];
  125. memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
  126. memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
  127. LMIC_setSession (0x1, DEVADDR, nwkskey, appskey);
  128. // Set up the channels used by the Things Network, which corresponds
  129. // to the defaults of most gateways. Without this, only three base
  130. // channels from the LoRaWAN specification are used
  131. LMIC_setupChannel(0, 868100000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
  132. LMIC_setupChannel(1, 868300000, DR_RANGE_MAP(DR_SF12, DR_SF7B), BAND_CENTI); // g-band
  133. LMIC_setupChannel(2, 868500000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
  134. LMIC_setupChannel(3, 867100000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
  135. LMIC_setupChannel(4, 867300000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
  136. LMIC_setupChannel(5, 867500000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
  137. LMIC_setupChannel(6, 867700000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
  138. LMIC_setupChannel(7, 867900000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
  139. LMIC_setupChannel(8, 868800000, DR_RANGE_MAP(DR_FSK, DR_FSK), BAND_MILLI); // g2-band
  140. LMIC_setLinkCheckMode(0); // Disable link check validation
  141. LMIC.dn2Dr = DR_SF9; // TTN uses SF9 for its RX2 window.
  142. LMIC_setDrTxpow(DR_SF7,14); // Set data rate and transmit power for uplink
  143. }
  144. void loop() { // Kontinuierliche Wiederholung
  145. while (ss.available() > 0)
  146. if (gps.encode(ss.read()))
  147. {
  148. /*
  149. // if millis() or timer wraps around, we'll just reset it
  150. if (timer > millis()) timer = millis();
  151. // approximately every 10 seconds or so, print out the current stats
  152. if (millis() - timer > 30000) {
  153. timer = millis(); // reset the timer
  154. */
  155. if (gps.location.isValid()) {
  156. Serial.print(gps.altitude.meters(), 6);
  157. Serial.print(F(","));
  158. Serial.print(gps.location.lat(), 6);
  159. Serial.print(F(","));
  160. Serial.print(gps.location.lng(), 6);
  161. Serial.print(F(","));
  162. Serial.println(gps.location.isValid(), 6);
  163. //Block------------------------------ sende Daten an TTN
  164. //int port = 10;
  165. //static uint8_t mydata[2];
  166. //int wert=round(boschBME280.readTempC()*10);
  167. //wert=round(boschBME680.readGas()/1000.*10);
  168. //mydata[0] = wert >> 8;
  169. //mydata[1] = wert & 0xFF;
  170. // Check if there is not a current TX/RX job running
  171. //if (LMIC.opmode & OP_TXRXPEND) {
  172. if (LMIC.opmode & (1 << 7)) {
  173. Serial.println(F("OP_TXRXPEND, not sending"));
  174. }
  175. else {
  176. // Prepare upstream data transmission at the next possible time.
  177. LoRaWAN_Tx_Ready = 0; // Merker für ACK
  178. //LMIC_setTxData2(port, mydata, sizeof(mydata), 0); // Sende
  179. int32_t lati = gps.location.lat() * 10000;
  180. int32_t lon = gps.location.lng() * 10000;
  181. int32_t altitudeGPS = gps.altitude.meters(); // int16_t
  182. int8_t hdopGPS = gps.hdop.value();
  183. Serial.println(lati);
  184. Serial.println(lon);
  185. Serial.println(altitudeGPS);
  186. Serial.println(hdopGPS);
  187. // Pad 2 int32_t to 6 8uint_t, big endian (24 bit each, having 11 meter precision)
  188. coords[0] = lati;
  189. coords[1] = lati >> 8;
  190. coords[2] = lati >> 16;
  191. coords[3] = lon;
  192. coords[4] = lon >> 8;
  193. coords[5] = lon >> 16;
  194. coords[6] = altitudeGPS;
  195. coords[7] = altitudeGPS >> 8;
  196. coords[8] = hdopGPS;
  197. LMIC_setTxData2(1, (uint8_t*) coords, sizeof(coords), 0);
  198. Serial.println(F("Packet queued"));
  199. while(LoRaWAN_Tx_Ready==0) {
  200. yield();
  201. os_runloop_once();
  202. }; // Warte bis gesendet
  203. //}
  204. // Blockende
  205. Serial.print(F("Waiting "));
  206. for (int i = 0;i<10;i++) {
  207. Serial.print(F("."));
  208. delay(1000); // please set a proper duty cycle
  209. }
  210. Serial.println(" done.");
  211. }
  212. } else {
  213. Serial.println("no GPS fix");
  214. }
  215. }
  216. }