Posted on

Raspberry Pi Stretch – GPS Dongle as a time source with Chrony & Timedatectl

The latest release of Raspbian Stretch for the Pi has replaced the familiar NTP application with a lightweight solution using timedatectl. As a result many of the online tutorials for adding  a GPS USB dongle are out of date.

After some experimenting, I have devised the following process:

For my tests, I was using the Diymall VK-172 GPS dongle that’s available from Amazon UK for £12.95. This is a ublox 7020 based dongle.

Start with a fresh download of Stretch and do the usual update/upgrade to get the latest patches.

1 — Install the following software:

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

 

2 — Next step is to make some changes to the gpsd configuration file as follows:

sudo nano /etc/default/gpsd

3 — In the file that opens, add or amend lines to make sure the following is present:

START_DAEMON=”true”

USBAUTO=”true”

DEVICES=”/dev/ttyACM0″

GPSD_OPTIONS=”-n”

Hit ctl-x followed by y to close and save the file.

4 — Reboot the Pi and check that the following services are active:

systemctl is-active gpsd

systemctl is-active chronyd

5 — You can use any of the following three commands to check that the GPS is visible and delivering NMEA words. NB: most GPS dongles will show a flashing LED when they have a fix:

cgps – s   or   gpsmon -n  or   xgps

6 — Next, we need to make a change to the chrony configuration file:

sudo nano /etc/chrony/chrony.conf

Add the following line to the end of the file:

refclock SHM 0 offset 0.5 delay 0.2 refid NMEA

Hit ctl-x followed by y to close and save the file.

7 — You can now check chrony’s sources with the command:

chronyc sources -v

If you are connected to the network, you will see a list of available time servers plus the GPS source which will be shown as NMEA. If you’re not network connected, you will just see the NMEA source listed. The two punctuation characters immediately before NMEA, indicate its status and you need to see #* where # means thge GPS is recognised as a local clock and * means that it’s being used to synchronise the Pi system time.

8 — You can now use chronyc to provide a more detailed view using the command:

sudo chronyc tracking.

This will confirm that NMEA is being used as the reference and will list the time difference between the reference and the system clock. To avoid clock jitter that can cause lots of software problems, chrony slowly changes the system clock until it matches the reference. However, this makes for a very slow synchronisation.

9 — If you want to quickly synchronise the time, use the following command to make a step change to the system clock:

sudo chronyc makestep

Operational Notes

If you boot the Pi with a network connection, chrony will automatically sync to the time server with the shortest propagation delay. If you remove the network connection after boot, it will take a while for chrony to switch back from the network server to the local GPS. This is because Chrony makes repeated attempts to reach what it considers to be the best timeserver. The simplest way to overcome this delay is to reboot the Pi without a network connection. It will immediately revert to using the NMEA (GPS) data as it’s the only timing source available.

NB: Pi Pixel Clock Update Delay – Please note that the PIXEL desktop clock (top right corner) only synchronises with the system time every 5 minutes. As a result, updates to the system time can take up to 5 minutes to permeate to the desktop clock! The most reliable way to check the system time is to use: sudo timedatectl. This shows all the vital timing detail and synchronisation with GPS USB dongle is indicated when the ‘NTP Synchronized’ entry reports yes.

Here are some useful time-related commands:

sudo date -s “Aug 7 09:15” – Manually sets the system time and is useful when testing RTC and GPS units.
sudo hwclock -w – This command updates the RTC with the current system time. Use this when you are connected to a network and want to force the RTC to sync with network/system time.
sudo hwclock -r – This displays the current RTC time and is useful for checking the RTC.
sudo hwclock – s -Sets the system time from the RTC
sudo hwclock –set –date ”8/11/18 15:24:00” Manually sets the RTC time and date – useful for testing.
sudo timedatectl – Displays the status of all the Pi clock sources.
sudo chronyc makestep – Forces the system time to make a step change to the reference time, i.e. GPS. This avoids the time lag caused by chrony incrementally adjusting the clock.

 

Mike Richards – G4WNC

 

 

16 thoughts on “Raspberry Pi Stretch – GPS Dongle as a time source with Chrony & Timedatectl

  1. Hello Mike
    Absolutely brilliant tutorial!
    73
    Julian oh8stn

    1. Hi Julian,

      Thanks for the feedback. Glad you found the tutorial useful. Mike – G4WNC

  2. Hello Mike,
    been very helpfull, works great, thanks a lot.
    73 de Marc, ON4BBD

    1. My pleasure. Mike – G4WNC

  3. Agree great tutorial. Just followed it and got my pi synced with gps dongle in about 30 minutes with wi-fi off. Setting up a field ham radio station on a pi. Software need exact timing to communicate successfully. Thanks from Horace Hinson KI5WA

    1. Hi Horace,

      Glad you found it useful. Mike – G4WNC

  4. THANK YOU FOR SHARING!!!
    It’s great to be learning from others, Thanks for taking the time to help others with GREAT step by step instructions that WORK! I barely know how to get my Pi turned on and I followed these instructions and Bam! gps setup.. Done!

    on to Digi modes 73,
    KG7ALK

    1. Excellent – glad you found it useful. Mike -G4WNC

  5. First of all thanks for the excellent tutorial. I followed the steps on my rpi and everything went smooth on the installation. My GPS is working fine, I see the blinking light, the time update, the NMEA data and all that good stuff.

    My issue now is that when I go to JS8Call or WSJT-x it gives me the following error and is not connecting to the radio.
    “hamlib error io error while opening connection to rig”

    Writing to see if this is something any of the other members have seen before and may have any suggestions for me to try.

    some notes –
    js8call was working OK before installing the gps.
    The radio hardware and js8call settings are the same as before the gps.
    Everything works fine after un installing the gps.

    73s
    Norbert

    1. Hi Norbert,

      This is a bit of a mystery!

      I followed my own instructions to install the GPS and tried WSJT-X and saw the same failure. A quick investigation showed that the USB serial port used for CAT had disappeared. You can check this using the command: ls /dev/tty* This lists all the tty devices and the USB CAT port would normally show as /dev/ttyUSB0.
      I then burnt a new sd card and went through the GPS install process step-by-step. At each step, I checked to see if the ttyUSB0 device was still present. It remained present throughout the process and I couldn’t replicate the fault.
      I even tried burning another card and installing from the instructions without any checks and all was well.

      At the moment I’m a bit stuck as I can’t replicate the problem here.

      It may depend on whether the CAT and GPS dongles are plugged in during the install.

      I’ll have another go in the morning

      Mike – G4WNC

  6. Hi, I got same problem as Norbert with my CAT cable on FT-897 but now something else happening and no idea how to solve.
    GPS working fine but off network won’t synchronize NTP anymore.
    Now of course the JS8Call in itself working fine.
    Tried all again with new card and again CAT problem with GPS running.
    It doesn’t make any difference if you are running on or off network.
    ttyUSB0 still there too.
    It seems things go wrong when GPS synchronizes NTP.

    1. Hi Kees,

      Sorry to hear you’re having problems. I’ve been trying to replicate the problem here without success.
      I’ve just created a new data modes card with FLDIGI, WSJT-X and QSSTV installed. I updated the OS to make sure I had all the latest fixes and installed GPSD and Chrony as per my blog instructions and all is working ok. I have the time derived from the GPS dongle with the network disconnected and rig control is still working on all three decoders.
      Are you using the latest OS and data modes software? Also are you using the same GPS dongle as in the blog?

      It’s hard for me to fix the problem is I can’t replicate it here.

      Regards,

      Mike – G4WNC

      1. Found a solution for the problem.
        Maybe someone more familiar with Raspbian comes up with an easier way.
        After trying 3 cards on 2 pi 3’s and 1 pi 2 and which all gave same troublem, even without a gps actually connected I finally came to this solution which works for me.

        Switch off hotplugging by changing gpsd file to USBAUTO=”false”
        When Raspberry-Pi is running start xps and only then the system clock updates to GPS time.
        After this JS8Call working fine.

        regards, Kees

        1. Hi Kees,

          Thanks for that fix. I know it’s far from ideal, but it gives me a useful clue as to the source of the problem. I’m away at the RSGB Convention this weekend but will dig deeper when I get back.

          Thanks,

          Mike – G4WNC

  7. Hi all,
    From reading these posts and it seems all my edit lines are Ok as far as chronyc, gpsd and such, my Ublox doesn’t blink except blue when first plugging it in. I am in an apartment so suspect poor satellite view. Is it possible that the network time overrides the gps until I’m either outside and out of range so it forces the gps for clock info? Also have the Pi Juice hat on as well…Do I simply wait for the data card I ordered and see how that plays?…

    1. Hi Jay,

      Chrony will pick the most accurate time source that it can find. In the case of using a USB GPS dongle, the network NTP servers will usually provide better accuracy due to USB processing delays. The use of a USB GPS dongle is really only intended for those situations when you’re off the grid but need to get the time. USB GPS accuracy is usually around 1/2 second. To get the benefit of very accurate GPS time, you need to use a dedicated GPS board with a 1 pulse per second output (PPS). Whilst the serial data from the GPS provides the high-level time/date data, the PPS pulse provides micro-second accuracy. There is currently a problem when using a USB GPS plus Chrony with FT8Call that causes the USB port to disappear, I’m investigating that problem at the moment. A temporary fix is to change the USBAUTO line in the GPSD config file to false (see note from Kees).
      If you have another PC handy you could try connecting your GPS to that computer. If there’s enough signal the GPS will provide a 1 second flash once it has a fix.

      Regards,

      Mike – G4WNC

Leave a Reply

Your email address will not be published. Required fields are marked *

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