-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensors.py
46 lines (34 loc) · 1.12 KB
/
sensors.py
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
import RPi.GPIO as GPIO
import logging
from exchange import set_bot_action,is_bot_action
class Pir:
def __init__(self):
self.pin = 23
GPIO.setup(self.pin, GPIO.IN)
def activate(self):
def execute_on_detection(self):
logging.debug('Movement detected')
GPIO.add_event_detect(self.pin, GPIO.RISING, callback=execute_on_detection)
def deactivate(self):
GPIO.cleanup(self.pin)
def wait_for_movement(self):
no_movement = True
no_bot_action = True
logging.info('Wait for movement...')
while no_movement and no_bot_action:
no_bot_action = not is_bot_action()
if GPIO.event_detected(self.pin):
no_movement = False
logging.info('Movement detected')
set_bot_action(False)
if __name__ == '__main__':
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG,
)
logger = logging.getLogger(__name__)
GPIO.setmode(GPIO.BCM)
pir = Pir()
pir.activate()
while True:
pir.wait_for_movement()