diff --git a/gps_time/LICENSE b/gps_time/LICENSE new file mode 100644 index 0000000..b5a7522 --- /dev/null +++ b/gps_time/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2021, LHardwick-git +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/gps_time/README.md b/gps_time/README.md new file mode 100644 index 0000000..048de22 --- /dev/null +++ b/gps_time/README.md @@ -0,0 +1,51 @@ +# gps-time + +Copyright (c) 2021 LHardwick-git +Licensed under the BSD 3-Clause license. +See LICENSE file in the project root for full license information. + +USE CASE +When running Venus OS on a Raspberry Pi there is no real time clock. +As my application is on a boat there may be no internet connection. +In this case the local device time is not set at startup so logs are created with the wrong time stamps. + +SOLUTION + +This is a shell script that runs at startup and reads the time from the data stream from a USB GPS device. If time information is found in the data stream it is read and used to set the device local time. As a result Venus OS logs are stamped with the correct time. + +If the USB GPS device is not connected or not producing a data stream with the right data +the script will exit. The script is run in the background to ensure that it does not block other +parts of the Venus OS initialisation. + +REQUIREMENTS / TEST ENVIRONMENT +This has been tested in the following environment. +It should work in other cases. + + Venus OS 2.6 + RPI 3B+ + USB GPS device with NMEA data stream output. + +INSTALLATION + +Place the gps-time.sh file in directory /data/venus-rpi-setup/ +Make sure it has execute set +>chmod a+x /data/venus-rpi-setup/gps-time.sh + +Place a copy of rc.local in directory /data +[ Or add the lines from from rc.local to you own rc.local if you need to + run other things as well ] +Make sure rc.local has execute access +>chmod a+x /data/rc.local + +If you choose to out gps-time.sh somewhere else, +then change rc.local to refer to the right place. + +IN USE +You can check the results of the script running in the log file. +/var/volatile/log/gps-time + +If all goes well the time on the Venus OS console will be correct at start up. + +Hope it works for you + + diff --git a/gps_time/Scripts/gps-time.sh b/gps_time/Scripts/gps-time.sh new file mode 100644 index 0000000..99c2be3 --- /dev/null +++ b/gps_time/Scripts/gps-time.sh @@ -0,0 +1,53 @@ +#!/bin/bash +# Copyright (c) 2021 LHardwick-git +# Licensed under the BSD 3-Clause license. +# See LICENSE file in the project root for full license information. +# Read GPS NMEA data from GPS device and use it to set the device (RPi) local time. +# +N=0; +FILE=/run/serial-starter/gps/ttyACM0 +echo "Waiting for GPS data" +while [ ! -e $FILE ] && [ $N -lt 50 ]; +do + sleep 1; + ((N++)) + echo -n "." +done; +sleep 5; + +if [ $N -ge 49 ]; then + echo " GPS data not available is the device plugged in" + exit 0; +fi + +echo +echo "Device attached waiting for data" + +input=$FILE +N=0 + +# check if GPS has FIX +LINE1='^\$..GGA,[0-9\.]+,[0-9\.]+,[NS],[0-9\.]+,[EW],([0-2])' +# Catch lines with date and time info +LINE2='^\$GPRMC' + +FIX="" + +# extract date and time from GPS packet + +function extract { +[[ $1 =~ ^\$GPRMC,([0-9]{4})([0-9]{2})\.[0-9]{2},.,[0-9\.]+,[NS],[0-9\.]+,[EW],[0-9\.]*,[0-9\.]*,([0-9]{2})([0-9]{2})([0-9]{2}), ]] + COMMAND="20${BASH_REMATCH[5]}${BASH_REMATCH[4]}${BASH_REMATCH[3]}${BASH_REMATCH[1]}.${BASH_REMATCH[2]}" + echo command is $COMMAND + date -s $COMMAND +exit 0 +} + +while IFS= read -r line && [ $N -lt 2000 ] +do +# echo "$line" + [[ $line =~ $LINE1 ]] && FIX=${BASH_REMATCH[1]} && echo "GPS device has a fix after $N lines" + [[ $line =~ $LINE2 ]] && [[ $FIX -eq 1 ]] && extract $line + ((N++)) +done < "$input" +echo "Timed out no date/time information in 2000 lines from GPS receiver" diff --git a/gps_time/Scripts/rc.local b/gps_time/Scripts/rc.local new file mode 100644 index 0000000..25c5885 --- /dev/null +++ b/gps_time/Scripts/rc.local @@ -0,0 +1,7 @@ +# Add these lines to rc.local to run gps-time at startup +# If you have nothing else to run - this is a perfectly good rc.local file + +if test -f /data/venus-rpi-setup/gps.sh; then + /data/venus-rpi-setup/gps-time.sh &>>/var/volatile/log/gps-time & +fi +exit 0