Caller ID

Monitor calls on your phone line and report via pop-up messages and web logs.


Click here to check out other projects.

Click to search the Acacia site
Click to search using Google

This project shows you how to capture, log and process telephone calls. Your Linux system will automatically know the caller ID but not interupt or answer the telephone. Each time a phone call is received, the Caller ID information is passed from the exchange to your phone between the second and third rings. When the phone rings, the sample code provided receives this information and publishes it to a web server and sends "net send" popup messages to Windows 2000 PCs on the LAN. It looks up a phone book to determine the name of the person calling. you can edit this phonebook, adding names to the numbers that accumulate there. Just send a HUP signal to the daemon to re-read the phone book.

I hope that this project and the source code provided will help get you started on some projects of your own.

Hardware

Software

Ideas

Notes

Code

Application: /bin/watch_calls.pl

#!/usr/bin/perl
#------------------------------------------------------
# Caller ID logging and annunciation
#
# Written 26/05/2003 GMC
#------------------------------------------------------

#------------------------------------------------------
# Turn off buffering
#------------------------------------------------------
$|=1;

#------------------------------------------------------
# Open and read the phone book into memory
#------------------------------------------------------
&Read_Phone_Book;

#------------------------------------------------------
# Trap kill signals
#------------------------------------------------------
$SIG{HUP} = sub 
{
	print "HUP received.  Re-reading phone book.\n";
	&Read_Phone_Book;
};
$SIG{KILL} = sub 
{
	print "Killed.  Saving phone book and Exiting.\n";
	&Save_Phone_Book;
	exit;
};
$SIG{INT} = sub 
{
	print "Control-C pressed.  Saving phone book and Exiting.\n";
	&Save_Phone_Book;
	exit;
};

#------------------------------------------------------
# Open serial port A for I/O
#------------------------------------------------------
use IPC::Open2;

$pid = open2(\*TTYIN, \*TTYOUT, "cu -l ttyS0") || die "Failed to open modem for output\n";

#------------------------------------------------------
# Issue startup message
#------------------------------------------------------
print "Enabling caller ID\n";

#------------------------------------------------------
# Initialise modem for caller ID mode
#------------------------------------------------------
print TTYOUT "at+vcid=1\r";
print TTYOUT "ats0=0\r";

#------------------------------------------------------
# Loop forever, waiting for incomming calls
#------------------------------------------------------
while(1==1)
{
	$line = ;
	chop($line);

	if($line =~ /^[ 	]*$/)
	{
		# Ignore blanks
		next;
	}
	if($line =~ /OK/)
	{
		# Ignore OK
		next;
	}
	if($line =~ /RING/)
	{
		# Ignore OK
		#print "Ringing\n";
		next;
	}
	if($line =~ /RING/)
	{
		#print "Ringing\n";
		next;
	}
	if($line =~ /DATE/)
	{
		($date) = $line =~ /DATE = ([0-9]+)/;
		next;
	}
	if($line =~ /TIME/)
	{
		($time) = $line =~ /TIME = ([0-9]+)/;
		next;
	}
	if($line =~ /NMBR/)
	{
		($number) = $line =~ /NMBR = ([0-9]+)/;
		print "Call from $number at $time on $date\n";
		&Log_Call($number, $date, $time, "");
		next;
	}
	if($line =~ /NAME/)
	{
		($name) = $line =~ /NAME = (.+)/;
		print "Call from $name on $number at $time on $date\n";
		&Log_Call($number, $date, $time, $name);
		next;
	}
	`sleep 1`;
}

sub Log_Call
{
	my ($number, $date, $time, $name) = @_;


	# If we dont know this number
	if($phonebook{$number} =~ /^[	 ]*$/)
	{
		# If Telstra doesn't know either then record new number with ??
		if($name =~ /^[	 ]*$/)
		{
			$phonebook{$number} = "??";
		}
		# Otherwise record the Telstra name provided
		else
		{
			$phonebook{$number} = $name;
		}
	}

	open(LOG, ">>call_log") || die "Problem writing to call log file\n";
	if($phonebook{$number} eq  $name || $name eq "")
	{
		print LOG "$date|$time|$number|$phonebook{$number}\n";
	}
	else
	{
		print LOG "$date|$time|$number|$phonebook{$number} $name\n";
	}
	close(LOG);

	# Now wack it onto the internet
	`./publish_call_log`;

	# And dump another copy of the updated phone book
	&Save_Phone_Book;
}


#------------------------------------------------------
# Open and read the phone book into memory
#------------------------------------------------------
sub Read_Phone_Book
{
	open(PHONEBOOK, ")
	{
		$line = $_;
		chop($line);
		
		($number,$name) = $line =~ /([0-9]+)\|(.*)$/;

		$phonebook{$number} = $name;
		#print "$number = $name\n";
	}
	close(PHONEBOOK);
}

sub Save_Phone_Book
{
	open(PHONEBOOK, ">phone_book") || die "Failed to open phone book for output\n";
	foreach $number (keys %phonebook)
	{
		print PHONEBOOK "$number|$phonebook{$number}\n";
	}
	close(PHONEBOOK);
}


Put the following start script into /bin and call it run_watch_calls
#!/bin/bash
cd /bin/cid
nohup watch_calls.pl &


Put the following in /etc/rc.d/rc.local to automatically start monitoring calls:
# Start caller monitoring on home phone
/bin/run_watch_calls


#/bin/bash
# Get profile settings
. /geoff/.bash_profile

# Write the message to the voice synthesiser
name=`tail -1 call_log | cut -d"|" -f4`
number=`tail -1 call_log | cut -d"|" -f3`
echo "$name is calling from $number" |  voice_client.pl

# Output file
html="$WWW/Secret/call_log.htm"

# Get last 20 lines for web
now=`date`
echo "
" > $html
echo "Last published: $now

" >> $html tail -20 call_log >> $html echo "
" >> $html # Set privs chmod 755 $html chgrp www $html # Also popup a window on hera now=`date "+%H:%M"` #exec popup "$name is calling from $number at $now" & msg="Call received from $name ($number) at $now" /bin/echo "$msg" | /usr/bin/smbclient -M "somepcname"

Click here to check out the list of other projects.

You may also like to click here to check out the list of Artificial Intelligence projects.

If you would like to get any further information on this or any of the other projects shown on this web site, please send an email to Acacia Lateral Technologies. or place a comment in our Guest Book

You might also like to submit your idea to our Free Ideas page for the benefit of other like-minded soles.