sensors-polling.py 761 B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env python3
  2. import time
  3. from time import sleep
  4. import RPi.GPIO as GPIO
  5. pin_back = 19
  6. pin_front = 21
  7. GPIO.setmode(GPIO.BOARD)
  8. GPIO.setup(pin_back, GPIO.IN) # , pull_up_down=GPIO.PUD_DOWN)
  9. previous_state = None
  10. def my_callback(channel):
  11. global previous_state
  12. # time.sleep(0.2) # confirm the movement by waiting 1.5 sec
  13. pin_val = GPIO.input(pin_back)
  14. if pin_val != previous_state:
  15. previous_state = pin_val
  16. if pin_val: # and check again the input
  17. print("[ ] DOOR CLOSED !")
  18. else:
  19. print("[+] DOOR OPENED !")
  20. GPIO.add_event_detect(pin_back, GPIO.BOTH, callback=my_callback)
  21. # , bouncetime=500)
  22. # you can continue doing other stuff here
  23. while True:
  24. time.sleep(50000)
  25. pass