-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgps_datalogger.ino
70 lines (56 loc) · 2.23 KB
/
gps_datalogger.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/***************************************************************************
* Arduino Xiao GPS Datalogger
* -- using ATGM336H + SD Module
*
*
* by Josh Hrisko | Maker Portal LLC (c) 2021
*
*
***************************************************************************/
#include <SPI.h>
#include <SD.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
const int chipSelect = 6; // chip select for SD module
String filename = "gpsLog.csv"; // filename for saving to SD card
static const int RXPin = 2, TXPin = 1; // pins for ATGM336H GPS device
static const uint32_t GPSBaud = 9600; // default baudrate of ATGM336H GPS device
TinyGPSPlus gps;
SoftwareSerial ss(TXPin, RXPin);
void setup() {
Serial.begin(9600); // start serial monitor for debugging
ss.begin(GPSBaud);
if (!SD.begin(chipSelect)) { // verify SD card and module are working
Serial.println("SD Card not found");
while (1);
}
if (SD.exists(filename)) {
SD.remove(filename); // delete file if it already exists
}
data_saver("Date [mm/dd/yyyy], Time [HH:MM:SS.ZZ], Latitude [deg], Longitude [deg]"); // save data header
}
void loop() {
while (ss.available() > 0){
if (gps.encode(ss.read()) && gps.location.isValid() && gps.date.isValid() && gps.time.isValid()){
String data_to_save = ""; // data string for saving
data_to_save += String(gps.date.month())+"/"+String(gps.date.day())+"/"+
String(gps.date.year())+",";
data_to_save += String(gps.time.hour())+":"+String(gps.time.minute())+":"+
String(gps.time.second())+"."+String(gps.time.centisecond())+",";
data_to_save += String(gps.location.lat(),8)+","; // latitude
data_to_save += String(gps.location.lng(),8); // longitude
data_saver(data_to_save); // save new data points
// Serial.println(data_to_save); // uncomment to print GPS data
}
}
}
void data_saver(String WriteData){ // data saver function
File dataFile = SD.open(filename, FILE_WRITE); // open/create file
if (dataFile) {
dataFile.println(WriteData); // write data to file
dataFile.close(); // close file before continuing
} else {
delay(50); // prevents cluttering
Serial.println("SD Error"); // print error if SD card issue
}
}