HOWTO:Zoneminder recording if your 'iPhone' is out of home
Posted: Sun Mar 15, 2015 6:55 pm
(Note: this howto needs a lot of cleaning up. It's currently geek-folk friendly, not user friendly - you need to change stuff etc and has dependencies on other changes I made to ZM... I'll clean it all up, I promise)
This weekend, I spent some time thinking about how cool it would be if I could automatically start ZM recording if I could somehow detect if my iPhone was out of the house. That would be a great addition to cron schedules, which I already have, to account for those time when my day does not follow 'schedule'
I first thought I could write a script to 'ping' the iPhone. If you can ping it in your LAN, you are at home. The problem is you can't ping your iPhone if its in screen off state (goes to low power a few seconds after screen off, and does not respond to ICMP). Arping also randomly stops working.
So instead, I looked around to see if there was a way I could use "find my iPhone" (assuming you have it enabled) to get current location, and if that location is not home, then boom - use zmpkg.pl to switch run states
(I did not want to load any special app on my iPhone, as it would contribute to battery drain compared to an iCloud find my iPhone query because its very likely the phone already has an open channel with iCloud and it would be more optimized)
I came across a great package called pyicloud here https://github.com/picklepete/pyicloud
I was hoping there is a Perl equivalent, because I know a bit of Perl, but the Perl library WebService::FindMyiPhone never worked. Well, time to learn a bit of python then.
So I wrote this script to write the word 'in' or 'out' to /usr/share/zoneminder/zm_phone_state.txt --> Now you can check that file to switch modes.
Enjoy!!
This is my first python program, so don't curse it. The only bad part is pyicloud needs the iCloud master password - I'm surprised why application specific password's don't work (and I have 2 factor auth enabled).
I have a Cron schedule to run this script every hour - given that it logs into iCloud, I don't want to hit any API limit of Apple. I think 1 hr is a reasonable time to check (so short in/out trips won't be caught). You can reduce this timeframe in cron.
Code here: https://github.com/pliablepixels/zmhack ... _iphone.py
And here is my cron script that runs every 5 minutes to check the state of iPhone and ZM state. If it finds out ZM thinks I am in, but the phone says I am out, then it forces ZM to switch to "out" run state
https://github.com/pliablepixels/zmhack ... kiPhone.sh
My crontab:
Basically, I run the iPhone location checker once an hour (so there is granularity loss - I just don't want to hit any API limits that iCloud may have. You can reduce this time if you want more frequent pings.
Then, I check for the file it creates every 5 minutes. Yes, I know, why check 5 minutes if it changes only once in an hour. I'll sync them later....
"zm_change_state.sh" is nothing but a wrapper around zmpkg.pl --> it makes sure it does not change state if I am in vacation mode (see my other thread linked above for that) - you can use zmpkg instead
https://github.com/pliablepixels/zmhack ... r/cronfile
This weekend, I spent some time thinking about how cool it would be if I could automatically start ZM recording if I could somehow detect if my iPhone was out of the house. That would be a great addition to cron schedules, which I already have, to account for those time when my day does not follow 'schedule'
I first thought I could write a script to 'ping' the iPhone. If you can ping it in your LAN, you are at home. The problem is you can't ping your iPhone if its in screen off state (goes to low power a few seconds after screen off, and does not respond to ICMP). Arping also randomly stops working.
So instead, I looked around to see if there was a way I could use "find my iPhone" (assuming you have it enabled) to get current location, and if that location is not home, then boom - use zmpkg.pl to switch run states
(I did not want to load any special app on my iPhone, as it would contribute to battery drain compared to an iCloud find my iPhone query because its very likely the phone already has an open channel with iCloud and it would be more optimized)
I came across a great package called pyicloud here https://github.com/picklepete/pyicloud
I was hoping there is a Perl equivalent, because I know a bit of Perl, but the Perl library WebService::FindMyiPhone never worked. Well, time to learn a bit of python then.
So I wrote this script to write the word 'in' or 'out' to /usr/share/zoneminder/zm_phone_state.txt --> Now you can check that file to switch modes.
Enjoy!!
This is my first python program, so don't curse it. The only bad part is pyicloud needs the iCloud master password - I'm surprised why application specific password's don't work (and I have 2 factor auth enabled).
I have a Cron schedule to run this script every hour - given that it logs into iCloud, I don't want to hit any API limit of Apple. I think 1 hr is a reasonable time to check (so short in/out trips won't be caught). You can reduce this timeframe in cron.
Code here: https://github.com/pliablepixels/zmhack ... _iphone.py
And here is my cron script that runs every 5 minutes to check the state of iPhone and ZM state. If it finds out ZM thinks I am in, but the phone says I am out, then it forces ZM to switch to "out" run state
https://github.com/pliablepixels/zmhack ... kiPhone.sh
My crontab:
Basically, I run the iPhone location checker once an hour (so there is granularity loss - I just don't want to hit any API limits that iCloud may have. You can reduce this time if you want more frequent pings.
Then, I check for the file it creates every 5 minutes. Yes, I know, why check 5 minutes if it changes only once in an hour. I'll sync them later....
"zm_change_state.sh" is nothing but a wrapper around zmpkg.pl --> it makes sure it does not change state if I am in vacation mode (see my other thread linked above for that) - you can use zmpkg instead
https://github.com/pliablepixels/zmhack ... r/cronfile