Setting time through GPS dongle

6,819

Solution 1

Since $GPSDATE is being reported as

Sun Aug 8 06:08:11 PKT 2010

the date -s command is doing exactly what you are telling it to do. Why is it reporting a wrong year (or nothing at all)? I have no idea.

Since you are already using ntpd why are you not content to let NTP do its thing? Is this an "I'd like gpsdate to work because it is there" issue?

The best way to help us help you debug this it by giving the full output of

gpsdate -w

in your question.

added in response to gpsdate output:

According to the gpsd documentation when the mode field of a TPV record is 1 it means "no fix". This means that the GPS receiver has not seen enough (or any) satellite data. This implies that the time value is junk which certainly makes sense if you are seeing a year of 1990 or 2014. In your script, the sleep 2 gives very little time for gpsd to get a proper GPS fix. Waiting longer might help, not having a proper antenna attached means you will wait forever for a fix.

The Network Time Protocol implemented by ntpd does not need GPS to work. It exchanges time synchronization information with other internet hosts, some of which do get their clock from a reference like GPS. Since your script stops and restarts your ntpd, you could just forget about a GPS fix and work with that. For example, the machine I am typing this on uses ntpd and is +4.7 milliseconds off of UTC which is more than accurate for many purposes. You can use ntpq -p to find out if and how well your clock is synchronized.

Solution 2

… "mode":1 …

That's why. You don't have a GPS fix and the GPS receiver likely has no idea what time it is. You want mode to be 3.

From the fact that SKY is being reported without any additional data, I'd further guess your receiver doesn't even have an almanac yet.

Leave the GPS running with a clear view of the sky for at least 15 minutes (yes, really). That should improve things.

See the documentation here for what those fields mean.

Share:
6,819

Related videos on Youtube

Dark Coder
Author by

Dark Coder

Updated on September 18, 2022

Comments

  • Dark Coder
    Dark Coder almost 2 years

    There is this code that claims to set the time of your Linux environment,

    http://blog.petrilopia.net/linux/raspberry-pi-set-time-gps-dongle/

    date -s '01/01/2014 00:01'
    sleep 1
    pkill ntpd
    pkill gpsd
    gpsd -b -n -D 2 /dev/ttyUSB0
    sleep 2
    GPSDATE=`gpspipe -w | head -10 | grep TPV | sed -r 's/.*"time":"([^"]*)".*/\1/' | head -1`
    echo $GPSDATE
    date -s "$GPSDATE"
    /usr/sbin/ntpd
    

    But When I am running this code through puTTy it sets my time to,

    Sun Aug 8 06:08:11 PKT 2010

    I'd like to know why it is setting my GPS time to 2010 while its 2013.

    Here is the Output of gpspipe -w command

    {"class":"TPV","tag":"MID2","device":"/dev/ttyUSB0","mode":1,"time":"1990-12-22T23:59:53.020Z","ept":0.005}
    {"class":"TPV","tag":"MID2","device":"/dev/ttyUSB0","mode":1,"time":"1990-12-22T23:59:54.020Z","ept":0.005}
    {"class":"SKY","tag":"MID4","device":"/dev/ttyUSB0","time":"1990-12-22T23:59:55.010Z"}
    {"class":"TPV","tag":"MID2","device":"/dev/ttyUSB0","mode":1,"time":"1990-12-22T23:59:55.010Z","ept":0.005}
    {"class":"TPV","tag":"MID2","device":"/dev/ttyUSB0","mode":1,"time":"1990-12-22T23:59:56.020Z","ept":0.005}
    {"class":"TPV","tag":"MID2","device":"/dev/ttyUSB0","mode":1,"time":"1990-12-22T23:59:57.020Z","ept":0.005}
    

    The output is continuous, which means it is changing every second.

    • msw
      msw over 10 years
      What is the output of the echo $GPSDATE?
    • Dark Coder
      Dark Coder over 10 years
      As for now, It display nothing at all, earlier it was displyaing Sun Aug 8 06:08:11 PKT 2010
  • Dark Coder
    Dark Coder over 10 years
    hello, i do not know anything about ntpd , and have never used GPS before. P.S: I have added the output you have asked for
  • msw
    msw over 10 years
    @Xufyan see "added" above.