Skip to content

Instantly share code, notes, and snippets.

@d3mon187
Created March 24, 2014 17:20
Show Gist options
  • Save d3mon187/9744846 to your computer and use it in GitHub Desktop.
Save d3mon187/9744846 to your computer and use it in GitHub Desktop.
Script that controls events for leaving home and coming back using phone pings and wemo motion sensors.
import os
import subprocess
import time
import datetime
import ephem #pyephem integration for checking if lights need to remain on
o=ephem.Observer()
o.lat='30' #change to your latitude
o.long='-98' #change to your longitude
running=1
gone=1
lightoff=0
p= subprocess.call("wemo list",shell=True)
hostname="192.168.1.7" #change to phones local ip
while running == 1:
response = os.system("ping -n 1 -w 2000 " + hostname) #change to ping -c 1 for linux
if response == 0: #im home, phone was connected, and so we're going to check if the sun is up
if gone == 1:
now = datetime.datetime.now().time()
s=ephem.Sun()
s.compute()
sunstart = ephem.localtime(o.next_rising(s))
start = sunstart.time()
sunstop = ephem.localtime(o.next_setting(s))
stop = sunstop.time()
if start <= now <= stop:
print(hostname,'is up')
print("Sun is up, no need for lights, turning them off in 30 seconds")
time.sleep(30)
p = subprocess.call("wemo switch \"Living Room Lamp\" off & wemo switch \"Hall Light\" off",shell=True) #change to name of wemo devices
gone=0
lightoff=0
time.sleep(300)
else:
print(hostname,'is up')
print(hostname,'Sun is down, leaving the lights on')
lightoff=0
gone=0
time.sleep(300)
else:
print("already home")
time.sleep(60)
else: #im gone
if lightoff== 0:
#ping again to make sure a packet wasn't dropped.
time.sleep(30)
response2 = os.system("ping -n 1 -w 2000 " + hostname) #change to ping -c 1 for linux
if response2 == 0:
print("just a hickup")
time.sleep(60)
else:
print(hostname, 'is down')
print("turning off light")
#you can add devices to turn off here, change the thermostat, whatever.
p= subprocess.call("wemo switch \"Living Room Lamp\" off & wemo switch \"Hall Light\" off",shell=True) #change to name of wemo devices
#process below controls an onkyo receiver in the living room using miracle2k's onkyo-eiscp
#p= subprocess.call("onkyo --host 192.168.1.3 --port 60128 system-power=standby",shell=True)
gone=1
lightoff=1
time.sleep(180)
else:
print(hostname, 'is down, light already turned off')
#now we're going to run watch.py to monitor the front door wemo motion detector.
#wifi would not connect quick enough on my phone to trigger lights to come on, so motion is used.
p= subprocess.call("python watch.py wemo",shell=True) #change wemo to name of wemo motion device
#motion detected, time to turn on the lights, set thermostat, welcome home, etc.
p = subprocess.call("wemo switch \"Living Room Lamp\" on & wemo switch \"Hall Light\" on",shell=True) #change to name of wemo devices
#p = subprocess.call("onkyo --host 192.168.1.3 --port 60128 system-power=on",shell=True)
#time.sleep(1)
#p = subprocess.call("onkyo --host 192.168.1.3 --port 60128 audio-selector=analog",shell=True)
#time.sleep(1)
#p = subprocess.call("plink -batch -pw password [email protected] tts 'Welcome home... Let me know if you need anything'",shell=True)
#p = subprocess.call("onkyo --host 192.168.1.3 --port 60128 audio-selector=hdmi",shell=True)
#after motion has been detected, we are going to give the phone 60 seconds to connect.
time.sleep(60)
#if it connects, great... if not, sound the alarm!
response3 = os.system("ping -n 1 -w 2000 " + hostname) #change to ping -c 1 for linux
if response3 == 0:
print("entry has been authorized")
time.sleep(5)
else:
print("INTRUDER ALERT")
#process below plays an intruder alert message from my raspberry pi
#p = subprocess.call("onkyo --host 192.168.1.3 --port 60128 audio-selector=analog",shell=True)
#p = subprocess.call("plink -batch -pw password [email protected] tts 'Intruder alert! Intruder alert! Intruder alert! The Police have been notified. Engaging home defence systems.'",shell=True)
time.sleep(5)
import argparse
import sys
import subprocess
import time
from ouimeaux.environment import Environment
from ouimeaux.utils import matcher
from ouimeaux.signals import receiver, statechange, devicefound
def mainloop(name):
matches = matcher(name)
@receiver(devicefound)
def found(sender, **kwargs):
if matches(sender.name):
print "Found device:", sender.name
@receiver(statechange)
def motion(sender, **kwargs):
if matches(sender.name):
print "{} state is {state}".format(
sender.name, state="on" if kwargs.get('state') else "off")
if kwargs.get('state'):
print "motion detected"
sys.exit(0)
else:
print "do nothing"
env = Environment(with_cache=False)
try:
env.start()
env.discover(10)
env.wait()
except (KeyboardInterrupt, SystemExit):
print "Goodbye!"
sys.exit(0)
if __name__ == "__main__":
parser = argparse.ArgumentParser("Motion notifier")
parser.add_argument("name", metavar="NAME",
help="Name (fuzzy matchable)"
" of the Motion to detect")
args = parser.parse_args()
mainloop(args.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment