NTP-Server with Raspberry Pi and Sure Electronics GPS Eval board
Inhaltsverzeichnis
1 Scope
It was already a few years ago when I bought a Sure Electronics GPS timing receiver. Though, there was never enough time to complete and deploy it. I have done it now.
The Sure receiver is nice because it has 3,3V IO pins, and this matches perfectly with a Raspberry Pi. So it's a good point using it.
Obviously many people have problems with the NTPD that comes with the recent raspian distributions: ntp 4.2.6p5. It does not support PPS...
It is rather easy to make the kernel PPS aware, but ntp still is not capable of using it. I needed to recompile the ntp package so that I was able to use it. The symptom therefore is:
- The tool ppstest does report the reception of a pulse, but even if you configure NTP correctly, it won't give you an output with PPS using the command ntpq -p.
2 References
- http://www.satsignal.eu/ntp/Sure-GPS.htm Here the board is described very well
- http://www.satsignal.eu/ntp/Raspberry-Pi-NTP.html
- https://store.uputronics.com/index.php?route=product/product&path=60_64&product_id=81
- http://rdlazaro.info/compu-Raspberry_Pi-RPi-stratum0.html
- http://mythopoeic.org/pi-ntp/
- https://raspberry.tips/raspberrypi-tutorials/raspberry-pi-uhrzeit-ueber-gps-beziehen-zeitserver/
3 Implementation
3.1 OS Installation and Preparation
First of all I decided to use a minibian image:
https://sourceforge.net/projects/minibian/files/2016-03-12-jessie-minibian.tar.gz
because I don't want to create a really big thing. I just want to have an NTP server.
- Unpack, write it to the SD card, as described so often everywhere on almost every RPI page.
- Use gparted to resize the root partition to the rest of your SD card. We should not waste space.
- Log in using uid root pwd raspberry (default). Note that opposed to Raspian, there is no user "pi".
- apt-get update; apt-get upgrade and update the minibian image.
- Give it a nice hostname, editing the file /etc/hostname and reboot.
- Edit /boot/config.txt - Add dtoverlay=pps-gpio,gpiopin=18 on a new line. - Connect the PPS signal of the Sure board to this pin. Don't get the pin wrong :-)
- I also added another line to the boot'config.txt file: dtparam=i2c_arm=on because I have included an I2C sensor that gives me some more values.
Here is why: http://stackoverflow.com/questions/32021924/raspberry-pi-2-cannot-enable-dev-i2c-0
- Save the file...
- Edit the file /etc/modules to contain the following lines. Just paste them below what is there already:
i2c-bcm2708 i2c_dev pps-gpio
- so to ensure that these modules will be loaded.
- Install the pps-tools:
apt-get install pps-tools
If you are using a BMP085 or a BMP180 sensor to grab some data as I do, then do this:
- Install the according i2c devices:
apt-get update apt-get install i2c-tools # I2C-Toolkit for commandline apt-get install python-smbus # Python library for I2C apt-get install libi2c-dev # library for C apt-get install git # need this later...
- Note here (German): http://www.netzmafia.de/skripten/hardware/RasPi/RasPi_I2C.html
- Configure the timezone using the command dpkg-reconfigure tzdata
Now reboot. A device will be created during system startup: /dev/pps0 Check for it to be created. Another device will be created for the I2C stuff, see below.
If you connected the PPS, the following command will produce results like here:
root@ntp1:/etc# ppstest /dev/pps0 trying PPS source "/dev/pps0" found PPS source "/dev/pps0" ok, found 1 source(s), now start fetching data... source 0 - assert 1460368282.999995290, sequence: 43451 - clear 0.000000000, sequence: 0 source 0 - assert 1460368284.000007548, sequence: 43452 - clear 0.000000000, sequence: 0 source 0 - assert 1460368284.999994807, sequence: 43453 - clear 0.000000000, sequence: 0 source 0 - assert 1460368285.999995063, sequence: 43454 - clear 0.000000000, sequence: 0
If you get one of these lines every seconf, this means that your kernel is now disciplined by a PPS. It reacts to the pulse coming in, it recognizes the pulse properly. Congratulations!
Now let's check for the I2C stuff: The device /dev/i2c-1 should have been created. If you also use the BMP sensor, you should find it as '77'. Once you got the sensor connected as described in the manuals, detect it:
i2cdetect -y 1
and it should show up as "77":
0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- 77
Great!
Then you can proceed to download Lady Ada's code examples:
git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
and then try it out:
cd Adafruit-Raspberry-Pi-Python-Code/Adafruit_BMP085 ./Adafruit_BMP085_example.py
Temperature: 25.29 C Pressure: 974.22 hPa Altitude: 530
Now let's go back for the NTP application.
3.2 NTP configuration
The default NTP package is not sufficient, as I wrote above. It does not support reading the PPS from the kernel. It is quite disappointing if you did everything right and you're still not successful.
Though I recommend installing it first because we can prepare it nevertheless, using the command apt-get install ntp
Then edit the config file /etc/ntp.conf and make it look like this. Maybe it's agood idea to copy it to another filename and to have a reference: cp /etc/ntp.conf /etc/ntp.conf_orig. Now edit the /etc/ntp.conf: root@ntp1:/etc# cat /etc/ntp.conf
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help driftfile /var/lib/ntp/ntp.drift # Enable this if you want statistics to be logged. statsdir /var/log/ntpstats/ 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 filegen sysstats file sysstats type day link enable tos minsane 3 tos orphan 10 tos mindist 0.4 server 127.127.22.0 minpoll 4 prefer fudge 127.127.22.0 refid PPS flag2 0 flag3 1 server 127.127.20.0 minpoll 4 mode 18 prefer # NMEA serial port, 16 = 9600 baud, 2 = $GPGGA fudge 127.127.20.0 time2 0.413 flag1 0 refid GPS stratum 1 flag3 1 #server 127.127.1.0 #fudge 127.127.1.0 stratum 10 pool de.pool.ntp.org minpoll 10 iburst # Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for # details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions> # might also be helpful. # # Note that "restrict" applies to both servers and clients, so a configuration # that might be intended to block requests from certain clients could also end # up blocking replies from your own upstream servers. # By default, exchange time with everybody, but don't allow configuration. restrict -4 default kod notrap nomodify nopeer noquery restrict -6 default kod notrap nomodify nopeer noquery restrict 172.16.0.0 mask 255.240.0.0 restrict 192.168.0.0 mask 255.255.0.0 # Local users may interrogate the ntp server more closely. restrict 127.0.0.1 restrict ::1
Since NTP is frequently writing to the SD card, I did a trick. The creation of the mount point /var/log/ntpstats will be done during package installation.
- Stop the daemon with servcie ntp stop
- Now delete the contents of this directory: rm /var/log/ntpstats/*
- Now add the following line to the file /etc/fstab and create a RAM disk
tmpfs /var/log/ntpstats tmpfs nodev,nosuid,size=10M 0 0
- It will cost 10 of your 512 MB RAM, but you didn't plan a high performance compute cluster anyway :-) If you want, you can easily add more. Occasional reboots will keep it empty anyway.
- Note that the process ntpd just consumes about 5 MBytes... So 512 is almost a waste of RAM. :-)
- Reboot.
Now let's check a couple of things:
- mount will show something like this:
tmpfs on /var/log/ntpstats type tmpfs (rw,nosuid,nodev,relatime,size=10240k)
- ntpq -p will show something like this:
root@ntp1:/etc# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *GPS_NMEA(0) .GPS. 1 l 7 16 377 0.000 1.300 11.017 -test.danzuck.ch 162.23.41.56 2 u 712 1024 377 20.054 -0.302 1.833 +stratum2-3.NTP. 129.70.130.70 2 u 288 1024 377 31.598 2.897 0.542 +static.5-9-80-1 213.239.239.165 3 u 448 1024 377 21.270 1.829 0.854 -ns2.bvc-cloud.d 109.75.188.245 3 u 212 1024 377 23.095 0.746 0.502
- Note that there is probably no line containing anything about PPS.
- Though this output already tells us that our NTP server is keeping a more or less precise time. Good enough for your alarm clock in the morning for sure :-)
- Now let's add PPS support. Here is a good reference page: https://raspberry.tips/raspberrypi-tutorials/raspberry-pi-uhrzeit-ueber-gps-beziehen-zeitserver/