-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquirrel_detector.py
executable file
·185 lines (172 loc) · 4.29 KB
/
Squirrel_detector.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#! /usr/bin/python3
# coding: utf8
import os
import time
from picamera import PiCamera
import adafruit_vl53l0x
import board
import busio
import RPi.GPIO as GPIO
DEBUG = True
if DEBUG:
import logging
from logging.handlers import RotatingFileHandler
logger = logging.getLogger("main_logger")
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
file_handler = RotatingFileHandler('activity.log', 'a', 1000000, 1)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)
logger.addHandler(stream_handler)
SRC_FOLDER = "/home/pi/Squirrel_detector/out/"
IMG_EXT = ".png"
WIFI = True
threshold = 45
BTN = 12
LED = 5
LIGHT = 24
def init_sensor():
global threshold
try:
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_vl53l0x.VL53L0X(i2c)
sensor.measurement_timing_budget = 200000
thrs = []
i = 5
while i > 0:
time.sleep(0.5)
GPIO.output(LED, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(LED, GPIO.LOW)
distance = sensor.range // 10
if DEBUG:
logger.debug("distance = %d" % distance)
if 0 < distance < 50:
thrs.append(distance)
i -= 1
if len(thrs):
new_thr = sum(thrs) // len(thrs)
if 0 < new_thr <= 50:
threshold = new_thr
if DEBUG:
logger.debug("threshold = %d" % threshold)
except Exception as e:
if DEBUG:
logger.info("Error setting dist sensor %s" % str(e))
return sensor
def init_GPIO():
GPIO.setup(BTN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED, GPIO.OUT)
GPIO.output(LED, GPIO.LOW)
def light_sensor():
count = 0
#Output on the pin for
GPIO.setup(LIGHT, GPIO.OUT)
GPIO.output(LIGHT, GPIO.LOW)
time.sleep(0.1)
#Change the pin back to input
GPIO.setup(LIGHT, GPIO.IN)
#Count until the pin goes high
while (GPIO.input(LIGHT) == GPIO.LOW) and count <= 10000:
count += 1
return count
def clean_all():
cmd = 'ifconfig wlan0 up'
os.system(cmd)
time.sleep(1)
cmd = "systemctl start [email protected]"
GPIO.output(LED, GPIO.LOW)
GPIO.cleanup()
def wifi_switch():
global start_AP
if WIFI == False:
if DEBUG:
logger.info("Disactivate Wifi AP")
cmd = "systemctl start [email protected]"
os.system(cmd)
time.sleep(1)
cmd = 'ifconfig wlan0 down'
os.system(cmd)
elif WIFI == True:
if DEBUG:
logger.info("Activate Wifi AP")
cmd = 'ifconfig wlan0 up'
os.system(cmd)
time.sleep(1)
cmd = "systemctl start [email protected]"
os.system(cmd)
start_AP = time.time()
time.sleep(1)
def button_callback(channel):
if GPIO.input(BTN) == 0:
global WIFI
if DEBUG:
logger.info("Button pushed ! Switching wifi state to %s" % ("OFF" if WIFI else "ON"))
WIFI = not WIFI
wifi_switch()
def take_snap():
with PiCamera(resolution=(1920, 1080)) as camera:
camera.rotation = 180
filename = SRC_FOLDER + str(time.time()) + IMG_EXT
if DEBUG:
logger.info("captured %s" % filename)
camera.capture(filename)
def test_snap():
try:
take_snap()
except Exception as e:
if DEBUG:
logger.debug(str(e))
clean_all()
exit()
GPIO.output(LED, GPIO.HIGH)
time.sleep(3)
GPIO.output(LED, GPIO.LOW)
if __name__ == "__main__":
if DEBUG:
logger.info("SQRT Started...")
init_GPIO()
sensor = init_sensor()
WIFI = False
start_AP = None
wifi_switch()
GPIO.add_event_detect(BTN, GPIO.FALLING, callback=button_callback, bouncetime=500)
try:
test_snap()
while True:
light = light_sensor()
if DEBUG:
logger.debug("Light level = %d" % light)
if not WIFI and light > 10000:
logger.info("Going to sleep for 10 min...")
time.sleep(60 * 10)
continue
if WIFI == True:
time.sleep(0.5)
GPIO.output(LED, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(LED, GPIO.LOW)
if start_AP != None and time.time() - start_AP >= 60 * 15:
start_AP = None
WIFI = False
wifi_switch()
if DEBUG:
distance = sensor.range // 10
logger.debug("distance = %d" % distance)
else:
GPIO.output(LED, GPIO.LOW)
time.sleep(1)
distance = sensor.range // 10
if DEBUG:
logger.debug("distance = %d" % distance)
if distance > 0 and distance < threshold:
if DEBUG:
logger.debug("SNAP")
take_snap()
except Exception as e:
if DEBUG:
logger.debug(str(e))
clean_all()