check-gpio.py 819 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env python3
  2. #
  3. # example of service to monitor a GPIO for Orange Pi PC 2e
  4. # (unfortunately it doesn't support edge triggers)
  5. import sys
  6. import ssl
  7. import time
  8. from urllib import request
  9. import OPi.GPIO as GPIO
  10. URL = "https://localhost:9000/button"
  11. PIN = 12
  12. DEBOUNCE_CYCLES = 5
  13. GPIO.setboard(GPIO.PC2)
  14. GPIO.setmode(GPIO.BOARD)
  15. GPIO.setup(PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  16. ctx = ssl.create_default_context()
  17. ctx.check_hostname = False
  18. ctx.verify_mode = ssl.CERT_NONE
  19. activeCycles = 0
  20. while True:
  21. if GPIO.input(PIN):
  22. if activeCycles == DEBOUNCE_CYCLES:
  23. print("BUTTON PRESSED!")
  24. req = request.Request(URL, data={})
  25. request.urlopen(req, context=ctx)
  26. activeCycles += 1
  27. else:
  28. activeCycles = 0
  29. time.sleep(0.01)
  30. OPi.GPIO.cleanup()