Broadband-Hamnet™ Forum :: Applications
Welcome Guest   [Register]  [Login]
 Subject :Automatice Clock Update without Internet.. 2013-05-12- 10:50:02 
Joined: 2013-04-12- 14:16:38
Posts: 7
Location: Henderson, Texas

Ok, so far we are working with a Mesh that is not connected to the Internet. However, I have recently installed a Pharos 360 GPS unit (inspired by K5KTF's article under the Applications link). 

I then wrote a simple perl program and a cron job script to grab the date and time data from the gps and set the node's date/time with the date command. So far it is only accurate to within about a minute, but that's a lot better that "Jan 1 2000" Now, I'm thinking about setting up a local time server on the same node to pass the date/time to other nodes. 

There's no pride in authorship here. I've been out of the programming world for nearly 20 years, so this was a 're-learning' experience for me. I'm sure there are probably more elegant ways to accomplish this. I'm open to improvements. I'm sure there are some regular expression ways to reformat, I'm just VERY rusty on my regular expressions! 

My first version was writing the time data to a file and then reading the file. Now, I've combined it and avoided the step involving the file completely.

Here's how I did it:

GPS unit is directly soldered to the second serial port with three wires. Once initiated at boot-up, it sends a continual stream of data at 4800 baud to /dev/tts/1. 

I set up a cron job to run every two hours to capture the GPRMC line of data from the GPS and pipe it to a perl program (fmtgpsdate) which pulls out the date and time date and re-formats it to something the date command can understand. Although the documentation at OpenWRT shows thre or four different acceptable formats, I could only get one (the one shown below) to actually work. All others returned an "Invalid Date" error from the date command.

crontab -e

Added the following line:

15 */2 * * * date -s `head /dev/tts/1|grep GPRMC | perl /www/cgi-bin/fmtgpsdate`

to tell the system to run the command at 15 minutes past the hour every two hours. Note: the argument for the date command is surrounded in backticks (under the ~ on my keyboard), not apostrophes. It tells the shell to execute the command.

I saved the following perl program in /www/cgi-bin/ directory, although it should probably be in /etc/sbin/ or similar. It's called fmtgpsdate.


$debug = 0;

use perlfunc;

# Read in  GPRMC Data from previous command

($gps_type, $gps_time, $gps_state, $gps_lat, $gps_ns, $gps_lon, $gps_ew, $gps_speed, $gps_track, $gps_date, $gps_rest) = split(',',<STDIN>);

# Format the data to acceptable form

print "20" . substr($gps_date,4,2) . "." . substr($gps_date,2,2) . "." . substr($gps_date,0,2) . "-" . substr($gps_time,0,2) . ":" . substr($gps_time,2,2) . ":45";

I then issued:

chmod +x fmtgpsdate 

to make it executable.

I wish I could do the cron job at boot-up but it takes some time for the gps to acquire the data. Again, its not accurate to the second. The GPRMC data is only accurate to the minute (no seconds are provided). So, I added 45 seconds to the minute which seems to get me fairly close to the actual time.

Have fun- 73s

W5CWT, David

IP Logged
Last Edited On: 2013-05-12- 14:43:31 By w5cwt for the Reason correct call sign
 Subject :Re:Automatice Clock Update without Internet.. 2013-05-12- 16:13:45 
Joined: 2012-02-13- 18:18:04
Posts: 126
Location: Albuquerque, NM

That is EXCELLENT!!  Thanks!  I always wondered about doing that.

IP Logged
 Subject :Re:Automatice Clock Update without Internet.. 2013-05-21- 21:37:41 
Joined: 2010-10-27- 00:47:17
Posts: 144
Location: Van Alstyne, TX


   Not sure what you mean that 'GPRMC data is only accurate to the minute (no seconds are provided)' because the standard is to provide the time to 1/100th of a second. At worst this might be off by a good portion of second which is basically "transmit time" via RS-232 at 4800-bps.

For example:

    $GPRMC,225446.33,A....(rest of sentence deleted)

This translates to 22:54:46.33 UTC ( Your script should add a check of the gps_state and look for 'A' or 'D' to see if the GPS has a valid fix. If the state is either letter (probably only need 'A' unless you have the GPS config'd for a differential GPS input) then the time in the time field is valid and you can use it to set the system's time.

Let me know if I can help. I was working on one of the earliest GPS's receivers back in 1982-83 at Texas Instruments and the USAF only had 6 sat's in orbit to use and only four them worked reliably. :)


IP Logged
Last Edited On: 2013-05-21- 21:41:02 By ae5ae for the Reason
Page # 

Powered by ccBoard