Create a NTP server using Orange-pi Zero 2 and GPS receiver

Posted by:

|

On:

|

In my previous post, I outlined the steps to create an NTP server using a Raspberry Pi 4B and GPS receiver. However, the cost of a Raspberry Pi 4B with a LAN port is quite high. Therefore, I decided to switch to the Orange Pi Zero 2, which can reduce costs by one-third. The steps to configure a NTP server for both Pi are similar. Here is a detailed description of the procedure using the Orange Pi Zero 2.

In this project, a Orange Pi Zero 2 is connected to a Neo7 GPS receiver via a USB cable to obtain GPS data, particularly the time information. The Orange Pi will then be configured as an NTP server for other IP-based devices.

Real Application
In a secure CCTV system, all components, including cameras, network switches, and NVR recorders, must be isolated to prevent the theft of video footage. Accurate timestamps on the video footage are essential for investigations in the event of accidents or robberies. However, isolated CCTV systems cannot access standard time servers via the internet to obtain real-time information. Therefore, a standalone NTP server is required within this isolated network.

What You’ll Need

  • Orange Pi Zero 2 with Ubuntu installed in a 32G SD card
  • Neo7 GPS receiver , with GPS antenna
  • USB cable (USB A to Micro USB)
  • Internet connection for setting up the Orange Pi (can be disconnected after installation is complete)

Let’s start
Connect the Neo7 GPS receiver to the Orange Pi Zero 2 using the USB cable (USB A to Micro USB) . Attach a GPS antenna to SMA jack of Neo7 GPS receiver. Power on Pi Zero 2. Try to add heatsink to the GPS receiver as some chips on it is very hot.

Make sure the GPS antenna are placed in an area with clear view of the sky. GPS signals can be obstructed by buildings, trees, or indoor environments.

I assume everyone knows how to install Orange Pi Ubuntu OS, add/edit users and change the IP address. These procedures will not be discussed here. Connect to the Pi Zero 2 using WinSCP (in case you are using Window PC) and Putty SSH using your PC.

Install Required Software

(1) Update the System

sudo apt update
sudo apt upgrade

(2) Install NTP and GPS Daemon:

sudo apt install ntp gpsd gpsd-clients

(3) Configure GPSD

After connecting the GPS receiver, check the kernel messages to see if the device is detected. Type the following command and look for messages indicating a new device, like  /dev/ttyACM0.

dmesg | grep tty

Returned messages:

[0.000000] Kernel command line: root=UUID=a6a42a97-600f-4ed7-ab06-6a2a169c62f3 rootwait rootfstype=ext4 console=ttyS0,115200 console=tty1 consoleblank=0 loglevel=1 ubootpart=4f045fc2-01 disp_reserve=8294400,0x7bf4fcc0
[0.000253] console [tty1] enabled
[1.881263] uart0: ttyS0 at MMIO 0x5000000 (irq = 280, base_baud = 1500000) is a SUNXI
[1.881421] console [ttyS0] enabled
[1.882502] uart1: ttyS1 at MMIO 0x5000400 (irq = 281, base_baud = 1500000) is a SUNXI
[1.883558] uart5: ttyS5 at MMIO 0x5001400 (irq = 282, base_baud = 1500000) is a SUNXI
[4.660882] systemd[1]: Created slice system-serial\x2dgetty.slice.
[5.580976] mtty_probe init device addr: 0xffffffc009443500
[6.248931] cdc_acm 4-1:1.0: ttyACM0: USB ACM device
[6.496711] systemd[1]: Found device /dev/ttyS0.
[38.359109] mtty_open device success!

Open the GPSD configuration file:

sudo nano /etc/default/gpsd

Change the configuration to:

START_DAEMON="true"
GPSD_OPTIONS="-n"
DEVICES="/dev/ttyACM0"
USBAUTO="true"

Restart GPSD:

sudo systemctl restart gpsd

Make sure GPS device is successfully detected as /dev/ttyACM0, using the following command:

ls /dev/tty*

Returned messages:


/dev/tty    /dev/tty19  /dev/tty3   /dev/tty40  /dev/tty51  /dev/tty62
/dev/tty0   /dev/tty2   /dev/tty30  /dev/tty41  /dev/tty52  /dev/tty63
/dev/tty1   /dev/tty20  /dev/tty31  /dev/tty42  /dev/tty53  /dev/tty7
/dev/tty10  /dev/tty21  /dev/tty32  /dev/tty43  /dev/tty54  /dev/tty8
/dev/tty11  /dev/tty22  /dev/tty33  /dev/tty44  /dev/tty55  /dev/tty9
/dev/tty12  /dev/tty23  /dev/tty34  /dev/tty45  /dev/tty56  /dev/ttyACM0
/dev/tty13  /dev/tty24  /dev/tty35  /dev/tty46  /dev/tty57  /dev/ttyBT0
/dev/tty14  /dev/tty25  /dev/tty36  /dev/tty47  /dev/tty58  /dev/ttyBT1
/dev/tty15  /dev/tty26  /dev/tty37  /dev/tty48  /dev/tty59  /dev/ttyp0
/dev/tty16  /dev/tty27  /dev/tty38  /dev/tty49  /dev/tty6   /dev/ttyS0
/dev/tty17  /dev/tty28  /dev/tty39  /dev/tty5   /dev/tty60  /dev/ttyS1
/dev/tty18  /dev/tty29  /dev/tty4   /dev/tty50  /dev/tty61  /dev/ttyS5

/dev/ttyACM0 is found !

Verify GPS Functionality

cgps -s

Key Observations
The SNR values are all 0.0, indicating that the GPS is not receiving signals from satellites.

Status: NO FIX:
The line Status: NO FIX (6 secs) indicates that the GPS is unable to determine its position. This is a critical issue, as a valid time fix is necessary for NTP synchronization.

Latitude and Longitude:
Both Latitude and Longitude are shown as n/a, further confirming that the GPS has not acquired a position.

SNR (Signal-to-Noise Ratio):
The SNR values are all 0.0, indicating that the GPS is not receiving signals from satellites.

Obviously, the GPS receiver cannot receive any signals from satellites. So I place the GPS antenna directly outside the window. Sometimes, it may take several minutes for the GPS to acquire a fix, especially if it has just been powered on or moved to a new location. I use the “cgps -s” command again after few minutes and the result is positive. I got a 3D FIX !!! The LED on the GPS receiver will blink every second when it successfully receives information from the satellites.

(4) Configure NTP

Open the NTP configuration file:

sudo nano /etc/ntp.conf

Remark some items and add new items. Final items without remark are shown below.

#Drift and Leap Files for accurate timekeeping 
driftfile /var/lib/ntpsec/ntp.drift
leapfile /usr/share/zoneinfo/leap-seconds.list

# Log file configuration
logfile /var/log/ntp.log

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

#specify GPS as the only time server
server 127.127.28.0 minpoll 4 maxpoll 4 prefer
fudge 127.127.28.0 stratum 1

# Use localhost as a backup. It doesn't work
server 127.127.1.0  
fudge 127.127.1.0  stratum 10
restrict 127.0.0.1

Explanation of GPS time server configuration lines:

  1. server 127.127.28.0 minpoll 4 maxpoll 4 prefer:
    • server 127.127.28.0: This specifies the local GPS as the NTP server using the shared memory interface.
    • minpoll 4: This sets the minimum polling interval for the NTP server to 16 seconds (2^4 seconds).
    • maxpoll 4: This sets the maximum polling interval to also 16 seconds.
    • prefer: This indicates that this server should be preferred over others if there are multiple servers configured. It tells NTP to use this server as the primary source of time.
  2. fudge 127.127.28.0 stratum 1:
    • fudge 127.127.28.0: This command allows you to modify the behavior of the specified server.
    • stratum 1: This sets the stratum level of the GPS time source to 1. In NTP, stratum levels indicate the distance from the reference clock (with 1 being the highest quality). Since GPS is considered a very accurate source, it is often set to stratum 1.

In summary, this configuration line tells your NTP server to use the GPS (via 127.127.28.0) as a preferred time source with a polling interval of 16 seconds, and it designates the GPS as a high-quality time source by assigning it a stratum level of 1. This setup is commonly used for accurate timekeeping in systems that rely on GPS.

Restart NTP Service

sudo systemctl restart ntp

 Verify GPS and NTP Functionality

  1. Check GPS Status:
    Run the following command to check if the GPS is receiving signals:
gpsmon
  1. Check NTP Status:
    Verify that the NTP server is synchronized with the GPS:
ntpq -p

Return message:

remote     refid st t when poll reach  delay  offset  jitter
=======================================================================================================
*SHM(0)   .SHM.  1  l   15  16  1      0.000  3.608   0.000
LOCAL(0)   .LOCL 10 l   -   64  0      0.000  0.000   0.000   

Breakdown of the Output

  1. *SHM(0)
    • remote*SHM(0) indicates that the system is using a shared memory time source for synchronization.
    • refid.SHM. shows that this is the shared memory reference.
    • st1 indicates this is a primary time source (very reliable).
    • tl signifies it is a local source.
    • when15 means it last polled 15 seconds ago.
    • poll16 indicates a polling interval of 16 seconds.
    • reach1 shows it has successfully reached the server once.
    • delay0.000 seconds indicates negligible round-trip delay.
    • offset3.608 seconds means the local clock is about 3.608 seconds ahead of the shared memory source.
    • jitter0.000 seconds indicates no variability in the offset.
  2. LOCAL(0)
    • remoteLOCAL(0) refers to the local clock.
    • refid.LOCL. shows it is using the local clock as a reference.
    • st10 indicates a high stratum, meaning it is not considered a reliable time source compared to lower stratum sources.
    • tl signifies it is a local source.
    • when- indicates it hasn’t polled recently or hasn’t been synchronized.
    • poll64 indicates a polling interval of 64 seconds.
    • reach0 shows that it is currently unreachable.
    • delay0.000 seconds indicates no measurable delay.
    • offset0.000 seconds indicates no offset to measure.
    • jitter0.000 seconds indicates no variability.

The *SHM(0) entry indicates that your system is currently synchronized with the shared memory time source, which suggests that the NTP daemon is functioning properly.
The LOCAL(0) entry shows that the local clock is being used but is not currently reachable and has a high stratum of 10.

Unplug the GPS antenna and wait for 7 minutes, type the “ntpq -p” again and return messages becomes

 remote     refid st t  when poll reach  delay   offset  jitter
==============================================================================
 SHM(0)    .SHM.  1  l  205   16    0    0.000   15.675   0.000
 LOCAL(0)  .LOCL. 10 l   44   64    1    0.000    0.000   0.000

Then NTP server will use the local time as a reference, although it may be not accurate.

In case of failure, you can look at the NTP logs to identify any issues

sudo /var/log/ntp.log

Or make sure the gpsd service is started and configured correctly. Check its status:

sudo systemctl status gpsd

(5) Set Up Clients

  • Other devices on your local network can now point to the Raspberry Pi’s IP address as their NTP server.

Posted by

in