The receptionist here at my office wants to know when I'm away, and I should phone or e-mail every time I leave or arrive (they're pretty low-tech, and don't mind receiving a lot of small e-mails). I tend to forget to let them know. And so does my colleague.
It was time to automate this. As I always carry a mobile phone with me, that has bluetooth enabled for my hands free car kit, I thought it would be neat to use that. Then I don't have to push any button, and can just leave, as long as I don't forget to take my phone.
I bought an extended range USB bluetooth dongle, that works perfectly under Linux. After installing bluez-utils, and compiling a kernel with a bluez stack, I wrote this script:
#!/usr/bin/perl -w
use strict;
my %phones = (
# Bluetooth addresses
'00:16:20:XX:XX:XX' => 'Juerd',
'XX:XX:XX:XX:XX:XX' => 'Foo Bar',
);
{
package Status;
sub new {
my ($class, $fn) = @_;
my $self = bless {}, $class;
open my $fh, '<', $fn or return $self;
while (defined (my $line = readline $fh)) {
my ($addr, $desc) = split ' ', $line, 2;
if ($phones{$addr}) {
$self->{ $phones{$addr} } = 1;
}
}
return $self
}
sub present {
my ($self, $who) = @_;
$self->{ $who };
}
}
package main;
use POSIX qw(strftime);
mkdir "$ENV{HOME}/.btscan" or die $! if not -e "$ENV{HOME}/.btscan";
chdir "$ENV{HOME}/.btscan" or die $!;
system 'mv now before';
system 'hcitool scan > now';
my $before = Status->new('before');
my $now = Status->new('now');
my $time = strftime '%Y-%m-%d %H:%M:%S', localtime;
my @people = keys %{ { reverse %phones } };
for my $person (@people) {
if ($before->present($person) and not $now->present($person)) {
print "[$time] $person left\n";
system './left', $person if -x 'left';
}
if ($now->present($person) and not $before->present($person)) {
print "[$time] $person arrived\n";
system './arrived', $person if -x 'arrived';
}
}
And I ran
crontab -e to let this script be run every minute:
* * * * * /root/btscan.pl
It now emailed root with concise info about my leaving and arriving. But I wanted something a little user-friendlier, so I wrote this script to send a message:
#!/usr/bin/perl -w
use strict;
use MIME::Lite;
my $to = 'foo@example.com';
my %text =
$0 =~ /left/ ? (
subject => "%s is weer weg",
data => "Dit is een geautomatiseerd bericht, dat wordt gest
+uurd " .
"omdat de telefoon van\n%s buiten bereik is."
)
:
$0 =~ /arrived/ ? (
subject => "%s is er weer",
data => "Dit is een geautomatiseerd bericht, dat wordt gest
+uurd " .
"omdat de telefoon van\n%s binnen bereik is."
)
:
die "What am I?"
;
my $person = shift;
my $msg = MIME::Lite->new(
From => 'bar@example.org',
To => $to,
Subject => sprintf($text{subject}, $person),
Data => sprintf($text{data}, $person),
);
$msg->send;
And symlinked that to ~/.btscan/left and ~/.btscan/arrived.
I've been using this for half a day now, and so far it has worked perfectly.
The server here can tell when I'm gone, so I no longer have to remember to notify people.
Updates:
- Removed unused time handling from mail script
- Someone told me that almost all USB bluetooth devices work under Linux because it's standardized