forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPIR_motion.py
More file actions
executable file
·52 lines (37 loc) · 1.12 KB
/
PIR_motion.py
File metadata and controls
executable file
·52 lines (37 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
# Detect motion with a PIR hooked to a Raspberry Pi.
# Copyright 2014 by Akkana Peck. Share and enjoy under the GPLv2 or later.
import RPi.GPIO
class PIR:
def __init__(self, pin):
self.pin = pin
RPi.GPIO.setmode(RPi.GPIO.BCM)
RPi.GPIO.setup(self.pin, RPi.GPIO.IN)
def poll(self):
return RPi.GPIO.input(self.pin)
def set_callback(self, cb):
RPi.GPIO.add_event_detect(self.pin, RPi.GPIO.RISING, callback=cb)
if __name__ == "__main__":
import time
import sys
def motion_detected(pin):
print "Motion detected! (callback)"
pin = 7
use_callback = False
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
if arg == "-c":
use_callback = True
else:
pin = int(arg)
pir = PIR(pin)
if use_callback:
pir.set_callback(motion_detected)
while True:
if use_callback:
print "Sleeping a long time ..."
time.sleep(300)
else:
if pir.poll():
print "Motion detected! (poll)"
time.sleep(1)