Skip to content

Instantly share code, notes, and snippets.

@csinge
Created March 23, 2012 10:00
Show Gist options
  • Save csinge/2169177 to your computer and use it in GitHub Desktop.
Save csinge/2169177 to your computer and use it in GitHub Desktop.
GPS Xbee on a Seeeduino Stalker v2
/*
Ported from SD card datalogger example
Write XBEE GPS output to the SD card of a Seeduino Stalker
*/
#include <SD.h>
#include <Streaming.h>
#include <TinyGPS.h>
TinyGPS gps;
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;
//Set to zero to turn off the debug writes
#define DEBUG 1
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
//Led - On when
pinMode(8, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop()
{
// make a string for assembling the data to log:
String dataString;
while(Serial.available())
{
int input;
input = (char)Serial.read();
if(gps.encode(input))
{
// process new gps info here
long lat, lon;
unsigned long fix_age;
// retrieves +/- lat/long in 100000ths of a degree
gps.get_position(&lat, &lon, &fix_age);
dataString += "$GPS #lat:";
dataString += lat;
dataString += " #lon:";
dataString += lon;
dataString += " #fix_age:";
dataString += fix_age;
dataString += "\n";
if (DEBUG)
{
if (fix_age == TinyGPS::GPS_INVALID_AGE)
dataString += "$Debug #fix_age_state:No fix\n";
else if (fix_age > 5000)
dataString += "$Debug #fix_age_state:Stale\n";
else
dataString += "$Debug #fix_age_state:Fixed\n";
}
}
}
if (DEBUG)
{
unsigned long chars;
unsigned short sentences, failed_checksum;
gps.stats(&chars, &sentences, &failed_checksum);
dataString += "$DEBUG #Char: ";
dataString += chars;
dataString += " #Sentences: ";
dataString += sentences;
dataString += " #Failed:";
dataString += failed_checksum;
dataString += "\n";
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment