Raspberry PI – GPS for clock

# Automatic Setting of GPS for clock
# Script Ver 1.0
# www.drbig.co.uk
# This is for Raspberry PI

#Update Raspberry PI First

sudo aptitude update -y
sudo aptitude dist-upgrade -y

#Check Time Zone set correctly

sudo dpkg-reconfigure tzdata

#Lets take backup copy first

sudo cp /boot/cmdline.txt /boot/cmdline_backup.txt

#and now remove the unwanted txt from the file we just backed up

sudo sed -i “s/console=ttyAMA0,115200//g” /boot/cmdline.txt| sed ‘s/OverallStatus//’ | sed ‘s/IfName//’
sudo sed -i “s/kgdboc=ttyAMA0,115200//g” /boot/cmdline.txt| sed ‘s/OverallStatus//’ | sed ‘s/IfName//’

#Next we are going to comment out #T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100 from /etc/inittab

sudo sed -i “/T0:23:respawn/ s/^/#/” /etc/inittab

#Switching The KernelDoing this isn’t particularly hard. However, keep in mind that it will overwrite any special modifications you’ve already done to the kernel, and will break on upgrades to the kernel
#In order to keep things simple, I have a repository with pre-made kernel modules and kernel image. If you’d like to view the source this is based on, click here. [2]
#To grab the pre-compiled kernel and modules:

cd ~
sudo apt-get install git
git clone https://github.com/davidk/adafruit-raspberrypi-linux-pps.git

#Back up and copy the kernel image

cd adafruit-raspberrypi-linux-pps
sudo mv /boot/kernel.img /boot/kernel.img.orig
sudo cp kernel.img /boot

#Move the modules over

sudo mv modules/* /lib/modules

#Finally, add the pps-gpio module to /etc/modules.We run this command in a sub-shell so appending works

sudo sh -c “echo ‘pps-gpio’ >> /etc/modules”

#Now Reboot the PI

sudo reboot

#Automatically Making Links In /etc/udev

sudo touch /etc/udev/rules.d/80-gps-to-ntp.rules
sudo chmod 777 /etc/udev/rules.d/80-gps-to-ntp.rules
sudo echo ‘# Change MODE of ttyAMA0 so it is readable by NTP and provide a symlink to’ >> /etc/udev/rules.d/80-gps-to-ntp.rules
sudo echo ‘# /dev/gps0’ >> /etc/udev/rules.d/80-gps-to-ntp.rules
sudo echo ‘KERNEL==”ttyAMA0″, SUBSYSTEM==”tty”, DRIVER==””, SYMLINK+=”gps0″, MODE=”0666″‘ >> /etc/udev/rules.d/80-gps-to-ntp.rules
sudo echo ‘# Symlink /dev/pps0 to /dev/gpspps0’ >> /etc/udev/rules.d/80-gps-to-ntp.rules
sudo echo ‘KERNEL==”pps0″, SUBSYSTEM==”pps”, DRIVER==””, SYMLINK+=”gpspps0″, MODE=”0666″‘ >> /etc/udev/rules.d/80-gps-to-ntp.rules
sudo chmod 644 /etc/udev/rules.d/80-gps-to-ntp.rules

#Installing and Configuring NTPTo install NTP, we abuse the default install a bit since the repository version doesn’t really pick up on 1PPS. It will also take some time to do all of this since we’re compiling from scratch.
# Latest Version at time of writing this install script was 4.2.7p431 March 5th 2014

cd ~
sudo /etc/init.d/ntp stop
sudo apt-get -y update
sudo apt-get -y install libcap-dev
sudo apt-get -y install pps-tools
mkdir ntp
cd ntp
wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-dev/ntp-dev-4.2.7p319.tar.gz
tar -xvf ntp-dev-4.2.7p319.tar.gz
cd ntp-dev-4.2.7p319
./configure –prefix=/usr –enable-all-clocks –enable-parse-clocks \
–enable-SHM –enable-debugging –sysconfdir=/var/lib/ntp –with-sntp=no \
–with-lineeditlibs=edit –without-ntpsnmpd –disable-local-libopts \
–disable-dependency-tracking && make
sudo make install

#We weill now edit /etc/init.d/ntp and change the DAEMON line to reflec the changes

sudo cp /etc/ntp.conf /etc/ntp-original.conf
sudo sed -i “s@DAEMON=/usr/sbin/ntpd@DAEMON=/usr/bin/ntpd@g” /etc/init.d/ntp

#Once you have been dropped back to the terminal, edit /etc/ntp.conf and add the following (be sure to change the broadcast line if it differs for you)

sudo sed -i “s@restrict -4 default kod notrap nomodify nopeer noquery@#restrict -4 default kod notrap nomodify nopeer noquery@g” /etc/ntp.conf
sudo sed -i ‘/server 3.debian.pool.ntp.org iburst/a pool uk.pool.ntp.org iburst’ /etc/ntp.conf
sudo sed -i “s@server 0.debian.pool.ntp.org iburst@# http://www.eecis.udel.edu/~mills/ntp/html/drivers/driver20.html explains these settings@g” /etc/ntp.conf
sudo sed -i “s@server 1.debian.pool.ntp.org iburst@#@g” /etc/ntp.conf
sudo sed -i “s@server 2.debian.pool.ntp.org iburst@server mode 17 minpoll 3 maxpoll 4 iburst true prefer@g” /etc/ntp.conf
sudo sed -i “s@server 3.debian.pool.ntp.org iburst@fudge 127.1270.28.0 time1 0.000 refid SHM stratum 15@g” /etc/ntp.conf

#The next steps are to install the gpsd software, and start the gpsd service pointing to the device name just discovered:

sudo apt-get -y install gpsd gpsd-clients python-gps

sudo gpsd /dev/ttyAMA0 -n -F /var/run/gpsd.sock

#At this point, you should be able to see a text-mode output from your GPS receiver by running the command “cgps -s”, something like the following.

cgps -s

#Optional step to check for version and basic function to see if it is working

sudo /etc/init.d/ntp start

ntpq -crv -pn

#We are done so Reboot

sudo reboot

# Cleanup

#Remove NTP Compile directory

rm -r ntp

Author: admin