-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpir.py
45 lines (39 loc) · 1.66 KB
/
pir.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
import RPi.GPIO as GPIO
import time
import threading
from indoorpersontrackerapi import IndoorPersonTrackerAPI
from configparser import ConfigParser
def sensorloop(gpio_pin, identifier, tracker):
while True:
i=GPIO.input(gpio_pin)
if i==1: #When output from motion sensor is HIGH
print("{}: Motion detected".format(identifier))
tracker.updateDetection(identifier)
time.sleep(1.5) # sleep longer to not detect the same person too often
else:
time.sleep(0.1)
class SensorThread (threading.Thread):
def __init__(self, gpio_pin, identifier):
threading.Thread.__init__(self)
self.gpio_pin = gpio_pin
self.identifier = identifier
def run(self):
print("Started thread for sensor on pin {}\nConnecting to web service...".format(self.gpio_pin))
tracker = IndoorPersonTrackerAPI()
success = tracker.register(self.identifier)
if success:
print("{}: Connection is up!".format(self.identifier))
sensorloop(self.gpio_pin, self.identifier, tracker)
else:
print("{}: error: sensor has not been accepted on server".format(self.identifier))
config = ConfigParser()
config.read('config.ini')
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
for sensor in config.sections():
try:
GPIO.setup(int(config[sensor]['GPIO_PIN']), GPIO.IN) #Read output from PIR motion sensor
sensorthread = SensorThread(int(config[sensor]['GPIO_PIN']), config[sensor]['IDENTIFIER'])
sensorthread.start()
except Exception as error:
print("unable to start thread: {}".format(error))