3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Garage Monitor 3001 Paging System

Step 3Monitoring Script

Monitoring Script
This is the script I use to monitor the garage, which will grab the page, and insert the status in the database.

#Script to enter garage status in DB
#Joe McManus 2010/01/26


#who mail should go to
$mailto="joe@example.com";

#who mail comes from
$mailfrom="garagemon@example.com";

#set the time
$now=date('Y-m-d H:i');

#web status  url
$garageurl="pink.example.com/garagemon-text.html";

#public url to include in page to check status online
$puburl="http://www.example.com/garage.php";

function connect(&$db){
  $db = mysql_pconnect("localhost", "garage", "monitor");
  if ( mysql_select_db("garagemon", $db)) {
        } else {
        echo "Sorry we are currently experiences technical issues. We'll be back shortly.";
        exit;
        }
}

connect($db);

#check the page
system("curl $garageurl -o /tmp/garage.txt", $retval);
if ($retval == 1 ) {
        echo "Error: Unable to reach web server";
        exit;
}

#Get the door status
$overhead=exec('grep Overhead /tmp/garage.txt | cut -d" " -f2');
$side=exec('grep Side /tmp/garage.txt | cut -d" " -f2');

#insert the side door in to the db
$insert="insert into status(id, date, door, status)";
$insert.="values('', now(), 'side', '$side')";
$result=mysql_query($insert, $db);

#insert the overhead door in to the db
$insert="insert into status(id, date, door, status)";
$insert.="values('', now(), 'overhead', '$overhead')";
$result=mysql_query($insert, $db);

echo "Overhead: $overhead ";
echo "Side: $side ";

#Query for doors being open 10 minutes or more.
$query='select date, door, status from status where status="Open" and date > date_sub(now(), interval 10 minute)';
$result=mysql_query($query, $db);
$rowcount=mysql_num_rows($result);

$mailfile="/tmp/garagemail.txt";
if ($rowcount >= 2) {
        echo "Alert: Door Open ";
        //Message start
        $mailsubject="Alert: Garage Door Open $now";
        $mailmsg="The Garage Monitor 3001 has detected that a door has been open for more than 10 minutes. $now ";
        $mailmsg.="$puburl ";

        if(file_exists($mailfile)) {
                echo "Mail not being sent $mailfile exists ";
        } else {
                mail($mailto, $mailsubject, $mailmsg);
                exec("/bin/touch $mailfile");
        }
} else {
        if(file_exists($mailfile)) {
                echo "Alert: Garage Door Closed $now";
                $mailsubject="Alert: Garage Door Closed";
                $mailmsg="The Garage Monitor 3001 has detected that a door has been closed. $now ";
                $mailmsg.="$puburl ";
                mail($mailto, $mailsubject, $mailmsg);
                exec("/bin/rm $mailfile");
        }
}

#Just a query for debugging
$query='select date, door, status from status where date > date_sub(now(), interval 10 minute)';
$result=mysql_query($query, $db);
echo  "door    | status  | date ";
while($row=mysql_fetch_row($result)) {
        echo "$row[0] | $row[1] | $row[2] ";
}

?>

« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
102
Followers
70
Author:joe
I like to tinker with just about anything, sometimes it works out in the end. Have fun looking at the projects, try tearing something open and let me know how it goes. cheers, -Joe