Skip to content

Instantly share code, notes, and snippets.

@Mrkisha
Created May 11, 2018 16:55
Show Gist options
  • Save Mrkisha/1125ae18ede8c8d99e6fce1de8fbd708 to your computer and use it in GitHub Desktop.
Save Mrkisha/1125ae18ede8c8d99e6fce1de8fbd708 to your computer and use it in GitHub Desktop.
Arduino NEO M8N gps that writes data to SD card
// GPS
// TX - Pin 8 - green
// RX - Pin 9 - yellow
//
// SD card attached to SPI bus as follows:
// MOSI - Pin 11
// MISO - Pin 12
// CLK - Pin 13
// CS - Pin 10
//
#include <NMEAGPS.h>
#include <GPSport.h>
#include <SD.h> //Load SD card library
#include <SPI.h> //Load SPI Library
//
//#define RX_PIN 8
//#define TX_PIN 9
NMEAGPS gps;
gps_fix fix;
int pinCS = 10; // CS pin of the SD card
File file;
void setup()
{
DEBUG_PORT.begin(9600);
while (!Serial)
;
pinMode(pinCS, OUTPUT); // pin mode for SD Card reader
if (SD.begin()) {
DEBUG_PORT.println("SD card ready to use.");
} else {
DEBUG_PORT.println("SD card initialization failed.");
return;
}
gpsPort.begin(9600);
}
void loop()
{
while (gps.available( gpsPort )) {
DEBUG_PORT.print( F("GPS tracking has been started\n") );
float lat = 0.0;
float lng = 0.0;
String altt = "0";
String speed = "0";
fix = gps.read();
DEBUG_PORT.print( F("Location: ") );
DEBUG_PORT.print(gps.fix().dateTime);
if (fix.valid.location) {
lat = fix.latitude();
lng = fix.longitude();
DEBUG_PORT.print( lng, 6 );
DEBUG_PORT.print( ',' );
DEBUG_PORT.print( lat, 6 );
}
DEBUG_PORT.print( F(", Speed: ") );
if (fix.valid.speed) {
DEBUG_PORT.print(fix.speed_kph());
speed = (String)fix.speed_kph();
}
DEBUG_PORT.print( F(", Altitude: ") );
if (fix.valid.altitude) {
DEBUG_PORT.print( fix.altitude() );
altt = (String) fix.altitude();
}
DEBUG_PORT.println();
// Try and append
file = SD.open("gpsdata.csv", FILE_WRITE);
if (file) {
file.print(lng, 6);
file.print(",");
file.print(lat, 6);
file.print(",");
file.print(speed);
file.println();
file.close();
Serial.println("File closed");
}
delay(60000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment