GPS with SD Card Data Logger
Location tracking by button. Connects AdaFruit GPS Breakout Unit with AdaFruit SD Card without Wifi. Battery Operated.
Disclaimer: The GPS model is very tricky to deal with. The libraries necessary for it to run smoothly take up most of the space in the microcontroller making it much more difficult to pair with other pieces of technology (ex. more than one button).
This is useful in the boating in particular because the button function can be used to either track the locations of species or the boat itself.
CODE
Copy & paste into Arduino IDE
#include <SPI.h>
#include <SD.h>
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
File myFile;
//Define Pin
#define firstKeyPin 4
SoftwareSerial mySerial(8, 7); // RX, TX
Adafruit_GPS GPS(&mySerial);
void setup()
{
Serial.begin(9600);
while (!Serial);
pinMode(firstKeyPin, INPUT_PULLUP);
GPS.begin(9600);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
GPS.sendCommand(PGCMD_ANTENNA);
delay(1000);
Serial.println("GPS ready");
Serial.print("Init SD...");
if (!SD.begin(10)) {
Serial.println("init fail");
while (1);
}
Serial.println("init done.");
}
void loop() {
// This part may be redundant, maybe use later
int firstKeyState = digitalRead(firstKeyPin);
GPS.read();
if (GPS.newNMEAreceived()) {
if (!GPS.parse(GPS.lastNMEA()))
return;
}
myFile = SD.open("teut.txt", FILE_WRITE);
// Read to SD Card
if (myFile) {
// Read to serial monitor
if (digitalRead(firstKeyPin) == LOW & GPS.fix) {
delay(200);
Serial.print("Latitude: ");
Serial.println(GPS.latitudeDegrees, 6);
Serial.print("Longitude: ");
Serial.println(GPS.longitudeDegrees, 6);
Serial.println();
delay(2000);
}
myFile.close();
}
else {
// if the file didn't open, print an error:
Serial.println("error opening teut.txt");
// re-open the file for reading:
myFile = SD.open("teut.txt");
if (myFile) {
Serial.println("teut.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening teut.txt");
}
}
}
