-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_home.py
47 lines (38 loc) · 997 Bytes
/
auto_home.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
from flask import Flask, render_template, request
import RPi.GPIO as GPIO
#GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
#GPIO.setup(7,GPIO.OUT)
app = Flask(__name__)
btns = {
3:"Light 1",
5:"Fan",
7:"Light 3",
11:"Light 4",
}
for i in btns:
GPIO.setup(i,GPIO.OUT)
GPIO.output(i,True)
@app.route('/')
def index():
# run RaspberryPi
return render_template('index.html', btns=btns)
@app.route('/checked/', methods=['POST'])
def checked():
btn_id = int(request.form['id'])
GPIO.output(btn_id,False)
return btns[btn_id] +" turned on successfully"
@app.route('/unchecked/', methods=['POST'])
def unchecked():
btn_id = int(request.form['id'])
GPIO.output(btn_id, True)
return btns[btn_id] +" turned off successfully"
try:
if __name__ == "__main__":
app.run(debug=True,host="0.0.0.0",port=80)
except:
GPIO.cleanup()
print("Error!")
finally:
GPIO.cleanup()
print("Exit")