Controlling Internet Connection and External Hard Drives
Phidgets are great! After going through two X10 controllers that failed on me two months after purchase I was looking for some dependable way on how to control my Internet connection and how to access my external hard drives when I am not home.
I did not want to keep hard drives always on and my cable modem needs to be re-booted every 3-5 days. I travel and always leave my old workhorse PowerMac G5 "on" all the time.
Phidgets controller and Servos
I bought a four servos Phidgets controller and installed Phidget21 API on my Panther version of Mac OS X. I had a few Futaba servos laying around.
First I had to check and make sure the software part works. After a few trials finally I got software part figured-out. I ran Perl daemon which checks Internet connection and requests for files on external hard drives.
- cable modem and a router hooked-up to one power strip.
- external hard drive connected to the other power strip.
The idea was to have an Applescript turn-on/off power strip switches when appropriate action requested or required. To figure-out if Internet connection is present easy enough, and to check for httpd.log file with "tail" from Perl daemon is even easier. I am running Apache on my PowerMac accessible from the Internet.
I glued servos to the power strips in position where they could flip the strip switch. Now was the time to connect it all to the Mac and figure-out positions where servos will turn strips on and off.
Perl and AppleScript
Perl daemon runs and checks for events every 30 seconds. Something like this will do:
#!/usr/bin/perl
use POSIX qw(setsid);
$| = 1;
&daemonize;
while(1) {$rdtail = `tail -n 40 /var/log/httpd/httpd.log`;
if($rdtail =~ m/trigger.file/) {`/usr/bin/perl /turn-on.hdd.cgi`; next;} # run Perl programm
sleep(30);} ## WHILE END
sub daemonize {
chdir '/' or die "Can't chdir to /: $!";
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
defined(my $pid = fork) or die "Can't fork: $!";
exit if $pid;
setsid or die "Can't start a new session: $!";
umask 0;
}
That's the Perl Program that runs the AppleScript. You could run AppleScript right from the daemon, but I have so-many Perl programs triggered by the daemon - I prefer each of them being separated.
#!/usr/bin/perl
unless($ENV{'SECURITYSESSIONID'}) {
$ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local:/usr/local/bin';
}
$phsrs = 'tell application "Phidget Servo Controller" to activate
delay 3
tell application "System Events"
-- move servo to "ON" position
tell process "Phidget Servo Controller" to tell slider 2 of list 1 of window 1 to set value to 154
keystroke tab
delay 1
keystroke space
delay 1
-- move servo to "Neutral" position
tell process "Phidget Servo Controller" to tell slider 2 of list 1 of window 1 to set value to 112
keystroke space
delay 1
end tell
tell application "Phidget Servo Controller" to quit';
open(TXT,">/temp.save.dir/turnon.scpt"); print TXT $phsrs; close(TXT);
`osascript /temp.save.dir/turnon.scpt`; sleep(15);
Perl script shown without checking procedure weather or not hard drive was mounted before and after AppleScript ran. The same way hard drive unmounted by Perl.
There is no limit what could be controlled by Phidgets - computer peripherals or in your house. Mine also makes coffe in the morning for me.
Limit is nly ones' imagination and amount of work that has to go into it.
Thanks for reading. Check-out our Mac Calls Home software as well.
More Apple stuff Phidgets Website