Created
October 31, 2017 00:44
-
-
Save rxhanson/1f364ec3b622bbe167db421276dd4649 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use the Raspberry Pi to detect motion and send an email | |
# When the motion sensor is tripped it will send an email with a duration of motion detection. The duration is so that it might be easier to determine if the motion sensor is being set off by an intruder or something else like changes in light. | |
# All you need is the Pi and a motion sensor. The motion sensor that I used is the Parallax PIR Sensor (Rev B). | |
# My setup is as follows | |
# pin1 -> Vcc on PIR Sensor | |
# pin15 -> Out on PIR Sensor | |
# pin6 -> GND on PIR Sensor | |
import RPi.GPIO as GPIO | |
import smtplib | |
from email.mime.text import MIMEText | |
from time import time | |
def send_email(message): | |
msg = MIMEText(message) | |
msg['Subject'] = 'RaspberryPi detected an intruder' | |
msg['From'] = '<source>@gmail.com' | |
msg['To'] = '<destination>@gmail.com' | |
# send email | |
s = smtplib.SMTP('smtp.gmail.com',587) | |
s.starttls() | |
s.login('<source>@gmail.com', '<password>') | |
s.sendmail(msg['From'], msg['To'], msg.as_string()) | |
s.quit() | |
def run_alarm_system(): | |
print "Alarm System Engaged" | |
while 1: | |
try: | |
if GPIO.event_detected(15): | |
if(GPIO.input(15)==GPIO.HIGH): | |
start_time = time() | |
else: | |
motion_time = time() - start_time | |
print "Seconds of continuous motion: " + str(motion_time) | |
send_email("Continuous motion for ~"+ str(motion_time) + " seconds") | |
except KeyboardInterrupt: | |
GPIO.cleanup() | |
print "Alarm System Disengaged" | |
break | |
GPIO.cleanup() | |
def init_GPIO(detection_mode=GPIO.BOTH): | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) | |
GPIO.add_event_detect(15, detection_mode) | |
init_GPIO() | |
run_alarm_system() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment