#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
File dataFile;
SoftwareSerial GPSModule(8,9); // RX, TX
// process and flow related variables
int updates;
int failedUpdates;
int pos;
int stringplace = 0;
//
// variable to hold out nmea data and labels.
String nmea[12];
String labels[12] {"Time: ", "Lat: ", "N/S: ", "Lng: ", "E/W: ", "Fix: ", "Sats: ", "HDP: ", "Alt: ", "Metres: ", "MSL: ", "Metres: "};
//
bool addtitle = true;
bool detefile = true;
bool correctupdates=1;
String Stringstats ="";
int Intstats;
void setup() {
Serial.begin(9600);
GPSModule.begin(9600);
if (SD.begin(2)) {
Serial.println("ok");
// don't do anything more
}
else{
Serial.println("failed" );
}
digitalWrite(5,HIGH);
}
void loop() {
if (detefile)
{SD.remove("gps.txt");
detefile = false;}
dataFile = SD.open("gps.txt", FILE_WRITE);
//flushing out data at the start of each loop, not sure it is really neccessay
Serial.flush();
GPSModule.flush();
while (GPSModule.available() > 0)
{
GPSModule.read();
}
// This is the meat of it really, we look for our nmea string and then split it into comma seperated values
if (GPSModule.find("$GPGGA,")) {
String tempMsg = GPSModule.readStringUntil('\n');
//Serial.println(tempMsg);
for (int i = 0; i < tempMsg.length(); i++) {
if (tempMsg.substring(i, i + 1) == ",") {
nmea[pos] = tempMsg.substring(stringplace, i);
stringplace = i + 1;
pos++;
}
if (i == tempMsg.length() - 1) {
nmea[pos] = tempMsg.substring(stringplace, i);
}
}
updates++;
// Here we convert our minutes and seconds latitute and longitute to decimal degrees
nmea[1] = ConvertLat();
nmea[3] = ConvertLng();
// Test to see if GPS is fixed
if(nmea[5].toInt() > 0) {
// save data to SD if data is fixed
if (dataFile) {
if(correctupdates) {
updates=1;
correctupdates=0;
}
if (addtitle){
dataFile.print("Number,Time,Lat,Lng");
dataFile.println("");
addtitle = false;
}
Serial.println("writing");
// update number
dataFile.print(updates);
Serial.println("updates");
dataFile.print(",");
// time
dataFile.print(nmea[0]);
dataFile.print(",");
// latitude
dataFile.print(nmea[1]);
dataFile.print(",");
// longitude
dataFile.print(nmea[3]);
dataFile.println("");
Serial.println("done");
}
else{
Serial.println("error");
}
}
}
else {
failedUpdates++;
Serial.println(failedUpdates);
}
// Print out data to the Serial Monitor
for (int i = 0; i < 11; i++) {
Serial.print(labels[i]);
Serial.print(nmea[i]);
Serial.println("");
Stringstats=nmea[6];
int Intstats=Stringstats.toInt();
Serial.println(Intstats);
}
String maplink = "Google Maps" + nmea[1] + "," + nmea[3];
Serial.println(maplink);
// Zero our flow control varibles
stringplace = 0;
pos = 0;
// close file on SD card
dataFile.close();
}
// Here is the funky stuff - More details here https://www.youtube.com/watch?v=bgOZLgaLa0g
String ConvertLat() {
String posneg = "";
if (nmea[2] == "S") {
posneg = "-";
}
String latfirst;
float latsecond;
for (int i = 0; i < nmea[1].length(); i++) {
if (nmea[1].substring(i, i + 1) == ".") {
latfirst = nmea[1].substring(0, i - 2);
latsecond = nmea[1].substring(i - 2).toFloat();
}
}
latsecond = latsecond / 60;
String CalcLat = "";
char charVal[9];
dtostrf(latsecond, 4, 6, charVal);
for (int i = 0; i < sizeof(charVal); i++)
{
CalcLat += charVal[i];
}
latfirst += CalcLat.substring(1);
latfirst = posneg += latfirst;
return latfirst;
}
String ConvertLng() {
String posneg = "";
if (nmea[4] == "W") {
posneg = "-";
}
String lngfirst;
float lngsecond;
for (int i = 0; i < nmea[3].length(); i++) {
if (nmea[3].substring(i, i + 1) == ".") {
lngfirst = nmea[3].substring(0, i - 2);
//Serial.println(lngfirst);
lngsecond = nmea[3].substring(i - 2).toFloat();
//Serial.println(lngsecond);
}
}
lngsecond = lngsecond / 60;
String CalcLng = "";
char charVal[9];
dtostrf(lngsecond, 4, 6, charVal);
for (int i = 0; i < sizeof(charVal); i++)
{
CalcLng += charVal[i];
}
lngfirst += CalcLng.substring(1);
lngfirst = posneg += lngfirst;
return lngfirst;
}
/*
*/
Here is my code. I just want to run the program without clicking button on the computer screen of the Serial Monitor. I have try to run the program by comment // Serial.begin(9600); but it dose not work.
The program which is a GPS data logger with SD card.