Raspberry Pi: Set time from GPS dongle

I had a little problem with my Raspberry Pi Raspbian Wheezy and with Kali-Linux when I wanted to do some wardriving
and wanted to have right time/date on my Pi and I didn’t have anykind of Internet connection.

You could get right time from the GPS but I didn’t get it to work with NTP and my GPS receiver
but I found a nice script that get’s me around that problem:

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`
date -s "$GPSDATE"

As far as I know this sets your Pi time to GMT but if that is not your local time then you can add this:

FIDATE=`date --date='+3 hour'`
echo $FIDATE
date -s "$FIDATE"
sleep 1

This one correct time to be my local (Finnish) time which is +3 hours from that GMT which you get from GPS.

So this script first sets your Pi time to 01/01/2014 00:01 because you need to set time near the right time before getting it from the GPS and after that this script
reads NMEA data from GPS and grep the time part from it and parse it so that it can be set to your Pi using normal date command. You have to have working GPS before trying this.
I have used this for a while with my wardrive Raspberry Pi setup with Raspbian Wheezy and with Kali-Linux distribution.

9 comments to Raspberry Pi: Set time from GPS dongle

  • Alayna Ratley

    Thanks for another great article. The place else could anyone get that type of info in such a perfect way of writing? I’ve a presentation subsequent week, and I am on the search for such information.

  • yoyoma

    Thanks for sharing this. Since the gps uses UTC, just use the -u option with the date command and no need to fiddle with timezone adjustments:

    date -u -s “$GPSDATE”

  • Michele

    is there a way to sync only when the gps is “fixed/locked” to a gps?


    Another solution is to use this:

    Works fine for me.

  • Annu006

    Guys this code is not working for me as I am using GNSS receiver and gpspipe is giving error. Can anyone guide me in this regards that how can I update my Raspberry time with GNSS time ?

  • R!

    Hey thanks for this writeup.
    i need to modify the script to get it working on my setup.
    the script above gives me something like “2014-09-25T18:46:38.990Z” but my date (openwrt version) want to have the time like this “2014-09-25 18:46:38”
    so i modified it:
    GPSDATE=$(gpspipe -w -n 10 | grep TPV | sed -r 's/.*"time":"([^"]*)".*/\1/' | tail -n 1 | sed -e 's/^\(.\{10\}\)T\(.\{8\}\).*/\1 \2/')

  • Jussi

    Okay, I think I got it. Snip from http://unix.stackexchange.com/questions/97224/setting-time-through-gps-dongle:

    … “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.

    I guess you will just have to wait for valid date from GPS.

  • Jussi

    For some reason date which I get from here is from year 1994 December (while now is July 2014). Previously I used solution as described before, but now it seems not work anymore. gpsmon is printing correct date so I guess that they are representing date in funny format. Any clues?

  • Fidel

    Thanks! This was very useful!

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



This site uses Akismet to reduce spam. Learn how your comment data is processed.