4

I've got a USB GPS receiver permanently attached to my raspberry pi. At boot time, I would like raspbian to wait until the GPS has a signal, then set the OS's time to whatever the GPS says.

I've already installed gpsd and gpspipe and I'm logging the output to a file.

Does anyone know how to achieve it?

Fidel
  • 181
  • 1
  • 1
  • 8

3 Answers3

4

The answer is to run a script which reads the GPS, then sets the system time. I found the following code here.

Create a bash file with the following:

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

To convert from GMT to your local time zone, add the following:

FIDATE=`date --date='+3 hour'`
echo $FIDATE
date -s "$FIDATE"
sleep 1
Fidel
  • 181
  • 1
  • 1
  • 8
4

I'm not sure if the script you have found is useful at all: parsing the output of gpspipe with a shell script is anything but fast. You'll most probably get worse clock accuracy using your script than you would by simply synchronizing with pool.ntp.org (unless you don't have network connection at all, or use a modem).

Both ntpd and chrony can use GPS time source directly, if you tell them so in the config. For example, check out chrony manual and search for refclock. You'll see the examples of configuration for different kinds of GPS devices (PPS or NMEA). Note that PPS (pulse per second) GPS devices require an additional clock source to be configured, to tell which second has just elapsed. For example:

server pool.ntp.org refid NTP
refclock PPS /dev/pps0 lock NTP

This will fetch your time from the network as usual, but will constantly readjust it to pulses form the PPS device.

For a NMEA GPS, the config would be:

 refclock SHM 1 poll 3 refid GPS1

Here is a nice tutorial which explains how GPS time servers work in greater detail.

Also check out this question.

Dmitry Grigoryev
  • 28,277
  • 6
  • 54
  • 147
1

Don't forget that GPS time is not 100% equal to GMT. GMT is regularly adjusted with leap seconds so if you want your clock accurate to the second you will have to add some seconds to the GPS time. This is also explained in the answer to question 8552086

ON5MF Jurgen
  • 313
  • 2
  • 12