Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

Juerd # { site => 'juerd.nl', do_not_use => 'spamtrap', perl6_server => 'feather' }

Updates:
  • Removed unused time handling from mail script
  • Someone told me that almost all USB bluetooth devices work under Linux because it's standardized

In reply to Bluetooth people presence detector :) by Juerd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-24 12:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found