Advertisement
Sascha311

Untitled

Apr 24th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1.  #!/usr/bin/python
  2. import time, sys
  3. import RPi.GPIO as GPIO
  4. import os
  5.  
  6. # Steckplatz am Raspberry
  7. PIR = 22
  8.  
  9. # Initialisierung
  10. def initPIR(PIR):
  11.     print "Warte aus PIR..."
  12.     while GPIO.input(PIR) == 1:
  13.         print "Bereit!"
  14.         return 0
  15.  
  16. # Was passiert bei Bewegung
  17. def motion_on():
  18.     print "Bewegung erkannt um"
  19.     print(time.strftime("%d.%m.%Y %H:%M:%S"))
  20.     os.system('vcgencmd display_power 1')
  21.     time.sleep(300)
  22.     return
  23.  
  24. # Was passiert wenn nach der Wartezeit keine Bewegung mehr wahrgenommen wird
  25. def motion_off():
  26.     print "Keine Bewegung seit"
  27.     print(time.strftime("%d.%m.%Y %H:%M:%S"))
  28.     os.system('vcgencmd display_power 0')
  29.     return
  30.  
  31. GPIO.setmode(GPIO.BCM)
  32. GPIO.setup(PIR, GPIO.IN)
  33.  
  34. PirPreviousState = 0
  35. PirCurrentState = initPIR(PIR)
  36.  
  37. try:
  38.     while True:
  39.         PirCurrentState = GPIO.input(PIR)
  40.        
  41.         if PirCurrentState == 1 and PirPreviousState == 0:
  42.            motion_on()
  43.            PirPreviousState=1
  44.         elif PirCurrentState == 0 and PirPreviousState == 1:
  45.            motion_off()
  46.            PirPreviousState=0
  47.            
  48.         time.sleep(.1)
  49. except KeyboardInterrupt:
  50.     GPIO.cleanup()
  51.     exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement