In this article, I will tell you how to build a Stratum 1 NTP server on a Raspberry PI to synchronize time for a modest amount and forget about the problems associated with inconsistent time on all your devices. And most importantly, it will give the result two orders of magnitude more accurately than a regular server.

In the previous article , devoted to the synchronization of time by radio and SRNS (radio navigation communication system), I did not have time to tell about choosing a GPS/GLONASS receiver with PPS output. Meanwhile, the accuracy of signal reception depends on this, the value can range from one millisecond to several microseconds and often this is crucial.

For the most accurate time signal reception, you need a GPS/GLONASS receiver with PPS output. However, the fact is that in the Russian market it is not easy to get a device with such characteristics at an affordable price. Many of these models have long ceased to produce, and in abandoned online stores with layout of the 1990s only their descriptions were left with an offer to subscribe to a notification upon receipt of the goods.

ITKarma picture

A complete list of tested GPS equipment can be found on the NTPSec GitLab resource. It is not difficult to notice that a small number of devices on the list have a 3-4 star mark and the PPS option. Thus, the following receivers fall into the shortlist.

  • Garmin GPS-18, not USB *** (approximate price of 10 thousand rubles)
  • GlobalSat MR-350P ****
  • Jackson Labs FireFly-II ***
  • Magellan Thales AC12 ***
  • Motorola Oncore GT + ***
  • Navisys GR601-W ****
  • SkyTraq SKG16B ****
  • Trimble Lassen IQ ***
  • u-blox ANTARIS LEA-4T ***
  • u-blox EVK 6H ****
  • u-blox LEA SQ ****

4 * Excellent performance: gpsd recognizes the receiver quickly and reliably, and reports are generated completely and correctly.

3 * Good performance: gpsd with slight problems or delay recognizes devices, but reports are generated completely and correctly.

If you are not afraid of the price of these models, and also there is no great desire to tinker with the glands, you can not read further. A receiver connected to the server via USB or RS232 interface will provide much greater accuracy in determining the time than an NTP server running on tcp/ip. But if the samurai’s path is no stranger to you, then let's build our Raspberry PI NTP server with GPS time synchronization.

Putting the Raspberry PI

So: we take the following components for our micro server.

  1. Raspberry Pi 4 Model B, 4 GiB RAM (6,200 rubles);
  2. Case, for example such (890 rubles);
  3. Micro SD card on 32 GiB, 16 GiB possible; (540 rub.)
  4. GPS module on a u-blox NEO-M8 chip (1700 rubles with an antenna);
  5. GPS antenna at 15 dB;
  6. Soldering iron.

Actually, the u-blox NEO-M8 is equipped with a UART interface, but for PPS output it is necessary to solder pin-3 on the GPS module to the corresponding GPIO connector on the Raspberri Pi board. The Swiss company module has gained popularity among specialists and this is no coincidence, the characteristics speak for themselves.

  • Supported SRNS: BeiDou, Galileo, GNSS; GPS/QZSS, GLONASS;
  • Supply voltage: V;
  • Interfaces: UART, USB, SPI, DDC, I2C;
  • Supported protocols: NMEA 0.183 version 4.0, UBX (binary), RTCM 2.3;
  • Sensitivity at detection: -167 dBm;
  • Tracking sensitivity: -160 dBm;
  • Cold start time: 26 s;
  • Hot start time: 1.5 s;
  • Power consumption: 35 mW;
  • Operating temperature: -40. + 85 ° С;
  • Dimensions: 16x12.2x2.4 mm

In this configuration with the latest equipment, the approximate total price of the assembled Raspberry PI is 9330 rubles. You can save by buying a Raspberry PI 3, or four with 2 GiB RAM. You can also save on a GPS chip, u-blox NEO-6M with an antenna costs about 650 rubles.Then the price of the NTP server will drop to 5500 rubles.

ITKarma picture

GPS/Glonass module UBLOX NEO 8M

The question may arise, why are all these investments necessary and what accuracy does this or that method of time synchronization provide. A small summary plate for reference.
Time Source Inaccuracy
Atomic GPS ± 50 nSec
KPPS ± 1 μSec
PPS ± 5 μSec
USB 1.1 Interface ± 1 mSec
USB 2.0 Interface ± 100 μSec (100000 nSec)
NTP over the network ~ ± 30 mSec

Kernel PPS (KPPS) differs from PPS in that it uses the kernel function Linux/Unix for accurate timestamps of state changes in the PPS line. Normal PPS is implemented in user-space. If the Linux kernel supports KPPS through the RFC 2783 API, gpsd will use it to increase accuracy.

Many Linux distributions have a pps-tools package that provides KPPS support and sets the timepps.h header file. Be sure to install this package.

(1:1146)$ sudo emerge -av pps-tools Local copy of remote index is up-to-date and will be used. These are the packages that would be merged, in order: Calculating dependencies... done! [binary R ] net-misc/pps-tools-0.0.20120407::gentoo 0 KiB Total: 1 package (1 reinstall, 1 binary), Size of downloads: 0 KiB Would you like to merge these packages? [Yes/No] 

Thus, by connecting a GPS receiver with a PPS output via USB, we get a 300x increase in the accuracy of time synchronization. Reading from the GPS chip on the board in KPPS mode gives an increase in accuracy two more orders of magnitude .

Let's be honest - not all of us know how to use a soldering iron, not everyone uses this tool regularly. In this case, it makes sense to look for a GPS module that does not require soldering contacts.

ITKarma picture

Raspberry Pi GPS/RTC Expansion Board

There are few such boards , and they are expensive. The difference in price will fully pay off the purchase of a soldering iron and labor costs.

OS Installation

There is a Raspberry PI OS as well. k. a. Raspbian, you can simply go link , download the latest version and install it. Many people do this, but let's recall that Raspberry PI 4 supports a 64-bit operating system, while Raspberry PI OS so far has only 32-bit Debian Linux modifications for the Arm architecture.

There is such a point of view that on a 64-bit OS it is not justified on Raspberry PI 4, since there is no way to provide a performance increase due to the peculiarities of architecture and assembly. It seems to me doubtful, I already wrote about it on Habré - 64-bit OS is faster .

There is a Debian Linux port for the arm64 architecture, however distribution Ubuntu for Raspberry PI has a distinct page and instructions. On the page we find additional confirmation that it is better to choose a 64-bit OS.

ITKarma picture

Installation Inventory:

  • Raspberry Pi 4;
  • USB-C power cable for Pi 4;
  • Micro SD card with installation image of Ubuntu;
  • Monitor with HDMI output;
  • MicroHDMI cable;
  • USB keyboard.

The next step is to write the installation image to the Micro SD card. If at your fingertips there is no specialized utility and program for these purposes, in the simplest case it is enough.

sudo dd if=/path/to/ubuntu-core-arm64.iso of=/dev/mmcblk0 status=progress 

The exact name of the device is visible in the dmesg output when a new device is detected.

PM: Adding info for No Bus:179:0 device: 'mmcblk0': device_add PM: Adding info for No Bus:mmcblk0 

By inserting a Micro SD card, connecting an HDMI monitor, a USB keyboard, and a power cable, boot into the Ubuntu Server on the Raspberry Pi. The default username and password is ubuntu.

Configuring an NTP server

  1. If Raspberry PI is turned on in console mode (headless), first you need to determine the IP address of the device. From the workstation, type the following command.

    (1:1151)$ arp -na | grep -i "dc:a6:32" 

    If the Pi is connected to an HDMI monitor and USB keyboard, skip steps 1-2 and proceed immediately to installing the packages.
  2. Connect via ssh

    (1:1152)$ ssh ubuntu@<Raspberry Pi’s IP address> 
  3. Install the required packages.

    user@server ~$ sudo apt-get install aptitude user@server ~$ sudo aptitude install wpasupplicant gpsd chrony 
  4. Set up Wi-Fi connection with wpasupplicant.
  5. On Linux UART0, the Pi interface is represented by the device file/dev/ttyAMA0. In order to free the UART0 interface for a GPS receiver, you need to change the boot parameters of the Linux kernel. You must disable console=ttyAMA0,115200, replacing it with console=tty1. To do this, in the file/etc/default/grub, change GRUB_CMDLINE_LINUX_DEFAULT. If the file/boot/config.txt exists, you can also set the same options in it.

    Raspberry Pi 4 has 6 UARTs
    Title Type Device Appointment
    UART0 PLO11 /dev/ttyAMA0 secondary (Bluetooth)
    UART1 mini UART /dev/ttyS0 primary
    UART2 PLO11
    UART3 PLO11
    UART4 PLO11
    UART4 PLO11
    By default, UART2-5 is turned off.

    As the name implies, UART0 is a full-fledged serial port and it has higher performance than the trimmed UART1, also known as mini UART. Therefore, it will not be superfluous to transfer Bluetooth to UART1 so that the main data stream goes through UART0. To do this, set/etc/default/grub, or/boot/config.txt enable_uart=1.
  6. In the file/etc/defaults/gpsd should be set.

    DEVICES="/dev/ttyAMA0/dev/pps0" GPSD_OPTIONS="-n" USBAUTO="false" 
  7. Start or restart gpsd.

    user@server ~$ sudo/etc/init.d/gpsd start user@server ~$ sudo/etc/init.d/gpsd restart 
  8. Checking GPS module operation.

    user@server ~$ cat/dev/ttyAMA0 user@server ~$ cgps -s user@server ~$ ppstest/dev/pps0 
  9. Edit the file/etc/ntp.conf.

    All lines containing network public Stratum 1, 2 NTP servers (such as pool [0-9] should be commented out to use only GPS/PPS data sources.

    # GPS Serial data reference (NTP0) server minpoll 4 fudge flag1 1 time1 0.9999 refid GPS #flag1 - PPS on 

    # GPS PPS reference (NTP1) server minpoll 4 fudge flag3 1 refid PPS #flag3 - enable KPPS API 

    The top NTP0 record indicates a universal time source available on almost all GPS devices. The bottom NTP1 entry defines a much more accurate PPS source.
  10. Restart ntpd

    user@server ~$ sudo/etc/init.d/ntpd restart 

Materials used

ITKarma picture.