-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartDoorBell.py
226 lines (116 loc) · 3.95 KB
/
SmartDoorBell.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
import os
from time import sleep
import RPi.GPIO as GPIO
import time
import serial
import webbrowser
import multiprocessing
import subprocess as sp
from tkinter import *
import tkinter.font
##HARDWARE##
GPIO.setmode(GPIO.BCM)
GPIO_TRIG = 14
GPIO_ECHO = 15
GPIO_COM = 18
GPIO_MO = 23
GPIO_COM2 = 24
GPIO.setup(GPIO_TRIG, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
GPIO.setup(GPIO_COM, GPIO.OUT)
GPIO.setup(GPIO_MO, GPIO.IN)
GPIO.setup(GPIO_COM2, GPIO.OUT)
##VAIRIABLES##
running = False
CameraOn = False
T1 = ()
T2 = ()
##Functions##
def distance():
#print("Measuring distance...")
# set Trigger to HIGH
GPIO.output(GPIO_TRIG, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(GPIO_TRIG, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
#print("Complete")
return distance
##Widgets##
if __name__ == '__main__':
print("Running...")
try:
while True:
dist = int(distance())
#print ("Measured Distance = %.1f cm" % dist)
motion = int(GPIO.input(GPIO_MO))
if(dist < 150 and motion == 1 ):
GPIO.output(GPIO_COM, True)
GPIO.output(GPIO_COM2, True)
print("Start")
print(dist)
print(motion)
if (CameraOn == False):
p1 = sp.Popen(['python3','Stream.py'])
CameraOn = True
running = True
time.sleep(10)
webbrowser.open( "http://10.0.0.3:5000")
time.sleep(300)
elif(dist < 150 and motion == 0 and running == False):
GPIO.output(GPIO_COM, True)
GPIO.output(GPIO_COM2, False)
print("Start")
print(dist)
print(motion)
if (CameraOn == False):
p1 = sp.Popen(['python3','Stream.py'])
CameraOn = True
time.sleep(10)
webbrowser.open( "http://10.0.0.3:5000")
running = True
time.sleep(300)
elif(dist > 150 and motion == 1 ):
GPIO.output(GPIO_COM, False)
GPIO.output(GPIO_COM2, True)
print("Start")
print(dist)
print(motion)
if (CameraOn == False):
p1 = sp.Popen(['python3','Stream.py'])
CameraOn = True
time.sleep(10)
webbrowser.open( "http://10.0.0.3:5000")
running = True
time.sleep(300)
elif(dist > 150 and motion == 0 ):
GPIO.output(GPIO_COM, False)
GPIO.output(GPIO_COM2, False)
if(CameraOn == True):
sp.Popen.terminate(p1)
CameraOn = False
print("Stop")
print(dist)
print(motion)
running = False
time.sleep(1)
# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Measurement stopped by User")
GPIO.cleanup()
if(CameraOn == True):
sp.Popen.terminate(p1)
CameraOn = False
print("Stop")