GPS data logger with SD card and Mega

Hello,

I am trying to create a GPS data logger using a EM-406 GPS, Arduino Ethernet shield with SD card, and a Mega 2560. I have used the SD, TinyGPS and SoftwareSerial libraries and I have tried to make the code as basic as possible at this stage. I have created the following code, which compiles ok. When I open the serial port the data being printed is as follows. I have the GPS hooked up to pin 51.

0.000000, 0.000000
0.000000, 0.000000
0.000000, 0.000000
0.000000, 0.000000
etc

Where I was expecting to get the values of latitude and longitude to 6 decimal places.

Does anyone have any clues as to what I have missed in my programming?

Many thanks and Regards.

#include <SD.h>
#include "TinyGPS.h"
#include <SoftwareSerial.h>
TinyGPS gps;
SoftwareSerial ss(51, 3);


float fkmph;
float flat, flon, fix_age;
void setup()
{
  ss.begin(4800);
  Serial.begin(9600);
  SD.begin(4);
  
  pinMode(53, OUTPUT); 
}

void loop()
{
 while (ss.available())
  {
    int c = ss.read();
    if (gps.encode(c))
    {
  // process new gps info here
      long lat, lon;
unsigned long fix_age, time, date, speed, course;
unsigned long chars;
unsigned short sentences, failed_checksum;

// retrieves +/- lat/long in 100000ths of a degree
gps.get_position(&lat, &lon, &fix_age);

// time in hhmmsscc, date in ddmmyy
gps.get_datetime(&date, &time, &fix_age);

// returns speed in 100ths of a knot
speed = gps.speed();

// course in 100ths of a degree
course = gps.course();

float flat, flon;

// returns +/- latitude/longitude in degrees
gps.f_get_position(&flat, &flon, &fix_age);
float falt = gps.f_altitude(); // +/- altitude in meters
float fc = gps.f_course(); // course in degrees
float fk = gps.f_speed_knots(); // speed in knots
float fmph = gps.f_speed_mph(); // speed in miles/hr
float fmps = gps.f_speed_mps(); // speed in m/sec
float fkmph = gps.f_speed_kmph(); // speed in km/hr

   }
  }
 
  File myFile = SD.open("GPSTEST.csv", FILE_WRITE);

  myFile.print(flat,6);
  myFile.print(",");
  myFile.println(flon,7);
  myFile.close();
 
 
  Serial.print(flat,6);
  Serial.print(",");
  Serial.println(flon,7);
  
  delay(500);
}

The ethernet shield uses pin 51. Why use SoftwareSerial at all, when you have a Mega?

dxw00d:
Why use SoftwareSerial at all, when you have a Mega?

To make it more clear, MEGA has 3 additional hardware serial ports, Serial1, Serial2, and Serial3.

Read the input and output section.

Ok so can I still use the TinyGPS library without the Softwareserial library if I use Serial or Serial1, Serial2 or Serial3?

Cheers

Steve_JB:
Ok so can I still use the TinyGPS library without the Softwareserial library if I use Serial or Serial1, Serial2 or Serial3?

Cheers

I have not used that library's most recent version but I think the place you want to look into might be the feedgps function that reads from serial port and feeds info to the library for processing. If you change that part from software serial to hardware serial, you will be able to do it.

Looking at the TinyGPS example called "Test with GPS device" . I tried to find all the parts that need work, hopefully I found it all.

Here is the part that determines serial input;

static bool feedgps()
{
  while (nss.available())
  {
    if (gps.encode(nss.read()))
      return true;
  }
  return false;
}

Change to this:

static bool feedgps()
{
  while (Serial1.available())
  {
    if (gps.encode(Serial1.read()))
      return true;
  }
  return false;
}

Remove this:

SoftwareSerial nss(3, 4);

Change this;

nss.begin(4800);

To this:

Serial1.begin(4800);

Thanks cyclegadget and liudur, I will change my code and see how it goes, will update with results...

Hello again,

Well I have successfully got my GPS data logger working using a hardware serial port (Serial 3) of my Mega 2560. The data is written to the SD card of the Ethernet shield and the csv file is set up for the GPS Visualizer software.

Thanks again for your suggestions and advice.

The code below is my latest version.

Cheers

#include "TinyGPS.h"
#include <SD.h>
TinyGPS gps;

void setup()
{

 Serial.begin(115200);
 Serial3.begin(4800);
 SD.begin(4);
 
 pinMode(53, OUTPUT); 
 
  File myFile = SD.open("trip.csv", FILE_WRITE);
  myFile.print("type,latitude,longitude,alt");
  myFile.close();
  Serial.print("type,latitude,longitude,alt");
}
void loop()
{
 bool newdata = false; // check if data is coming in
    if (feedgps())
      newdata = true;
  
  if (newdata)
  {   
  
  unsigned long chars;
  unsigned short sentences, failed;
  
  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
  
      // process new gps info here
      long lat, lon;
unsigned long fix_age, time, date, speed, course;
unsigned long chars;
unsigned short sentences, failed_checksum;

// retrieves +/- lat/long in 100000ths of a degree
gps.get_position(&lat, &lon, &fix_age);

// time in hhmmsscc, date in ddmmyy
gps.get_datetime(&date, &time, &fix_age);

// returns speed in 100ths of a knot
speed = gps.speed();

// course in 100ths of a degree
course = gps.course();

float flat, flon;

// returns +/- latitude/longitude in degrees
gps.f_get_position(&flat, &flon, &fix_age);
float falt = gps.f_altitude(); // +/- altitude in meters
float fc = gps.f_course(); // course in degrees
float fk = gps.f_speed_knots(); // speed in knots
float fmph = gps.f_speed_mph(); // speed in miles/hr
float fmps = gps.f_speed_mps(); // speed in m/sec
float fkmph = gps.f_speed_kmph(); // speed in km/hr

File myFile = SD.open("trip.csv", FILE_WRITE);

  myFile.print("T");
  myFile.print(",");
  myFile.print(flat,7);
  myFile.print(",");
  myFile.print(flon,7);
  myFile.print(",");
   myFile.println(falt);
  myFile.close();
  
Serial.print("T"); 
Serial.print(",");
Serial.print(flat,7);
Serial.print(",");
Serial.print(flon,7);
Serial.print(",");
Serial.println(falt);

delay(500);

    }
 }
 
}
static bool feedgps()
{
  while (Serial3.available())
  {
    if (gps.encode(Serial3.read()))
      return true;
  }
  return false;
}

Great! Not everyone gets back to us.

For future project planning, you may want to add a user interface such as lcd and buttons. I have a complete GPS interactive logger project code wirh my lcd shield and EEPROM. Just go to

Tou are welcome to take the code and use it if you want.

Thanks liudr. I do intend on adding an LCD screen so I will check it out.

Regards