To get this all working, I started with a fresh install of Raspbian on a 4GB SD Card. I ran through it several times and got the same result every time. Before getting started, you will want to follow the step I mentioned in my first NTP post about setting your timezone. If that isn’t done your new NTP server won’t show the correct local time.
Before I get started, I will assume that you have your GPS Receiver from Adafruit already connected per the instructions on their website. The first thing that needs to be done is verify that the USB Console cable is connected and seen by Raspian. You do this by using the lsusb command. You should see something similar to this on your screen –
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
This is a good sign. We next need to verify that the your Raspberry Pi is getting the datas stream from the GPS Receiver. You do this by using the following command – cat /dev/ttyUSB0. The output you will get should look something like this –
pi@raspberrypi:~$ cat /dev/ttyUSB0
You should get a few lines and then the display may stop. If it doesn’t stop, you can press Ctrl-C and it will. Nothing to worry about. That says that everything is working. Now we need to get the software installed to allow the GPS to talk to the NTP service.
sudo apt-get install gpsd gpsd-clients python-gps
Once the software is downloaded and install, we need to verify that the GPS receiver is sending data to your RPi –
pi@raspberrypi:~$ cgps -s
cgps: no gpsd running or network error: -4, can’t create socket
This error indicates that the gpsd daemon isnt running. We need to see if it is stopped or having another problem. Using the sudo service gpsd status command will give us that info
pi@raspberrypi:~$ sudo service gpsd status
gpsd is not running … failed!
Since the service isn’t running, it will need to be started –
pi@raspberrypi:~$ sudo service gpsd restart
Restarting GPS (Global Positioning System) daemon: gpsd.
We next need to check that the GPS service is running correctly, using the cgps -s command. If you get the following error – cgps: GPS timeout, the following three commands will resolve the problem –
sudo killall gpsd
sudo gpsd /dev/ttyUSB0 -F /var/run/gpsd.sock
sudo service ntp restart
Once that is done, you can run the cgps -s and get the following output –
│ Time: 2013-02-09T21:32:00.000Z ││PRN: Elev: Azim: SNR: Used: │
│ Latitude: 38.902806 N ││ 31 60 208 42 Y │
│ Longitude: 94.386301 W ││ 14 57 044 36 Y │
│ Altitude: 303.3 m ││ 22 50 132 32 Y │
│ Speed: 0.0 kph ││ 51 42 200 47 Y │
│ Heading: 0.0 deg (true) ││ 25 31 086 32 Y │
│ Climb: 0.0 m/min ││ 32 31 311 33 Y │
│ Status: 3D FIX (156 secs) ││ 1 29 299 32 Y │
│ Longitude Err: +/- 1 m ││ 11 23 273 23 Y │
│ Latitude Err: +/- 3 m ││ 12 17 051 33 Y │
│ Altitude Err: +/- 12 m ││ 18 15 131 28 N │
│ Course Err: n/a ││ 20 06 312 18 N │
│ Speed Err: +/- 24 kph ││ 30 04 178 19 N │
│ Time offset: 0.654 ││ │
│ Grid Square: EM28tv ││ │
With that part running, you now need to configure ntp to accept the GPS receiver as a time source. Add the following lines to ntp.conf to let it know that it is going to be a NTP Server using a GPS clock source – sudo nano /etc/ntp.conf
server 127.127.28.0 minpoll 4
fudge 127.127.28.0 time1 0.183 refid NMEA
As with any change to ntp.conf, you will restart ntp service to reflect this changes you just made – sudo service ntp restart. Use the ntp query program, ntpdc to see how things are working –
pi@raspberrypi:~$ ntpdc -p
remote local st poll reach delay offset disp
*SHM(0) 127.0.0.1 0 16 17 0.00000 0.036143 0.97069
Then things are working. You should see a set of numbers under the offset column. If you see all zero’s such as below, then you have a problem in getting the gpsd to talk to ntp. This is something to watch over a period of time. Sometimes I will see numbers under the offset, sometimes not, most of the time I do. If you always see zeroes under the offset column, you may have a problem. The key thing to watch is the Time offset field in cpgs app. If the number fluctuates up and down, that says things are working, If numbers are always climbing, you have some type of configuration problem.
That’s it, you now have a fully function GPS clock based NTP Server for less than $200. This is a bargain when you compare that to the $1200 to $2000 that some of the commercial vendors ask for their equivalent products. I would suggest that you get the external antenna also available from www.adafruit.com. To connect the external antenna to the GPS receiver, you will need a short adapter cable due to the small connector on the GPS circuit board to the connector on the end of the GPS antenna.
To see more of my posts about the Raspberry Pi, please go to http://www.ronnutter.com/category/raspberry-pi/