All,
enclose a script i'm using to set the routers clock based on GPS data from serial port.
#!/bin/sh
# Script to set the time based on GPS clock
# Gerald Peklar - info@dk7xe.de
# settime-gps v1.2 - 20080206
# tested on WRT54GL - Kamikaze7.09 - Deluo GPS
#---------------------------------------------------------------------------------
# set the GPS port and speed
gpsport=/dev/tts/1
gpsspeed=4800
# define the tmp-file for storing the GPS data input stream
gpsdata=/tmp/devtts1
# define the file for storing the current position (maybe useful for other things ;-)
posdata=/tmp/position.gps
# define the limits for the loop (repeating gps data read until LIMIT reched)
var0=0
LIMIT=5
# start flashing white led to indicate that process is running
echo f >/proc/diag/led/ses_white
# set the GPS port to the correct speed
/usr/sbin/stty -F $gpsport $gpsspeed
# execute the whole stuff n times to get a GPS - lock
while [ "$var0" -lt "$LIMIT" ]
do
# wait some time
sleep 3
# grap some data out of the serial port data stream
# and store it in the temporary gpsdata-file
dd if=$gpsport bs=4k count=20 of=$gpsdata
# search for the dataline with the GPRMC data that contais date and time information
gprmcline=$(grep GPRMC $gpsdata)
# check if the GPS receiver has a lock ( receives valid data )
# = character A at position 15 of GPRMC line
data_ok=$(expr substr "$gprmcline" 15 1)
# if GPS data are ok
if [ "$data_ok" = "A" ]; then
# extract the components of the date/time out of the GPRMC sentence
gpsdate_MM=$(expr substr "$gprmcline" 56 2)
gpsdate_DD=$(expr substr "$gprmcline" 54 2)
gpsdate_YY=$(expr substr "$gprmcline" 58 2)
gpstime_hh=$(expr substr "$gprmcline" 8 2)
gpstime_mm=$(expr substr "$gprmcline" 10 2)
gpstime_ss=$(expr substr "$gprmcline" 12 2)
# execute the set date command with the data from GPRMC sentence
date -u -s $gpsdate_MM$gpsdate_DD$gpstime_hh$gpstime_mm"20"$gpsdate_YY"."$gpstime_ss
# write the current position into a file for probably later use
echo $(expr substr "$gprmcline" 17 24) > $posdata
# since valid data are found force the loop to end
var0=$LIMIT;
else
echo "GPS data are not valid - no date update (loop $var0)";
fi
# increments counter by one
var0=$(($var0+1));
done
# remove temporary file to free up space
rm -f $gpsdata
# stop flashing white led to indicate that process is done
echo 0 >/proc/diag/led/ses_white
#
# Reference - position of data in the GPRMC sentence:
# 0 1 2 3 4 5 6
# 123456789012345678901234567890123456789012345678901234567890
# $GPRMC,113556,V,5334.8071,N,00942.0269,E,000.9,141.6,040208,,,N*65
# | time |V - no lock / A - GPS lock | date |
This script is tested on WRT54GL - Kamikaze 7.09 - Deluo serial GPS.
One remark to the serial lines:
Since the serial ports on the routers have only two lines we should disable the hardware handshaking.
On my WRT54GL's i'm doing that with a boot script that sets the correct baud base, enable TTS/1 for 115k2 operation
and finally disables all the hardware handshaking.
place that script in /etc/init.d
#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.orgSTART=15
boot() {
#/usr/sbin/setserial /dev/tts/1 irq 3
/usr/sbin/setserial /dev/tts/0 port 0xB8000300 baud_base 1267285
echo "/dev/tts/0 SET to: port 0xB8000300, baud_base 1267285"
/usr/sbin/setserial /dev/tts/1 port 0xB8000400 baud_base 1267285 irq 3
echo "/dev/tts/1 SET to: port 0xB8000400, baud_base 1267285, irq 3"
/usr/sbin/stty -F /dev/tts/0 115200 -parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
/usr/sbin/stty -F /dev/tts/0 ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff
/usr/sbin/stty -F /dev/tts/0 -iuclc -ixany -imaxbel
/usr/sbin/stty -F /dev/tts/0 -opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0
/usr/sbin/stty -F /dev/tts/0 ff0 -isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop
/usr/sbin/stty -F /dev/tts/0 -echoprt -echoctl -echoke
/usr/sbin/stty -F /dev/tts/1 115200 -parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
/usr/sbin/stty -F /dev/tts/1 ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff
/usr/sbin/stty -F /dev/tts/1 -iuclc -ixany -imaxbel
/usr/sbin/stty -F /dev/tts/1 -opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0
/usr/sbin/stty -F /dev/tts/1 ff0 -isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop
/usr/sbin/stty -F /dev/tts/1 -echoprt -echoctl -echoke
}
Gerald
(Last edited by dk7xe on 6 Feb 2008, 07:06)