NTP-Server with Raspberry Pi and Trimble Resolution SMT
Inhaltsverzeichnis
1 Scope
Basically this project is the same as the predecessor: NTP-Server with Raspberry Pi and Sure Electronics GPS Eval board
However I am using another timing receiver board here. It's one that I also purchased a long time ago but it would be shame throwing it away. Particularly given that I still have a large stockpile of Raspberry Pi boards.
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/
- https://support.ntp.org/bin/view/Sandbox/HowtoPpsOnRaspberryPi
- https://learn.adafruit.com/adafruit-ultimate-gps-on-the-raspberry-pi/setting-everything-up
- https://raspberry.tips/raspberrypi-tutorials/gps-modul-mit-dem-raspberry-pi-ortung-und-navigation/
3 On Problems I encountered
CMOS to Raspberry Pi:
- http://www.mikrocontroller.net/articles/Pegelwandler
- http://tansi.info/rp/interfacing5v.html
- http://www.allaboutcircuits.com/textbook/digital/chpt-3/logic-signal-voltage-levels/
- http://www.interfacebus.com/voltage_threshold.html
4 Implementation
4.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 :-)
As before, I connected the PPS with GPIO pin 18. In order to make this work, I edited the file /boot.config.txt...
- I 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 we're running into a difference to the previous project, because I am not using an external USB source, but I decided to connect the native Raspberry's UART.
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@ntp2:~# 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 1462395271.608254834, sequence: 4022 - clear 0.000000000, sequence: 0 source 0 - assert 1462395272.608254999, sequence: 4023 - clear 0.000000000, sequence: 0 source 0 - assert 1462395273.608254163, sequence: 4024 - clear 0.000000000, sequence: 0 source 0 - assert 1462395274.608255327, sequence: 4025 - clear 0.000000000, sequence: 0 source 0 - assert 1462395275.608373487, sequence: 4026 - clear 0.000000000, sequence: 0 ^C root@ntp2:~#
If you get one of these lines every second, this means that your kernel is now disciplined by a PPS. It reacts to the pulse coming in, it recognizes the pulse properly. Congratulations! Note that it does not mean that your ntpd is using the PPS pulse! It just means that you are receiving a pulse by the kernel.
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!
4.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 # these are the lines for the SURE Electronics board. #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 # HERE we will insert the configuration for the Trimble receiver. #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 service 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/
- Since the recent minibian is based on jessie rather than on wheezy:
sh -c 'echo "deb-src http://mirrordirector.raspbian.org/raspbian/ jessie main contrib non-free rpi" >> /etc/apt/sources.list' apt-get update
- Get the build dependencies and the source code of the package:
cd ~ apt-get -y build-dep ntp apt-get -y source ntp
- The source code was stored in my home directory. In the moment and for me it is the version ntp-4.2.6.p5+dfsg. Note that it could be different for you.
cd ntp-4.2.6.p5+dfsg/debian/
- Edit the rules file and add the missing support for PPS/ATOM. Search for configure and append a line at the end of the statement. Don't forget the \ at the end of the previous line.
./configure CFLAGS='$(CFLAGS)' CPPFLAGS='$(CPPFLAGS)' LDFLAGS='$(LDFLAGS)' \ --prefix=/usr \ --enable-all-clocks --enable-parse-clocks --enable-SHM \ --disable-debugging --sysconfdir=/var/lib/ntp \ --with-sntp=no \ --with-lineeditlibs=edit \ --without-ntpsnmpd \ --disable-local-libopts \ --enable-ntp-signd \ --disable-dependency-tracking \ --with-openssl-libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \ --enable-ATOM
- Now add the changelog file so that it reflects the change:
ntp (1:4.2.6.p5+dfsg-7+deb8u1) jessie-security; urgency=medium
- into:
ntp (1:4.2.6.p5+dfsg-8+deb8u1) jessie-security; urgency=medium
- Now 'cd' into the package directory and build the package.
cd .. sudo dpkg-buildpackage -b
- It will take a while now. In my case three deb files were created:
root@ntp2:~# ls -ltr *deb -rw-r--r-- 1 root root 1030546 May 4 23:14 ntp-doc_4.2.6.p5+dfsg-8+deb8u1_all.deb -rw-r--r-- 1 root root 350312 May 4 23:17 ntp_4.2.6.p5+dfsg-8+deb8u1_armhf.deb -rw-r--r-- 1 root root 68844 May 4 23:17 ntpdate_4.2.6.p5+dfsg-8+deb8u1_armhf.deb root@ntp2:~#
- Now restart the ntp server with the command service ntp restart and after ~ 1 minute it should give you this output using ntpq -p:
root@ntp2:~# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== PPS(0) .PPS. 0 l 3 16 377 0.000 360.496 2.939 *lite.computer .PPS. 1 u 616 1024 1 50.043 4.338 72.743 +public.streikt. 192.53.103.108 2 u 615 1024 1 23.819 1.246 0.965 -muenchen.klaus- 129.70.132.34 3 u 614 1024 1 29.009 9.956 0.614 +fry.celeborn.de 192.53.103.108 2 u 613 1024 1 23.976 2.342 0.982 root@ntp2:~#
- The offset of our PPS source is gigantic and there is some considerable jitter. It is because I still have no antenna and the SMT cannot have a fix under any circumstances. But though, we see that the signals are coming in.
- For now, we're still missing a GPS signal...
- Create a symbolic link. We're going to use the driver Nr. 29:
root@ntp2:~# ln -s /dev/ttyAMA0 /dev/palisade0
- For further info check here:
- Edit the file /etc/ntp.conf and edit the following lines:
server 127.127.29.0 minpoll 4 prefer fudge 127.127.20.0 time2 0.1 refid GPS stratum 1
- Note: still uncalibrated, and still without antenna...
- Now the ntpq -p command produces this:
root@ntp2:~# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== PPS(0) .PPS. 0 l - 16 0 0.000 0.000 0.000 GPS_PALISADE(0) .GPS. 0 l - 16 0 0.000 0.000 0.000 wotan.tuxli.ch 235.106.237.243 3 u - 1024 1 24.452 -44.328 0.001 net1.uni-paderb .INIT. 16 u - 1024 0 0.000 0.000 0.001 ns3.customer-re .INIT. 16 u - 1024 0 0.000 0.000 0.001 web1.sys.ccs-ba .INIT. 16 u - 1024 0 0.000 0.000 0.001 root@ntp2:~#
- If you wonder about the 377 reach value, then check this: http://www.linuxjournal.com/article/6812
4.3 System considerations concerning DHCP
In case you retrieve your IP configuration via DHCP, there is a default statement to also retrieve the according NTP server config: You will want to exclude that!
Edit the file /etc/dhcp/dhclient.conf and remove the string ntp-servers from the line behind the request keyword.
Then remove the file /var/lib/ntp/ntp.conf.dhcp.