
274
|
Chapter 5, Mapping with Gadgets
#61 Build a Map of Local GSM Cells
HACK
Also, you’ll need the Device::Modem module from the CPAN to run this
code. You can obtain it by running
perl -MCPAN -e 'install Device::Modem'
as root on your Linux system.
The Code
Here is the code contained in cellstumbler.pl:
#!/usr/bin/perl
use 5.6.1;
use IO::Socket;
use Device::Modem;
use Getopt::Std;
use strict;
use warnings;
BEGIN { $|++ }; # autoflush STDOUT.
# Read command line options.
my %opt;
getopt('gpr', \%opt);
# Set parameters based on command line options, with defaults.
my $GPSD = $opt{g} || "localhost:2947";
my $GSM_Port = $opt{p} || "/dev/ttyS0";
my $GSM_Baud = $opt{b} || 115_200;
my $Rate = $opt{r} || 1;
# Connect to gpsd.
my $gps = IO::Socket::INET->new($GPSD)
or die "Can't connect to gpsd at $GPSD: $!\n";
# Connect to the cell phone.
my $gsm = Device::Modem->new( port => $GSM_Port );
$gsm->connect( baudrate => $GSM_Baud )
or die "Can't connect to modem on $GSM_Port: $!\n";
# Tell the cell phone we want all the network registration data it has.
$gsm->reset;
$gsm->atsend( "AT+CREG=2\r" );
die "AT+CREG not recognized by modem on $GSM_Port"
unless $gsm->answer eq "OK";
while (1) {
# Tell gpsd we want position, date, and status.
$gps->print("pds\n");
# Parse out the response. If the date is blank, gpsd needs a second to
catch up.
my $location = <$gps>;
my ($lat, $long, $date, $status) =