Advertisement
Guest User

PIR Motion Script

a guest
Jun 4th, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. #!/usr/bin/python
  2. #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3. #|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k|
  4. #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  5. #
  6. # pir_1.py
  7. # Detect movement using a PIR module
  8. #
  9. # Author : Matt Hawkins
  10. # Date   : 21/01/2013
  11.  
  12. # Import required Python libraries
  13. import RPi.GPIO as GPIO
  14. import time
  15. import subprocess
  16. import os.path
  17.  
  18. # Use BCM GPIO references
  19. # instead of physical pin numbers
  20. GPIO.setmode(GPIO.BCM)
  21.  
  22. # Define GPIO to use on Pi
  23. GPIO_PIR = 7
  24.  
  25. print "PIR Module Test (CTRL-C to exit)"
  26.  
  27. # Set pin as input
  28. GPIO.setup(GPIO_PIR,GPIO.IN)      # Echo
  29.  
  30. Current_State  = 0
  31. Previous_State = 0
  32.  
  33. try:
  34.  
  35.   print "Waiting for PIR to settle ..."
  36.  
  37.   # Loop until PIR output is 0
  38.   while GPIO.input(GPIO_PIR)==1:
  39.     Current_State  = 0    
  40.  
  41.   print "  Ready"    
  42.    
  43.   # Loop until users quits with CTRL-C
  44.   while True :
  45.    
  46.     # Read PIR state
  47.     Current_State = GPIO.input(GPIO_PIR)
  48.    
  49.     if Current_State==1 and Previous_State==0:
  50.       # PIR is triggered
  51.  
  52.       Dateida = os.path.exists("/var/www/go.info")
  53.       if Dateida == True:
  54.         subprocess.call(['/home/pi/Alarm/scripts/alarm.sh'])    
  55.  
  56.       print "  Motion detected!"
  57.       # Record previous state
  58.       Previous_State=1
  59.     elif Current_State==0 and Previous_State==1:
  60.       # PIR has returned to ready state
  61.       print "  Ready"
  62.       Previous_State=0
  63.      
  64.     # Wait for 10 milliseconds
  65.     time.sleep(0.01)      
  66.      
  67. except KeyboardInterrupt:
  68.   print "  Quit"
  69.   # Reset GPIO settings
  70.   GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement