From: https://github.com/thijse/Arduino-Code-and-Libraries/blob/master/Libraries/Time/Examples/TimeGPS/TimeGPS.pde
/*H*******************************************************
* TimeGPS.pde
* example code illustrating time synced from a GPS
********************************************************/
#include <Time.h>
#include <TinyGPS.h> //http://arduiniana.org/libraries/TinyGPS/
#include <NewSoftSerial.h> //http://arduiniana.org/libraries/newsoftserial/
//************************* DEFINES ************************************
//************************* PROTOTYPES ************************************
void digitalClockDisplay();
time_t gpsTimeSync();
time_t gpsTimeToArduinoTime();
//************************* VARIABLES ************************************
// GPS AND NewSoftSerial LIBRARIES ARE WORK OF mIKAL HART
TinyGPS gps;
NewSoftSerial serial_gps = NewSoftSerial( 3, 2 ); // RECEIVE ON PIN 3
const int offset = 1; // OFFSET HOURS FROM GPS TIME ( UTC )
time_t prevDisplay = 0; // WHEN DIGITAL CLOCK WAS DISPLAYED
/*F********************************************************************
*
**********************************************************************/
void
setup( )
{
Serial.begin( 9600 );
serial_gps.begin( 4800 );
Serial.println( "Waiting for GPS time ... " );
setSyncProvider( gpsTimeSync );
}
/*F********************************************************************
*
**********************************************************************/
void
loop( )
{
while ( serial_gps.available( ) )
{
gps.encode( serial_gps.read( ) ); // PROCESS GPS MESSAGES
}
if( timeStatus( )!= timeNotSet )
{
if( now( ) != prevDisplay ) // UPDATE DISPLAY ONLY IF TIME HAS CHANGED
{
prevDisplay = now( );
digitalClockDisplay( );
}
}
}
/*F********************************************************************
* DIGITAL CLOCK DISPLAY OF TIME
**********************************************************************/
void
digitalClockDisplay()
{
Serial.print( hour( ) );
printDigits( minute( ) );
printDigits( second( ) );
Serial.print( " " );
Serial.print( day( ) );
Serial.print( " " );
Serial.print( month( ) );
Serial.print( " " );
Serial.print( year( ) );
Serial.println( );
}
/*F********************************************************************
* DIGITAL CLOCK DISPLAY: PRINTS PRECEDING COLON AND LEADING 0
**********************************************************************/
void
printDigits( int digits )
{
Serial.print( ":" );
if( digits < 10 )
Serial.print( '0' );
Serial.print( digits );
}
/*F********************************************************************
* RETURNS TIME IF AVAIL FROM GPS, ELSE RETURNS 0
**********************************************************************/
time_t
gpsTimeSync( )
{
unsigned long fix_age = 0 ;
gps.get_datetime( NULL,NULL, &fix_age );
unsigned long time_since_last_fix;
if( fix_age < 1000 )
return( gpsTimeToArduinoTime()); // RTRN TIM IF UPDATED RECENTLY BY GPS
return( 0 );
}
/*F********************************************************************
* RETURNS TIME_T FROM GPS DATE AND TIME WITH GIVEN OFFSET HOURS
**********************************************************************/
time_t
gpsTimeToArduinoTime()
{
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) );
}