Guest User

Untitled

a guest
Nov 2nd, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. # Use the Raspberry Pi to detect motion and send an email
  2. # 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.
  3. # All you need is the Pi and a motion sensor. The motion sensor that I used is the Parallax PIR Sensor (Rev B).
  4. # My setup is as follows
  5. # pin1 -> Vcc on PIR Sensor
  6. # pin15 -> Out on PIR Sensor
  7. # pin6 -> GND on PIR Sensor
  8.  
  9. import RPi.GPIO as GPIO
  10. import smtplib
  11. from email.mime.text import MIMEText
  12. from time import time
  13.  
  14. def send_email(message):
  15. msg = MIMEText(message)
  16. msg['Subject'] = 'RaspberryPi detected an intruder'
  17. msg['From'] = '<source>@gmail.com'
  18. msg['To'] = '<destination>@gmail.com'
  19. # send email
  20. s = smtplib.SMTP('smtp.gmail.com',587)
  21. s.starttls()
  22. s.login('<source>@gmail.com', '<password>')
  23. s.sendmail(msg['From'], msg['To'], msg.as_string())
  24. s.quit()
  25.  
  26. def run_alarm_system():
  27. print "Alarm System Engaged"
  28. while 1:
  29. try:
  30. if GPIO.event_detected(15):
  31. if(GPIO.input(15)==GPIO.HIGH):
  32. start_time = time()
  33. else:
  34. motion_time = time() - start_time
  35. print "Seconds of continuous motion: " + str(motion_time)
  36. send_email("Continuous motion for ~"+ str(motion_time) + " seconds")
  37. except KeyboardInterrupt:
  38. GPIO.cleanup()
  39. print "Alarm System Disengaged"
  40. break
  41. GPIO.cleanup()
  42.  
  43. def init_GPIO(detection_mode=GPIO.BOTH):
  44. GPIO.setmode(GPIO.BOARD)
  45. GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  46. GPIO.add_event_detect(15, detection_mode)
  47.  
  48. init_GPIO()
  49. run_alarm_system()
Add Comment
Please, Sign In to add comment