Having an accurate time source is important for some digital modes, and normally not a problem when your Raspberry Pi is connected to the internet.

But what about when you are at sea or in the middle of the forest or anywhere else with no internet connection?

I tried setting up the Raspberry Pi to use the GPS NMEA source as an NTP server following some instructions that others had some success with, but for some reason I couldn’t get mine to work.

So I thought I would figure out how to do it myself, but as usual these days it doesn’t take long to find someone who has already done it – https://wb4son.com/wpblog/?p=1635 have a read, it explains it all, below is the script.

 

 

#! /usr/bin/python

import os
import sys
import time
from gps import *

print ‘Set System Clock to GPS UTC time’

try:
    gpsd = gps(mode=WATCH_ENABLE)
except:
    print ‘ERROR: No GPS found, the time has not been set’
    sys.exit()

   while True:
       #wait until the next GPSD time tick
      gpsd.next()
      if gpsd.utc != None and gpsd.utc != ”:
     #gpsd.utc is formatted like”2015-04-01T17:32:04.000Z”
     #convert it to a form the date -u command will accept: “20140401 17:32:04”
     #use python slice notation [start:end] (where end desired end char + 1)
     # gpsd.utc[0:4] is “2015”
     # gpsd.utc[5:7] is “04”
     # gpsd.utc[8:10] is “01” 
     gpsutc = gpsd.utc[0:4] + gpsd.utc[5:7] + gpsd.utc[8:10] + ‘ ‘ + gpsd.utc[11:19]
     os.system(‘sudo date -u –set=”%s”‘ % gpsutc)
     sys.exit()

Copy the above to a file and save it, I called my setclock.py

make it executable with this command

    chmod a+x set clock.py

 

In order for this to work you must first install and configure gpsd

1. Install the relevant GPS software using the following command:

sudo apt -y install gpsd gpsd-clients python-gps

2. Now edit the configuration file:

sudo nano /etc/default/gpsd

3. The setting should look like this (update as necessary):

    START_DAEMON=”true”

    USBAUTO=”false”

    DEVICES=”/dev/ttyACM0″

    GPSD_OPTIONS=”-n”

4. Save the file and Reboot the Raspberry Pi

sudo reboot

5. You can check if the gpsd service is active using this command:

systemctl is-active gpsd

   and you can see the output from the GPS using

gpsmon -n

 

Once that’s all done you can run the python script to set the time

   ./setclock.py

 

if you want to test that its actually working simply set the time to something else, then run the script (note the desktop clock does not update right away so ignore that and use the command line)

%d bloggers like this: