Friday, 5 October 2012

Linux Time server using NMEA output from a GiSTEQ USB GPS Dongle

It's amazing how accurate the time on a GPS device actually is, with as little as 4 satellites your location can be triangulated and time obtained. The time comes from 4 caesium or rubidium atomic clocks on board each of those satellites, the atomic clocks are so accurate they lose no more than one second in millions of years.

You'll need to install ntpd and gpsd on your system, in Ubuntu:

sudo apt-get install ntp gpsd

For older versions of Ubuntu eg. 11.04, you may need to get a newer version of gpsd, in my case I experienced a silent failure as a result of an older gpsd version. If you cat /dev/ttyUSB0 (assuming thats your device) and don't get any output try:

Setting Serial Baud: sudo stty -F /dev/ttyUSB0 38400
or Initializing the device: sudo minicom --device /dev/ttyUSB0

Spare netbook I have running as a NTP server, indoors but still has a fix from 8 GPS satellites...

To configure the gpsd edit the /etc/default/gpsd file and change it to look like this:

# Default settings for gpsd...
START_DAEMON="true"
GPSD_OPTIONS="-b -n"
DEVICES="/dev/ttyUSB0"
USBAUTO="false"
GPSD_SOCKET="/var/run/gpsd.sock"

Then edit the file /etc/ntp.conf and change it to look something like this (just so you now, this file will allow remote systems to sync to your server):

driftfile /var/lib/ntp/ntp.drift

# Enable this if you want statistics to be logged.
statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# Query the local GPSD daemon
server 127.127.28.0 minpoll 4 maxpoll 4 iburst
fudge 127.127.28.0 time1 0.420 refid GPS

server 127.127.28.1 minpoll 4 maxpoll 4 iburst prefer
fudge 127.127.28.1 refid PPS

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

You can go through the ntp.conf file and change it to your own setup, the above worked perfectly for me.

You can check your config on the time server by using: ntpq -p (sample output below)

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*SHM(0)          .GPS.            0 l   15   16  377    0.000   -2.578   5.689
 SHM(1)          .PPS.            0 l    -   16    0    0.000    0.000   0.000

You can query the time server remotely with ntpdate (see below), remove the -qv to set your local clock to the time server clock.

root@etienne-Lenovo:~# ntpdate -qv 192.168.1.177
 5 Oct 14:22:08 ntpdate[3569]: ntpdate 4.2.6p2@1.2194 Fri Sep  2 18:37:16 UTC 2011 (1)
 5 Oct 14:22:16 ntpdate[3569]: adjust time server 192.168.1.177 offset 0.005287 sec

Overall I'm extremely happy with the GiSTEQ USB GPS dongle, it's a reliable device and a perfect device for the $40 it cost me with shipping to South Africa. For my time server purposes and playing around it works perfectly - that being said - the time keeping club members are very strict! They usually speak of inaccurate in nanoseconds (ie. Linux won't cut it as it uses milliseconds, so they tend to use BSD) and would probably never consider a non Pulse Per Second (PPS) device for time keeping... ;-)

No comments:

Post a Comment