Guest User

Untitled

a guest
Feb 4th, 2019
548
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. import RPi.GPIO as GPIO
  4. import os
  5. import time
  6. import sys
  7.  
  8. # PIR pin
  9. REL_GPIO = 21
  10. PIR_GPIO = 16
  11. # Display timeout in seconds
  12. TIMEOUT = 350
  13.  
  14. # DONT EDIT BELOW THIS LINE
  15. # UNLESS YOU KNOW WHAT YOU ARE DOING
  16.  
  17. GPIO.setmode(GPIO.BOARD)
  18. GPIO.setup(PIR_GPIO, GPIO.IN)
  19. GPIO.setup(REL_GPIO, GPIO.IN)
  20. timer = 0
  21. display = False
  22. relais = 0
  23.  
  24.  
  25. def set_display(enable,an=0):
  26.     global display
  27.     display = enable
  28.     relais=an
  29.     os.system("sudo gpio -g write 21 " + "an")
  30.     os.system("sudo vcgencmd display_power " + str(int(enable)))
  31.    
  32.    
  33.  
  34. def check_timeout():
  35.     print("timed")
  36.     if timer >= TIMEOUT and display:
  37.         set_display(False)
  38.         os.system("sudo gpio -g write 21 1")
  39.     elif not display and timer < TIMEOUT:
  40.         set_display(True)
  41.         os.system("sudo gpio -g write 21 0")
  42.  
  43.  
  44. def motion(pin):
  45.     global timer
  46.     timer = 0
  47.  
  48.  
  49. if __name__ == "__main__":
  50.     try:
  51.         GPIO.add_event_detect(PIR_GPIO, GPIO.RISING, callback=motion)
  52.         while True:
  53.             time.sleep(1)
  54.             if timer < TIMEOUT:
  55.                 timer += 1
  56.             check_timeout()
  57.     except KeyboardInterrupt:
  58.         GPIO.cleanup()
  59.         sys.exit(0)
RAW Paste Data