main.py 998B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import RPi.GPIO as GPIO
  4. import time
  5. import socket
  6. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  7. CONNECT_TO = ("10.0.0.4", 2000)
  8. def buttonEventHandler(value):
  9. if value is 0:
  10. GPIO.output(24, True)
  11. sock.sendall("button_press")
  12. print('Button Pressed...')
  13. else:
  14. GPIO.output(24, False)
  15. sock.sendall("button_release")
  16. print('Button Released...')
  17. def main():
  18. print "Let's do this!"
  19. GPIO.setmode(GPIO.BCM)
  20. GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  21. GPIO.setup(24, GPIO.OUT)
  22. #GPIO.add_event_detect(23, GPIO.FALLING)
  23. #GPIO.add_event_callback(23, buttonEventHandler)
  24. try:
  25. sock.connect(CONNECT_TO)
  26. while True:
  27. GPIO.wait_for_edge(23, GPIO.BOTH, bouncetime=100)
  28. buttonEventHandler(GPIO.input(23))
  29. except:
  30. GPIO.cleanup()
  31. sock.close()
  32. raise
  33. if __name__== "__main__":
  34. main()