-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
180 lines (146 loc) · 4.46 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
import cv2
import sys
from mail import sendEmail,sendAlertEmail
from flask import Flask, render_template, Response
from camera import VideoCamera
from flask_basicauth import BasicAuth
import time
import threading
from multiprocessing import Process
import RPi.GPIO as GPIO
from time import sleep
email_update_interval = 600 # sends an email only once in this time interval
video_camera = VideoCamera(flip=True) # creates a camera object, flip vertically
object_classifier = cv2.CascadeClassifier("models/facial_recognition_model.xml") # an opencv classifier
# App Globals (do not edit)
app = Flask(__name__)
app.config['BASIC_AUTH_USERNAME'] = 'nitin'
app.config['BASIC_AUTH_PASSWORD'] = 'password'
app.config['BASIC_AUTH_FORCE'] = True
basic_auth = BasicAuth(app)
last_epoch = 0
def check_for_objects():
global last_epoch
while True:
try:
frame, found_obj = video_camera.get_object(object_classifier)
if found_obj and (time.time() - last_epoch) > email_update_interval:
last_epoch = time.time()
print ("Sending email...")
sendEmail(frame)
print ("done!")
except:
print ("Error sending email: ", sys.exc_info()[0])
def check_gas_leak():
channel = 15
GPIO.setmode(GPIO.BOARD)
GPIO.setup(channel, GPIO.IN)
def callback(channel):
sendAlertEmail()
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, callback) # assign function to GPIO PIN, Run function on change
# infinite loop
while True:
time.sleep(1)
@app.route('/')
@basic_auth.required
def index():
return render_template('index.html')
def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route('/video_feed')
def video_feed():
return Response(gen(video_camera),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route("/home")
def nitin():
return render_template('new.html')
@app.route("/servoOn/")
def snmain():
GPIO.setmode(GPIO.BOARD)
servoPIN = 12
GPIO.setup(servoPIN, GPIO.OUT)
p = GPIO.PWM(servoPIN, 50)
p.start(2.5)
time.sleep(1.5)
p.ChangeDutyCycle(2.5)
time.sleep(1.5)
GPIO.cleanup()
return render_template('new.html')
@app.route("/servoOff/")
def sfmain():
GPIO.setmode(GPIO.BOARD)
servoPIN = 12
GPIO.setup(servoPIN, GPIO.OUT)
p = GPIO.PWM(servoPIN, 50)
p.start(2.5)
time.sleep(1.5)
p.ChangeDutyCycle(12.5)
time.sleep(1.5)
GPIO.cleanup()
return render_template('new.html')
@app.route("/ledOn/")
def lnmain():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(29, GPIO.OUT)
GPIO.setup(31, GPIO.OUT)
GPIO.setup(33, GPIO.OUT)
GPIO.setup(37, GPIO.OUT)
GPIO.output(29, True)
GPIO.output(31, True)
GPIO.output(33, True)
GPIO.output(37,True)
return render_template('new.html')
@app.route("/ledOff/")
def lfmain():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(29, GPIO.OUT)
GPIO.setup(31, GPIO.OUT)
GPIO.setup(33, GPIO.OUT)
GPIO.setup(37, GPIO.OUT)
GPIO.output(29, False)
GPIO.output(31, False)
GPIO.output(33, False)
GPIO.output(37,False)
return render_template('new.html')
@app.route("/fanOff/")
def fanfmain():
Motor1B = 16
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.output(Motor1B,GPIO.LOW)
return render_template('new.html')
@app.route("/fanOn/")
def fanmain():
Motor1B = 16
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.output(Motor1B,GPIO.HIGH)
sleep(5)
return render_template('new.html')
@app.route("/notify/")
def usmain():
channel = 15
GPIO.setmode(GPIO.BOARD)
GPIO.setup(channel, GPIO.IN)
def callback(channel):
flameStatus='Fire!'
usStatus='open'
templateData={'title' : 'Status Check!',
'ultraSonic' : usStatus,
'flameSensor': flameStatus
}
return render_template('alert.html', **templateData)
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, callback) # assign function to GPIO PIN, Run function on change
# infinite loop
while True:
time.sleep(1)
if __name__ == '__main__':
t = threading.Thread(target=check_for_objects, args=())
t.daemon = True
t.start()
app.run(host='0.0.0.0', debug=False)