Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (15)
    As of 2015-07-31 13:08 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (277 votes), past polls