Advertisement
EmptySet5150

Raspberry Pi - Python - Door Alarm

Jun 2nd, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. from time import time
  3. from time import sleep
  4.  
  5.  
  6. GPIO.setmode(GPIO.BOARD)
  7. doorSwitch = 11
  8. buzzer = 15
  9.  
  10.  
  11. GPIO.setup(doorSwitch, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  12. GPIO.setup(buzzer, GPIO.OUT)
  13. GPIO.output(buzzer, 0)
  14.  
  15. doorStatus = 0
  16. openTime = time()
  17.  
  18.  
  19. try:
  20.         while True:
  21.                 if GPIO.input(doorSwitch) == 1 and doorStatus == 0:
  22.                         openTime = time()
  23.                         doorStatus = 1
  24.                         print("Door is Open")
  25.                 if GPIO.input(doorSwitch) == 1 and doorStatus == 1:
  26.                         if time() - openTime >= 120:
  27.                                 print("Open 2 minutes")
  28.                                 GPIO.output(buzzer, 1)
  29.                                 sleep(5)
  30.                                 GPIO.output(buzzer, 0)
  31.                                 sleep(1)
  32.                         if time() - openTime >= 30 and time() - openTime < 120:
  33.                                 print("Open 30 seconds")
  34.                                 GPIO.output(buzzer, 1)
  35.                                 sleep(1)
  36.                                 GPIO.output(buzzer, 0)
  37.                                 sleep(3)
  38.                 if GPIO.input(doorSwitch) == 0 and doorStatus == 1:
  39.                         doorStatus = 0
  40.                         GPIO.output(buzzer, 0)
  41.                         print("Door is Closed")
  42.  
  43. except KeyboardInterrupt:
  44.         GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement