I have a working Arduino Mega with LS00031 GPS that reports location and GPS time just fine, using tinyGPS.
I needed to convert UTC time from the GPS to local time, and the timeGPS example in the IDE seemed to offer exactly this, using functions and time_t from the time library. But it does not work for me so far, continually reporting the same time when I start it (7:37:12 8 Jan 2000 from memory) and incrementing the seconds from there.
Search reveals a few other people have had problems with timeGPS example.
Has anyone out there succeeded in getting timeGPS to work in this way?
Here's my code, main difference to timeGPS is I'm using Serial2 instead of softserial, plus I've added some reporting to try and debug the problem. It seems the GPS is not communicating over Serial2 because fix_age is reporting as a huge number. But Serial2 from GPS works fine in my other program
// based on TimeGPS.pde example
#define CJ_ID "CJ UTCa"
#include <Time.h>
#include <TinyGPS.h>//#include <NewSoftHOST.h> //NewSoftSerial | Arduiniana
// GPS and NewSoftSerial libraries are the work of Mikal Hart#define HOST Serial
#define HOST_BAUD_RATE 38400#define GPS Serial2
#define GPS_BAUD_RATE 57600TinyGPS gps;
//NewSoftSerial GPS = NewSoftSerial(3, 2); // receive on pin 3const int offset = 0; // offset hours from gps time (UTC)
//const float offset = 10.5; // offset hours from gps time (UTC)
time_t prevDisplay = 0; // when the digital clock was displayedvoid setup()
{
HOST.begin(HOST_BAUD_RATE);
HOST.println(CJ_ID);
GPS.begin(GPS_BAUD_RATE);
HOST.println("Waiting for GPS time ... ");
setSyncProvider(gpsTimeSync);
}void loop() {
processGpsBuffer();
//while (GPS.available()) {gps.encode(GPS.read());} // process gps messages
//if(timeStatus()!= timeNotSet)
if(timeStatus()== timeSet)
{if( now() != prevDisplay) //update the display only if the time has changed
{prevDisplay = now();
digitalClockDisplay();}
}
else
{HOST.print("timeStatus: "); HOST.println(timeStatus());}
delay(1000);
}void digitalClockDisplay(){
// digital clock display of the time
HOST.print(hour());
printDigits(minute());
printDigits(second());
HOST.print(" ");
HOST.print(day());
HOST.print(" ");
HOST.print(month());
HOST.print(" ");
HOST.print(year());
HOST.println();
}void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
HOST.print(":");
if(digits < 10)
HOST.print('0');
HOST.print(digits);
}time_t gpsTimeSync(){
// returns time if avail from gps, else returns 0
unsigned long fix_age = 0 ;
gps.get_datetime(NULL,NULL, &fix_age);
HOST.print(" Fix age: "); HOST.print(fix_age); HOST.println(" ms");
unsigned long time_since_last_fix;
return gpsTimeToArduinoTime(); // return time only if updated recently by gps
// if(fix_age < 1000)
// return gpsTimeToArduinoTime(); // return time only if updated recently by gps
// return 0;
}time_t gpsTimeToArduinoTime(){
// returns time_t from gps date and time with the given offset hours
tmElements_t tm;
int year;
gps.crack_datetime(&year, &tm.Month, &tm.Day, &tm.Hour, &tm.Minute, &tm.Second, NULL, NULL);
tm.Year = year - 1970;
time_t time = makeTime(tm);
return time + (offset * SECS_PER_HOUR);
}// processGpsBuffer. get characters from the GPS serial buffer and pass them to the gps object
bool processGpsBuffer(){
while (GPS.available())
{if (gps.encode(GPS.read())) return true;}
return false;}