First post! I've managed to get geofencing working with my Android phone using IFTTT's Maker Channel to toggle my ZoneMinder Run State depending on whether I'm home or away. The same should work for iPhones. Here's how:
Note: You'll need to make sure that your ZoneMinder server is publicly accessible at a static IP or hostname so IFTTT's Maker Channel can send the "change Run Time" request to your ZoneMinder API. If you don't know what all that means, you'll find out in a moment!
1. Set up your ZoneMinder Run States. For me, that means turning the function of the monitors inside my house to "Monitor" when I get home (saved as "Home" Run Time), and to "Modect" when I go away (saved as "Away" Run Time). See "I" here for more info. Remember what you call your home/away Run Times, you'll need them in part 3 below.
2. Save the following code into a new file called "setruntime.php" in your zoneminder www folder (/usr/share/zoneminder/www for me). You'll need to replace USER/PASS with your ZoneMinder username and password. If you've set up apache to use https, change the $url1 and $url2 variables to "https://...".
Code: Select all
<?php
//variables
$tmpfname = tempnam ("/tmp", 'cookiename');
$runstate = htmlspecialchars($_GET["runstate"]);
$url1 = "http://localhost/zm/index.php";
$url2 = "http://localhost/zm/api/states/change/" . $runstate . ".json";
$post = "username=USER&password=PASS&action=login&view=console";
//
//Authenticate
//
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url1);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_POSTFIELDS, $post);
curl_setopt( $ch, CURLOPT_COOKIEJAR, $tmpfname );
curl_setopt( $ch, CURLOPT_COOKIEFILE, $tmpfname );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//
//Change Run State
//
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url2);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_COOKIEJAR, $tmpfname );
curl_setopt( $ch, CURLOPT_COOKIEFILE, $tmpfname );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>
http://YOURSERVER/zm/setruntime.php?run ... NSTATENAME
We need this because IFTTT's Maker Channel can only send standard HTTP requests/posts, and can't do any authentication or store cookies (which we need to do to access ZoneMinder's API).
3. Install IFTTT on your (Android/iPhone) phone. Sign in/up for an account.
4. In IFTTT go to "My Applets". Add a new Applet. The "this" should be your Android (or iPhone) location, the trigger being "You enter an area". Set the location of your home and continue. The "that" should be Maker Channel, the action being "Make a web request". In the action details, add the following URL, replacing YOURSERVER with your ZoneMinder server's IP address/hostname, and HOMERUNSTATENAME with whatever you've called your "I'm Home" Run State (in my case, that's "Home").
http://YOURSERVER/zm/setruntime.php?run ... NSTATENAME
You can leave the other settings as they are. Click on the tick and then Finish.
5. Repeat #4 but the location trigger should be "You exit an area" and the URL in the Maker action details should be the following, replacing YOURSERVER with your ZoneMinder server's IP address/hostname, and AWAYRUNSTATENAME with whatever you've called your "I'm Away" Run State (in my case, that's "Away").
http://YOURSERVER/zm/setruntime.php?run ... NSTATENAME
That's it. IFTTT should start toggling your Run Times when you arrive at/leave home.
Hope this helps some folks. Shout if you have any questions.