-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
207 lines (174 loc) · 6.24 KB
/
main.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import mpu6050
from machine import *
import network
import esp32
import time
import binascii
import urequests
import json
motion_detected = False
active = False
green_led = Pin(26, Pin.OUT)
red_led = Pin(25, Pin.OUT)
sample_timer = Timer(1)
motion_detect_timer = Timer(2)
ssid = ""
password = ""
wlan = network.WLAN(network.STA_IF)
i2c = I2C(scl=Pin(22), sda=Pin(23))
mpu = mpu6050.accel(i2c)
thingspeak_url = 'https://api.thingspeak.com/channels/1724153/fields/1.json?api_key=&results=1'
ifttt_url = 'https://maker.ifttt.com/trigger/Motion_Detected/with/key/'
test_ifttt_url = ifttt_url + '?value1=1&value2=2&value3=3'
# hardcoded offsets found from calibration
ac_offsets = {
"AcX" : 990,
"AcY" : -199,
"AcZ" : -1184,
"Tmp" : 0,
"GyX" : 0,
"GyY" : 0,
"GyZ" : 0
}
def main():
global active
global motion_detected
green_led.off()
red_led.off()
calibrate()
get_connection()
sample_timer.init(mode=Timer.PERIODIC, period=30000, callback=sample_timer_callback)
motion_detect_timer.init(mode=Timer.ONE_SHOT, period=10000, callback=motion_detect_timer_callback)
while True:
# activate will trigger on timeout and set this var in callback
if (active):
green_led.on()
# Sample the mpu
ac_vals = mpu.get_values()
ac_adjusted = {key: ac_vals[key] - ac_offsets.get(key, 0) for key in ac_vals}
if (ac_adjusted.get('AcX') > 500 or ac_adjusted.get('AcY') > 500 or ac_adjusted.get('AcZ') > 500):
# if the first occurence of motion detected, send a notification with the values we just got
if not motion_detected:
motion_detected = True
send_notification(ac_adjusted)
motion_detect_timer.init(mode=Timer.ONE_SHOT, period=10000, callback=motion_detect_timer_callback)
red_led.on()
else:
red_led.off()
else:
green_led.off()
red_led.off()
time.sleep(0.1)
def calibrate():
num_iterations = 200
ac_sum = {
"AcX_sum" : 0,
"AcY_sum" : 0,
"AcZ_sum" : 0
}
ac_avg = {}
printProgressBar(0, num_iterations, prefix = 'Calibrating:', suffix = 'Complete', length = 50)
for i in range(num_iterations):
vals = mpu.get_values()
ac_sum["AcX_sum"] = ac_sum["AcX_sum"] + vals["AcX"]
ac_sum["AcY_sum"] = ac_sum["AcY_sum"] + vals["AcY"]
ac_sum["AcZ_sum"] = ac_sum["AcZ_sum"] + vals["AcZ"]
time.sleep(0.1)
printProgressBar(i+1, num_iterations, prefix = 'Calibrating:', suffix = 'Complete', length = 50)
ac_avg["AcX_avg"] = round(ac_sum["AcX_sum"] / num_iterations)
ac_avg["AcY_avg"] = round(ac_sum["AcY_sum"] / num_iterations)
ac_avg["AcZ_avg"] = round(ac_sum["AcZ_sum"] / num_iterations)
ac_offsets["AcX"] = ac_avg["AcX_avg"]
ac_offsets["AcY"] = ac_avg["AcY_avg"]
ac_offsets["AcZ"] = ac_avg["AcZ_avg"]
print(ac_offsets)
def get_connection():
if wlan.isconnected():
return wlan
connected = False
try:
time.sleep(1)
if wlan.isconnected():
return wlan
wlan.active(True)
networks = wlan.scan()
print('Available networks:', str([i[0].decode('utf-8') for i in networks]))
connected = do_connect(ssid, password)
except OSError as e:
print("exception", str(e))
return wlan if connected else None
def do_connect(ssid, password):
wlan.active(True)
if not wlan.isconnected():
wlan.status()
print('connecting to network', ssid)
wlan.connect(ssid, password)
for retry in range(100):
connected = wlan.isconnected()
if connected:
break
time.sleep(0.1)
print('.', end='')
if connected:
print('\nconnected to:', ssid)
print('IP Address:', wlan.ifconfig()[0])
else:
print('\nnot connected to: ' + ssid)
return connected
def sample_timer_callback(t):
'''Sample thingspeak for activation status'''
global active
url = thingspeak_url
res = http_req(url, 'GET')
# catch bad responses
try:
res_dict = json.loads(res)
field1 = res_dict.get('feeds')[0].get('field1').strip()
if field1 is not None:
if (field1 == 'activate'):
active = True
else:
active = False
print(field1)
except ValueError as e:
print("exception", str(e))
def motion_detect_timer_callback(t):
global motion_detected
motion_detected = False
def send_notification(ac_vals):
acx = round(ac_vals.get('AcX') / 16384, 2)
acy = round(ac_vals.get('AcY') / 16384, 2)
acz = round(ac_vals.get('AcZ') / 16384, 2)
url = f'{ifttt_url}?value1={acx}&value2={acy}&value3={acz}'
http_req(url, 'POST')
print('Sent Notification')
def http_req(req_url, req_type):
res = None
if req_type == 'POST':
res = urequests.post(url=req_url).text
elif req_type == 'GET':
res = urequests.get(url=req_url).text
return res
# Print iterations progress
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', printEnd = "\r"):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
# Print New Line on Complete
if iteration == total:
print()
if __name__ == "__main__":
main()