GPS Time! (Code Issue... new problem) [FIXED]

I decided to make a clock that syncs to GPS time..

But, the GPS time i'm getting is accurate, it just appears not to be in my Time Zone, are all GPS time signals based on GMT or something else?...

EG, do i simply need to deduct/add from the time? or should the GPS give me my time zone?

Location: -34.85XXX6,138.61XXX9 Date/Time: 9/8/2014 05:51:07.00

it should be

Location: -34.85XXX6,138.61XXX9 Date/Time: 9/8/2014 15:23:07.00

(UTC + 9:30 for Adelaide, South Australia)

I don't want to add X amount of hours to find out tomorrow it changes, i have no idea about GPS time.

edit..

/*
 * TimeGPS.pde
 * example code illustrating time synced from a GPS
 * 
 */

#include <Time.h>
#include <TinyGPS.h>       // http://arduiniana.org/libraries/TinyGPS/
#include <SoftwareSerial.h>
// TinyGPS and SoftwareSerial libraries are the work of Mikal Hart

SoftwareSerial SerialGPS = SoftwareSerial(A0, A1);  // receive on pin 10
TinyGPS gps; 

// To use a hardware serial port, which is far more efficient than
// SoftwareSerial, uncomment this line and remove SoftwareSerial
//#define SerialGPS Serial1

// Offset hours from gps time (UTC)
const int offset = 1;   // Central European Time
//const int offset = -5;  // Eastern Standard Time (USA)
//const int offset = -4;  // Eastern Daylight Time (USA)
//const int offset = -8;  // Pacific Standard Time (USA)
//const int offset = -7;  // Pacific Daylight Time (USA)

// Ideally, it should be possible to learn the time zone
// based on the GPS position data.  However, that would
// require a complex library, probably incorporating some
// sort of database using Eric Muller's time zone shape
// maps, at http://efele.net/maps/tz/

time_t prevDisplay = 0; // when the digital clock was displayed
const int UTC_offset = 9;

void setup()
{
  Serial.begin(9600);
  while (!Serial) ; // Needed for Leonardo only
  SerialGPS.begin(9600);
  Serial.println("Waiting for GPS time ... ");
}

void loop()
{
  while (SerialGPS.available()) {
    if (gps.encode(SerialGPS.read())) { // process gps messages
      // when TinyGPS reports new data...
      unsigned long age;
      int Year;
      byte Month, Day, Hour, Minute, Second;
      gps.crack_datetime(&Year, &Month, &Day, &Hour, &Minute, &Second, NULL, &age);
      if (age < 500) {
        // set the Time to the latest GPS reading
        setTime(Hour, Minute, Second, Day, Month, Year);
        adjustTime((UTC_offset * SECS_PER_HOUR) + SECS_PER_HOUR/2);         
      }
    }
  }
  if (timeStatus()!= timeNotSet) {
    if (now() != prevDisplay) { //update the display only if the time has changed
      prevDisplay = now();
      digitalClockDisplay();  
    }
  }
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits) {
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

used different code from the same person who wrote the other time lib.

Thanks! but this does the job :slight_smile:

They use Universal Coordinated Time (UTC) or, as you put it, GMT.

Oh...

So i have to add hours, minutes which means having to adjust everything else that rolls over!

Thanks....

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Time.h>
/*
   This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
static const int RXPin = A0, TXPin = A1;
static const uint32_t GPSBaud = 9600;
const int UTC_offset = 9;

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

void setup()
{
  Serial.begin(9600);
  ss.begin(GPSBaud);

}



void loop()
{
  // This sketch displays information every time a new sentence is correctly encoded.
  while (ss.available() > 0)
    if (gps.encode(ss.read()))
      displayInfo();

  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    while(true);
  }
}

void displayInfo()
{
 
 /* Serial.print(F("Location: ")); 
  if (gps.location.isValid())
  {
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(","));
    Serial.print(gps.location.lng(), 6);
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.print(F("  Date/Time: "));
  if (gps.date.isValid())
  {
    Serial.print(gps.date.month());
    Serial.print(F("/"));
    Serial.print(gps.date.day());
    Serial.print(F("/"));
    Serial.print(gps.date.year());
  }
  else
  {
    Serial.print(F("INVALID"));
  }
   Serial.print(F(" "));
  */
  
 
  if (gps.time.isValid())
  {
      int Year = gps.date.year();
      byte Month = gps.date.month();
      byte Day = gps.date.day();
      byte Hour = gps.time.hour();
      byte Minute = gps.time.minute();
      byte Second = gps.time.second();

        // Set Time from GPS data string
        setTime(Hour, Minute, Second, Day, Month, Year);
        // Calc current Time Zone time by offset value
        
        
        adjustTime((UTC_offset * SECS_PER_HOUR) + SECS_PER_HOUR/2);           
      
       Year = year();
       Month = month();
       Day = day();
       Hour = hour();
       Minute = minute();
       Second = second();

    

    
    
    if (Hour < 10) Serial.print(F("0"));
    Serial.print(Hour);
    Serial.print(F(":"));
    if (Minute < 10) Serial.print(F("0"));
    Serial.print(Minute);
    Serial.print(F(":"));
    if (Second < 10) Serial.print(F("0"));
    Serial.print(Second);
    Serial.print(F("."));
//    if (gps.time.centisecond() < 10) Serial.print(F("0"));
 ///   Serial.print(gps.time.centisecond());
  }
     else
  {
    Serial.print(F("INVALID"));
  }
   
  delay(5000); 
   
  Serial.println();
  
  

}

This is a weird one... it alternates between UTC and the correct time?!

17:07:58.
07:37:58.
17:08:09.
07:38:09.
17:08:20.
07:38:20.
17:08:31.
07:38:31.
17:08:42.
07:38:42.
17:08:53.
07:38:53.
17:09:04.
07:39:04.
07:39:04.
17:09:20.
07:39:20.
17:09:31.
07:39:31.
17:09:42.
07:39:42.
17:09:53.
07:39:53.
17:10:04.

How can it display the correct time/date and then all of a sudden with the next iteration of the loop display GMT time...

So it will display the correct time eg

17:09:31
pauses for 5 seconds

Then the next read will display 7am

It's got to be my code, do i need to move part of this thread into the programming section?

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Time.h>
/*
   This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
static const int RXPin = A0, TXPin = A1;
static const uint32_t GPSBaud = 9600;
const int UTC_offset = 9;
    int Year;
    byte Month;
    byte Day;
    byte Hour;
    byte Minute;
    byte Second;


// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

void setup()
{
  Serial.begin(9600);
  ss.begin(GPSBaud);

}



void loop()
{
  // This sketch displays information every time a new sentence is correctly encoded.
  while (ss.available() > 0)
    if (gps.encode(ss.read()))
      displayInfo();

  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    while(true);
  }
}

void displayInfo()
{
 
  Serial.print(F("Location: ")); 
  if (gps.location.isValid())
  {
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(","));
    Serial.print(gps.location.lng(), 6);
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.print(F("  Date/Time: "));
  if (gps.date.isValid())
  {
    Serial.print(gps.date.month());
    Serial.print(F("/"));
    Serial.print(gps.date.day());
    Serial.print(F("/"));
    Serial.print(gps.date.year());
  }
  else
  {
    Serial.print(F("INVALID"));
  }
   Serial.print(F(" "));
 
  
 
  if (gps.time.isValid())
  {
      Year = gps.date.year();
       Month = gps.date.month();
      Day = gps.date.day();
       Hour = gps.time.hour();
       Minute = gps.time.minute();
      Second = gps.time.second();

        // Set Time from GPS data string
        setTime(Hour, Minute, Second, Day, Month, Year);
        // Calc current Time Zone time by offset value
        
        
        adjustTime((UTC_offset * SECS_PER_HOUR) + SECS_PER_HOUR/2);           
      
       Year = year();
       Month = month();
       Day = day();
       Hour = hour();
       Minute = minute();
       Second = second();

    

    
    
    if (Hour < 10) Serial.print(F("0"));
    Serial.print(Hour);
    Serial.print(F(":"));
    if (Minute < 10) Serial.print(F("0"));
    Serial.print(Minute);
    Serial.print(F(":"));
    if (Second < 10) Serial.print(F("0"));
    Serial.print(Second);
    Serial.print(F("."));

  }
     else
  {
    Serial.print(F("INVALID GPS DATA"));
  }
  Serial.println("Pausing for 5 seconds");   
  Serial.println();
  delay(5000); 

   

  
  

}

Location: -34.xxxxx,138.xxxxDate/Time: 9/8/2014 07:51:58.Pausing for 5 seconds

Location: -34.xxxxx,138.xxxxDate/Time: 9/8/2014 17:22:09.Pausing for 5 seconds

Location: -34.xxxxxx,138.xxxxxDate/Time: 9/8/2014 07:52:09.Pausing for 5 seconds

http://forum.arduino.cc/index.php?topic=265596.msg1872997#msg1872997

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Time.h>
/*
   This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
static const int RXPin = A0, TXPin = A1;
static const uint32_t GPSBaud = 9600;
const int UTC_offset = 9;
    int Year;
    byte Month;
    byte Day;
    byte Hour;
    byte Minute;
    byte Second;


// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

void setup()
{
  Serial.begin(9600);
  ss.begin(GPSBaud);

}



void loop()
{
  // This sketch displays information every time a new sentence is correctly encoded.
  while (ss.available() > 0)
    if (gps.encode(ss.read()))
      displayInfo();

  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    while(true);
  }
}

void displayInfo()
{
 
  Serial.print(F("Location: ")); 
  if (gps.location.isValid())
  {
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(","));
    Serial.print(gps.location.lng(), 6);
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.print(F("  Date/Time: "));
  if (gps.date.isValid())
  {
    Serial.print(gps.date.month());
    Serial.print(F("/"));
    Serial.print(gps.date.day());
    Serial.print(F("/"));
    Serial.print(gps.date.year());
  }
  else
  {
    Serial.print(F("INVALID"));
  }
   Serial.print(F(" "));
 
  
 
  if (gps.time.isValid())
  {
      Year = gps.date.year();
       Month = gps.date.month();
      Day = gps.date.day();
       Hour = gps.time.hour();
       Minute = gps.time.minute();
      Second = gps.time.second();

        // Set Time from GPS data string
        setTime(Hour, Minute, Second, Day, Month, Year);
        // Calc current Time Zone time by offset value
        
        
        adjustTime((UTC_offset * SECS_PER_HOUR) + SECS_PER_HOUR/2);           
      
       Year = year();
       Month = month();
       Day = day();
       Hour = hour();
       Minute = minute();
       Second = second();

    

    
    
    if (Hour < 10) Serial.print(F("0"));
    Serial.print(Hour);
    Serial.print(F(":"));
    if (Minute < 10) Serial.print(F("0"));
    Serial.print(Minute);
    Serial.print(F(":"));
    if (Second < 10) Serial.print(F("0"));
    Serial.print(Second);
    Serial.print(F("."));

  }
     else
  {
    Serial.print(F("INVALID GPS DATA"));
  }
  Serial.println("Pausing for 5 seconds");   
  Serial.println();
  delay(5000); 

   

  
  

}

Location: -34.850788,138.618484 Date/Time: 9/8/2014 07:54:54.Pausing for 5 seconds

Location: -34.xx,138.xxDate/Time: 9/8/2014 17:25:05.Pausing for 5 seconds

Location: -34.xx,138.xxDate/Time: 9/8/2014 07:55:05.Pausing for 5 seconds

Location: -34.xx,138.xxDate/Time: 9/8/2014 17:25:16.Pausing for 5 seconds

Location: -34.xx,138.xxDate/Time: 9/8/2014 07:55:16.Pausing for 5 seconds

Location: -34.xx,138.xxDate/Time: 9/8/2014 17:25:27.Pausing for 5 seconds

Location: -34.xx,138.xxDate/Time: 9/8/2014 07:55:27.Pausing for 5 seconds

How can it possibly get the right time and then on the next loop get it right and then wrong and then right?

(Mods feel free to delete everything from the 2nd post down on the other thread, as it's now a coding issue not a protocol issue thanks)

Sources:

GitHub - PaulStoffregen/Time: Time library for Arduino (Latest Version)
TinyGPS++ | Arduiniana (GPS code)

What do the raw numbers from the gps object look like (before the call to setTime)?

wildbill:
What do the raw numbers from the gps object look like (before the call to setTime)?

Absolutely Fine....

the 2 different times produced both before and after (one is the correct GMT time, the other is the adjusted time stamp) the problem is down to it simply not encoding the new date/time stamp..

Year = gps.date.year();
       Month = gps.date.month();
      Day = gps.date.day();
       Hour = gps.time.hour();
       Minute = gps.time.minute();
      Second = gps.time.second();

        // Set Time from GPS data string
        setTime(Hour, Minute, Second, Day, Month, Year);
        // Calc current Time Zone time by offset value
        
        
        adjustTime((UTC_offset * SECS_PER_HOUR) + SECS_PER_HOUR/2);           
      
       Year = year();
       Month = month();
       Day = day();
       Hour = hour();
       Minute = minute();
       Second = second();

AdjustTime works and then it does not work, works then does not work, etc etc... (Serial monitor shows GPS side working)

Any change if you hard code the 9 1/2 hour offset?

adjustTime(34200L);

Hackscribble:
Any change if you hard code the 9 1/2 hour offset?

adjustTime(34200L);

returns

Location: INVALID Date/Time: 9/10/2014 00:03:30.Pausing for 5 seconds

Location: INVALID Date/Time: 9/10/2014 14:33:30.Pausing for 5 seconds

Location: INVALID Date/Time: 9/10/2014 14:33:30.Pausing for 5 seconds

Location: INVALID Date/Time: 9/10/2014 00:03:41.Pausing for 5 seconds

Location: INVALID Date/Time: 9/10/2014 00:03:46.Pausing for 5 seconds

Location: INVALID Date/Time: 9/10/2014 14:33:46.Pausing for 5 seconds

if ((gps.time.isValid()) && gps.date.isValid() )
{
Year = gps.date.year();
Month = gps.date.month();
Day = gps.date.day();
Hour = gps.time.hour();
Minute = gps.time.minute();
Second = gps.time.second();

// Set Time from GPS data string
setTime(Hour, Minute, Second, Day, Month, Year);
// Calc current Time Zone time by offset value
adjustTime(34200L);

// adjustTime((UTC_offset * SECS_PER_HOUR) + SECS_PER_HOUR/2);

Year = year();
Month = month();
Day = day();
Hour = hour();
Minute = minute();
Second = second();

if (Hour < 10) Serial.print(F("0"));
Serial.print(Hour);
Serial.print(F(":"));
if (Minute < 10) Serial.print(F("0"));
Serial.print(Minute);
Serial.print(F(":"));
if (Second < 10) Serial.print(F("0"));
Serial.print(Second);
Serial.print(F("."));

}
else
{
Serial.print(F("INVALID GPS DATA"));
}
Serial.println("Pausing for 5 seconds");
Serial.println();
delay(5000);


Fixed, see post 1.