Flood detection and notification using Perl

by Gordon Meyer

If there's an area in your house that's prone to flooding, such as a basement entryway or the corner behind the washing machine, it's something that can constantly nag at your mind. If it's happened once, it seems that you can never quite trust that it won't happen again, particularly if the circumstances that trigger it are varied or infrequent. In fact, given the recent weather here in California, I'm betting that there are several of you out there who are nervously checking for encroaching moisture, puddles, or (heaven forbid) streams right this very moment.

Rather than fret, put your hacking skills to work and whip up a tool that will notify you when water is detected, so you can rest comfortably or worry about more trivial matters (whichever is your inclination). For Smart Home Hacks, Dr. Edward Cheung contributed Hack #44: Detect Flooding. It's a clever technique for converting a smoke detector into an inexpensive, yet very effective, moisture alarm. In the book I suggested that you could substitute an X10 PowerFlash module for the modified smoke detector and have your computer notify you instead of the blaring siren.

Ray Tracy took this idea and implemented it using Perl, David Shaw's Xtend, and Daniel Suthers' heyu. Xtend and heyu are open source X10 automation utilities that allow you to receive and send, respectively, X10 commands.

The script, in its entirety below, watches for an On command for the X10 address I5. If it sees one, that's the PowerFlash in action, and you know that you've got a water problem to deal with. When that occurs, an On command is sent to the Chime module at address I3.

#!/usr/bin/perl
###########################################################################
## FLOOD WATCH SCRIPT
##
## Bare wires in contact with flood area attached to
## Powerflash module set to unit 5 is the water sensor.
## Bell module set to unit 3 serves as audio alarm.
##
## I use housecode I for my system change yours accordingly
## Change bell (3) or detector unit (5) as desired
##
## This routine sounds the bell at thirty second intervals
## while the sensor is in contact with water.
##
###########################################################################

while(1) {
@info = `heyu info`;
@s = split(/ /,$info[5]);
for ($x = 16; $x >= 1; $x--) { $unit[17-$x] = substr($s[6],$x,1); }
if ($unit[5] eq "1") {
system("heyu turn I3 on");
# uncomment next two lines to have stats printed
#for ($i=1; $i<=16; $i++) { print "$unit[$i]";
#print " - UNIT 5 --> $unit[5]\n";
}
sleep 30;
}
exit 0;



Ray suggests that the sleep time at least 10 seconds to make sure you're not overwhelming your X10 system with commands, but in reality you're probably going to want a longer delay so you don't overwhelm your ears with the chime. The module will keep ringing as long as the water is present, so you're sure not to forget to deal with the problem.

You might also consider adding a routine that sends an email to your cell phone when the PowerFlash is triggered, just in case you're not home to hear the bell. If you decide to tackle this, please get in touch.

What else should this hack do?