comments and cleanup
[saartuer.git] / ringd
1 #!/usr/bin/python3
2 import time, socket
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
12 lastEvent = 0
13
14
15 while True:
16         GPIO.wait_for_edge(ringPin, GPIO.BOTH)
17         # measure time since event
18         now = time.time()
19         timePassed = now-lastEvent
20         print("Time between events %f" % timePassed)
21         # remember, remember
22         lastEvent = now
23         # action to be taken?
24         if timePassed >= 1.5 and timePassed <= 3:
25                 print("Opening door")
26                 # talk with tuerd
27                 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
28                 s.connect(tuerSock)
29                 s.send(b'buzz')
30                 data = s.recv(4)
31                 s.close()
32                 print("...done")
33                 if data != b'1':
34                         print("Received unexpected answer %s" % str(data))
35
36 GPIO.cleanup()