Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. /*
  2. GPS Logger
  3.  
  4. Simple project which logs data from GPS module (NEO 6M) into SD card.
  5. Locations are stored as file (yyyyMMdd.txt) and the file will contain one row per location (dd.MM.yyyy HH:mm:ss lat,lon).
  6. Location is stored for each interval given as configuration variable 'frequency'.
  7.  
  8. Led modes:
  9. continuous -> error
  10. blinking -> looking for location
  11. off -> everything ok
  12.  
  13. Connecting modules:
  14. D1 -> GPS-module-RX
  15. D3 -> GPS-module-TX
  16. MOSI - D7
  17. MISO - D6
  18. CLK - D5
  19. CS - D8
  20.  
  21.  
  22. Dependency(TinyGPS++ library): http://arduiniana.org/libraries/tinygpsplus/
  23.  
  24. created Apr 2017
  25. by CheapskateProjects
  26.  
  27. ---------------------------
  28. The MIT License (MIT)
  29.  
  30. Copyright (c) 2017 CheapskateProjects
  31.  
  32. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  33.  
  34. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  35.  
  36. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. */
  38.  
  39. #include <TinyGPS++.h>
  40. #include <SoftwareSerial.h>
  41. #include <SPI.h>
  42. #include <SD.h>
  43.  
  44. // Pins used for communicating with GPS module
  45. //static const int RXPin = 0, TXPin = 5;
  46. // Status led
  47. static const int GpsLedPin = 9;
  48. // Baud rate of your GPS module (usually 4800 or 9600)
  49. static const uint32_t GPSBaud = 9600;
  50. // How frequently should we save current location (milliseconds)
  51. static const unsigned long frequency = 500;
  52.  
  53. // gps object
  54. TinyGPSPlus gps;
  55. // true when we have found location since last restart and file has been opened
  56. boolean opened = false;
  57. // current data file
  58. File dataFile;
  59. // file name
  60. String fileName;
  61. // timestamp of previous location capture. Used to check if we should save this location or skip it
  62. unsigned long previous = 0;
  63. // The serial connection to the GPS device
  64. SoftwareSerial ss(0,5);
  65. /*const int timezone = +2;*/
  66. double prev_lat = 0;
  67. double prev_lon = 0;
  68. boolean first = true;
  69.  
  70. void setup()
  71. {
  72. ss.begin(GPSBaud);
  73. pinMode(GpsLedPin, OUTPUT);
  74. digitalWrite(GpsLedPin, LOW);
  75.  
  76. if (!SD.begin(10))
  77. {
  78. digitalWrite(GpsLedPin, HIGH);
  79. while (true);
  80. }
  81. }
  82.  
  83. void loop()
  84. {
  85. // If we have data, decode and log the data
  86. while (ss.available() > 0)
  87. {
  88. if (first)
  89. {
  90. prev_lat = gps.location.lat();
  91. prev_lon = gps.location.lng();
  92. first = false;
  93. // continue;
  94. }
  95. if (gps.encode(ss.read()))
  96. logInfo();
  97.  
  98. }
  99. // Test that we have had something from GPS module within first 10 seconds
  100. if (millis() > 10000 && gps.charsProcessed() < 10)
  101. {
  102. // Set error led
  103. digitalWrite(GpsLedPin, HIGH);
  104.  
  105. // Wiring error so stop trying
  106. while (true);
  107. }
  108. }
  109.  
  110. // Help function to pad 0 prefix when valus < 10
  111. void printIntValue(int value)
  112. {
  113. if (value < 10)
  114. {
  115. dataFile.print(F("0"));
  116. }
  117. dataFile.print(value);
  118. }
  119.  
  120. // Log current info if we have valid location
  121. void logInfo()
  122. {
  123. // Wait until we have location locked!
  124. if (!gps.location.isValid())
  125. {
  126. digitalWrite(GpsLedPin, HIGH);
  127. delay(20);
  128. digitalWrite(GpsLedPin, LOW);
  129. return;
  130. }
  131.  
  132. if (!opened)
  133. {
  134. // When we first get something to log we take file name from that time
  135. fileName = "";
  136. fileName += gps.date.year();
  137. if (gps.date.month() < 10) fileName += "0";
  138. fileName += gps.date.month();
  139. if (gps.date.day() < 10) fileName += "0";
  140. fileName += gps.date.day();
  141. fileName += ".txt";
  142. opened = true;
  143. }
  144.  
  145. // Show that everything is ok
  146. digitalWrite(GpsLedPin, LOW);
  147.  
  148. if (millis() - previous > frequency)
  149. {
  150. if (abs(prev_lat - gps.location.lat()) < 1 && abs(prev_lon - gps.location.lng()) < 1)
  151. {
  152.  
  153. previous = millis();
  154. // Write data row (yy.MM.dd HH:mm:ss lat,lon)
  155. dataFile = SD.open(fileName, FILE_WRITE);
  156. printIntValue(gps.date.year());
  157. dataFile.print(F("."));
  158. printIntValue(gps.date.month());
  159. dataFile.print(F("."));
  160. dataFile.print(gps.date.day());
  161. dataFile.print(F(" "));
  162. printIntValue(gps.time.hour());
  163. dataFile.print(F(":"));
  164. printIntValue(gps.time.minute());
  165. dataFile.print(F(":"));
  166. printIntValue(gps.time.second());
  167. dataFile.print(F(" "));
  168. dataFile.print(gps.location.lat(), 6);
  169. dataFile.print(F(" "));
  170. dataFile.print(gps.location.lng(), 6);
  171. dataFile.print(F(" "));
  172. dataFile.print(gps.speed.mps());
  173. dataFile.print(F(" "));
  174. /*dataFile.print(gps.speed.kmph());
  175. dataFile.print(F(" "));*/
  176. dataFile.print(gps.satellites.value());
  177. dataFile.print(F(" "));
  178. dataFile.print(gps.altitude.meters());
  179. dataFile.print(F(" "));
  180. dataFile.print(gps.hdop.value());
  181. dataFile.println();
  182. dataFile.close();
  183. Serial.print(gps.date.year());
  184. Serial.print(F("."));
  185. Serial.print(gps.date.month());
  186. Serial.print(F("."));
  187. Serial.print(gps.date.day());
  188. Serial.print(F(" "));
  189. Serial.print(gps.time.hour());
  190. Serial.print(F(":"));
  191. Serial.print(gps.time.minute());
  192. Serial.print(F(":"));
  193. Serial.print(gps.time.second());
  194. Serial.print(F(" "));
  195. Serial.print(gps.location.lat(), 6);
  196. Serial.print(F(","));
  197. Serial.print(gps.location.lng(), 6);
  198. Serial.print(" ");
  199. Serial.print(gps.altitude.meters());
  200. Serial.print(" ");
  201. Serial.print(gps.satellites.value());
  202. Serial.print(" ");
  203. Serial.print(gps.hdop.value());
  204. Serial.print(" ");
  205. Serial.print(gps.speed.mps());
  206. Serial.println("");
  207. }
  208. prev_lat = gps.location.lat();
  209. prev_lon = gps.location.lng();
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement