my senf to ralfs senf to tuerd
[saartuer.git] / ringd
1 #!/usr/bin/python3
2 import time, socket, atexit
3 import RPi.GPIO as GPIO
4
5 tuerSock = "/run/tuer.sock"
6
7 ringPin = 18
8
9 GPIO.setmode(GPIO.BOARD)
10 GPIO.setup(ringPin, GPIO.IN)
11 atexit.register(GPIO.cleanup);
12
13 lastEvent = 0
14
15
16 while True:
17         GPIO.wait_for_edge(ringPin, GPIO.BOTH)
18         # measure time since event
19         now = time.time()
20         timePassed = now-lastEvent
21         print("Time between events %f" % timePassed)
22         # remember, remember
23         lastEvent = now
24         # action to be taken?
25         if timePassed >= 1.5 and timePassed <= 3:
26                 print("Opening door")
27                 # talk with tuerd
28                 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
29                 s.connect(tuerSock)
30                 s.send(b'buzz')
31                 data = s.recv(4)
32                 s.close()
33                 print("...done")
34                 if data != b'1':
35                         print("Received unexpected answer %s" % str(data))
36
37