rpi-experiments/main.py

44 lines
998 B
Python
Raw Permalink Normal View History

2018-04-04 08:39:59 +00:00
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
2018-04-06 20:07:49 +00:00
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
CONNECT_TO = ("10.0.0.4", 2000)
2018-04-04 08:39:59 +00:00
2018-04-04 21:40:10 +00:00
def buttonEventHandler(value):
if value is 0:
GPIO.output(24, True)
2018-04-06 20:07:49 +00:00
sock.sendall("button_press")
2018-04-04 21:40:10 +00:00
print('Button Pressed...')
else:
GPIO.output(24, False)
2018-04-06 20:07:49 +00:00
sock.sendall("button_release")
2018-04-04 21:40:10 +00:00
print('Button Released...')
2018-04-04 08:39:59 +00:00
2018-04-04 21:40:10 +00:00
def main():
print "Let's do this!"
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.OUT)
#GPIO.add_event_detect(23, GPIO.FALLING)
#GPIO.add_event_callback(23, buttonEventHandler)
try:
2018-04-06 20:07:49 +00:00
sock.connect(CONNECT_TO)
2018-04-04 21:40:10 +00:00
while True:
2018-04-06 20:07:49 +00:00
GPIO.wait_for_edge(23, GPIO.BOTH, bouncetime=100)
2018-04-04 21:40:10 +00:00
buttonEventHandler(GPIO.input(23))
2018-04-06 20:07:49 +00:00
except:
2018-04-04 21:40:10 +00:00
GPIO.cleanup()
2018-04-06 20:07:49 +00:00
sock.close()
2018-04-04 21:40:10 +00:00
raise
2018-04-06 20:07:49 +00:00
2018-04-04 21:40:10 +00:00
if __name__== "__main__":
main()