NTP+GPS+RaspberryPi+Pidora

I’ve finally put one of my Raspberry Pis to good use. I have two of them that have been sitting around taking up space collecting dust.
At the office most of the devices are set to use the primary server as a time source.
This is good in that most of the network has the same time as the primary server. However that server is starting to show its age.
Also from a compliance and security point of view that server has many issues since it handles the user logons, file sharing, email, internal web server, and a few other services.
So to decrease the load on that server, increase the redundancy in the time sync service, and move to a “more secure” setup, I decided to get my Raspberry Pis into play.
Using the RPi as a network time server is a fairly easy process and has been documented several times.
I am planning on deploying three NTP servers at the office all of which will be using GPS as the clock source.
The office is a 70,000 square foot warehouse and I do not have any restrictions with roof access for the antennae.
The first two servers are going to be using my Raspberry Pis. The third will most likely be using a Beagle Bone Black.
I will be calling these servers: tick, tock, and littleben. (I know, very original names!!)
Tick and Tock will be the Raspberry Pis.
LittleBen will add more functionality than just a time server. He’ll have an LED display and audio.

Well that’s more than enough background and future intentions, on to the work…

The hardware

When I can I purchase via a Canadian reseller. In this case I picked up the RPis at Sayal in Toronto during one of my visits to their store. They are close to the office so I can make my way to work, say hi, and then go shopping for more toys!! I ended up getting various items from Spikenzie Labs in Montreal and R.P. Electronics in Vancouver.
What I bought:

  • Adafruit Ultimate GPS Breakout
  • 3V Magnetic Mount GPS Antenna with SMA connector
  • SMA to u.FL cable
  • Adafruit small-size perma-proto Raspberry Pi breadboard
  • Adafruit Pi Cobbler breakout + cable (assembled)

You have to be carefull with the SMA cable as the GPS receiver links to a reverse polarity cable on the Adafruit site as well as the SpikenzieLabs site. The cable on the Adafruit site does say to watch what you buy and gives a link to a normal cable. SpikenzieLabs did not have this normal cable on their site when I was shopping, they only had the RP-SMA version which is not compatible with the GPS antenna. After shopping around a few of the major distributor sites I finally found the required cable at R.P. Electronics.

The software

Most of the “tutorials” I found online were using Raspian. I had decided on using Pidora 2014 as I am familiar with Fedora having used it for many years. This means that much of the instructions are “guidance” only but I worked it out in the end.

At first I have issues getting Pidora to boot on my Pi. Turns out I had to disable everything in the config.txt file so that the HDMI monitor would be used. The Pi did boot up but was not using the display at all. The Pidora 2014 install boots up nicely after that. I left the install with a GUI desktop just to see what it was like. Slow comes to mind but it does work. For actual deployment I’ll switch this to text mode.

After running the updates I removed chrony as I wanted to use NTP.
I installed ntp, ntp-doc, and made sure ntpdate was installed.
Since the Pi does not have a real time clock, the system defaults to December 1969 which is way too far out of sync for the GPS and NTP to update automatically. The Pidora folks must of realized this and the ntpdate service is available via systemd as a one time service. So I hacked the gpsd service definition removing the chrony references and adding a reference to ntpdate (I added “After ntpdate.service”). I enabled the ntpdate service so it runs at startup, this way the Pi will fetch the correct time from the servers defined for ntp before running the gps service.

I edited the cmdline.txt, config.txt, and inittab files as per the tutorials thus freeing up the serial port.
I had to edit the “/etc/sysconfig/gpsd” file setting the OPTIONS to “-n /dev/ttyAMA0” to enable gpsd to use the serial port.
I used “cgps -s” to test the gps system once the service was started.

I had to edit the “/etc/sysconfig/ntpd” file setting the OPTIONS entry to get things to run as required.
I added the server in the “/etc/ntp.conf” file to enable NTP to use the GPS service.
Once that was working I found I had to recompile the kernel to get the kernel mode PPS to work so I decided to go the user-land route instead.
So I picked up the “rpi_gpio_ntp” mentioned in the tutorials and figured out what I needed as parameters.
I then created an entry in “/lib/systemd/system/” called “ppsd.service” using the required parameters.
I edited the ntp.conf to add the PPS source.

I made sure that ntpdate, ntp, gpsd, and ppsd services run at start up.

Now I’ve got a working Stratum 1 NTP Time Server for under $200.

I need to duplicate this on the second Pi which should just be a matter of connecting the hardware and cloning the SD card.

At 5W max power these servers will not stress the UPS very much.
They even boot up in just a few seconds compared to the generic Intel servers.
Using PoE I can cycle the power easily, and remotely.

References

http://www.satsignal.eu/ntp/Raspberry-Pi-NTP.html
http://mythopoeic.org/pi-ntp/
http://open.konspyre.org/blog/2012/10/18/raspberry-pi-time-server/
http://transmission.xmission.com/2014/05/28/building-a-stratum-1-ntp-server-with-a-raspberry-pi
https://cs.uwaterloo.ca/twiki/view/CF/Stratum1NTPRpi
http://vanheusden.com/time/rpi_gpio_ntp/

Next Steps:

Assemble everything into a nice case, add a PoE splitter, reduce the local logging, and deploy at the office.

Files

ppsd.service

[Unit]
Description=PPS (Pulse Per Second) Daemon
Requires=gpsd.service
After=gpsd.service

[Service]
ExecStart=/usr/local/bin/rpi_gpio_ntp -N 1 -g 18 -f

[Install]
WantedBy=multi-user.target

gpsd.service

[Unit]
Description=GPS (Global Positioning System) Daemon
Requires=gpsd.socket
After=ntpdate.service

[Service]
EnvironmentFile=-/etc/sysconfig/gpsd
ExecStart=/usr/sbin/gpsd -N $OPTIONS

[Install]
WantedBy=multi-user.target
Also=gpsd.socket

ntpd.service

[Unit]
Description=Network Time Service
After=syslog.target ntpdate.service sntp.service gpsd.service

[Service]
Type=forking
EnvironmentFile=-/etc/sysconfig/ntpd
ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS
PrivateTmp=true

[Install]
WantedBy=multi-user.target

/etc/sysconfig/gpsd

# Options for gpsd, including serial devices
OPTIONS="-n /dev/ttyAMA0"
# Set to 'true' to add USB devices automatically via udev
USBAUTO="true"

/etc/sysconfig/ntpd

# Command line options for ntpd
OPTIONS="-g"

/etc/ntp.conf

# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

server 127.127.28.0 minpoll 4 maxpoll 4 prefer
fudge 127.127.28.0 time1 0.475 refid GPS

server 127.127.28.1 minpoll 4 prefer
fudge 127.127.28.1 refid UPPS

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.fedora.pool.ntp.org iburst
#server 1.fedora.pool.ntp.org iburst
#server 2.fedora.pool.ntp.org iburst
#server 3.fedora.pool.ntp.org iburst

server tick.utoronto.ca
server tock.utoronto.ca
server time.nrc.ca
server time.chu.nrc.ca

#broadcast 192.168.1.255 autokey        # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 autokey            # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.
#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey 8

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats

/boot/cmdline.txt

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 ro rootfstype=ext4 rootwait raid=noautodetect quiet

Commands

Enable Services

systemctl enable ppsd.service
systemctl enable gpsd.service
systemctl enable ntpd.service
systemctl enable ntpdate.service
systemctl enable sshd.service

Packages

yum remove chrony
yum install ntp ntp-doc ntpdate

Tune the Adafruit GPS module

echo -e '$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n' > /dev/ttyAMA0
Bookmark the permalink.

Comments are closed.