#!/usr/bin/perl -w # -*- perl -*- use Getopt::Long; use vars qw($opt_verbose $opt_help); sub initialize { GetOptions("verbose", "help"); if (defined $opt_verbose) { $verbose=1; } else { $verbose=0; } if (defined $opt_help) { print "usage: gpsd [-verbose] [-help]\n"; exit; } $gps = "/dev/gps"; open (LOC, ">>/tmp/location_log") || die "Couldn't open /tmp/location_log: $!\n"; select LOC ; $|=1; select STDOUT; $status = system("stty speed 4800 < $gps"); print "error: couldn't initialize $gps $! $status\n" if $status; $status = system("echo ASTRAL > $gps"); print "error: couldn't send magic word ASTRAL $! $status\n" if $status; print "starting gps daemon\n"; open (GPS, "<$gps"); $time = time; $time = $birds = $hdop = $lat = $latdir = $lon = $londir = $speed = $dir = $date = $mag = $magvar = "" } sub monitor { $oldt = time; while() { @Sentence = split(',', $_); if ($Sentence[0] eq "\$GPGGA" && $Sentence[2] ne '') { $time = $Sentence[1]; $lat = $Sentence[2]/100; $latdir = $Sentence[3]; $lon = $Sentence[4]/100; $londir = $Sentence[5]; $birds = $Sentence[7]; $hdop = $Sentence[8]; if ($birds > 2) { $valid = 1; } else { $valid = 0; } } elsif ($Sentence[0] eq "\$GPRMC" && $Sentence[3] ne '') { $time = $Sentence[1]; $lat = $Sentence[3]/100; $latdir = $Sentence[4]; $lon = $Sentence[5]/100; $londir = $Sentence[6]; $speed = $Sentence[7]; $dir = $Sentence[8]; $date = $Sentence[9]; $mag = $Sentence[10]; $magvar = $Sentence[11]; if ($Sentence[2] eq 'A') { $valid = 1; } else { $valid = 0; } } if (time - $oldt > 9 && $lat ne '') { # convert to decimal degrees # the raw gps output format is in degreesminutes.decimalseconds $lonmin = $latmin = ""; ($latdeg, $latmin, $latsec) = ($1, $2, $3) if $lat =~ m/(\d\d)\.(\d\d)(.*)/; ($londeg, $lonmin, $lonsec) = ($1, $2, $3) if $lon =~ m/(\d\d)\.(\d\d)(.*)/; $latmin .= "." . "$latsec"; $lonmin .= "." . "$lonsec"; $latitude = $latdeg + $latmin/60; $longitude = $londeg + $lonmin/60; $latitude *= -1 if $latdir eq 'S'; $longitude *= -1 if $londir eq 'W'; spew(); $oldt = time; } } } sub spew { $ts = time; print LOC "$ts,$latitude,$longitude,$birds,$hdop,$date,$time,$speed,$dir\n"; } initialize(); monitor();