-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
108 lines (89 loc) · 2.93 KB
/
controller.py
File metadata and controls
108 lines (89 loc) · 2.93 KB
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
"""
Air conditiner controller
"""
from datetime import datetime
import os
import time
from threading import Thread
from flask import Flask, render_template, redirect, url_for, jsonify
import Adafruit_DHT
app = Flask(__name__)
os.environ["POWER"] = "0"
os.environ['TEMPERATURE'] = "26"
os.environ['POWERFIGURE'] = "start-button"
os.environ['AIRCONAUTO'] = "0"
@app.route('/indoor_temperature')
def temperature_measurement():
_, indoor = Adafruit_DHT.read_retry(11, 27)
return jsonify(result=indoor)
@app.route('/')
def initial():
temperature = os.getenv('TEMPERATURE')
_, indoor = Adafruit_DHT.read_retry(11, 27)
power_figure = os.getenv('POWERFIGURE')
auto_mode = int(os.getenv('AIRCONAUTO'))
mode = 'AUTO' if auto_mode else ""
return render_template(
'controller.html',
setting=temperature,
indoor=indoor,
power=power_figure,
mode=mode)
@app.route('/power')
def power():
on_off = int(os.getenv('POWER'))
if on_off:
os.system('irsend SEND_ONCE aircon power_off')
os.environ["POWER"] = "0"
os.environ['POWERFIGURE'] = "start-button"
os.environ['AIRCONAUTO'] = "0"
else:
os.system('irsend SEND_ONCE aircon power_on')
os.environ["POWER"] = "1"
os.environ['POWERFIGURE'] = "start-button-on"
return redirect(url_for('initial'))
def temperature_detector():
_, indoor = Adafruit_DHT.read_retry(11, 27)
while indoor < 30 and os.getenv('POWER') == '0' and datetime.now(
).hour >= 3 and datetime.now().hour <= 5:
_, indoor = Adafruit_DHT.read_retry(11, 27)
time.sleep(10)
os.system('irsend SEND_ONCE aircon power_on')
os.environ["POWER"] = "1"
@app.route('/auto')
def auto():
on_off = int(os.getenv('AIRCONAUTO'))
if on_off:
os.environ["AIRCONAUTO"] = "0"
else:
os.environ["AIRCONAUTO"] = "1"
os.environ['POWERFIGURE'] = "start-button-on"
thread = Thread(target=temperature_detector)
thread.start()
return redirect(url_for('initial'))
def temp_limit(temp):
if temp < 18:
return 18
if temp > 30:
return 30
return temp
@app.route('/add')
def add_temperature():
on_off = int(os.getenv('POWER'))
if on_off:
temperature = int(os.getenv('TEMPERATURE'))
temperature = temp_limit(temperature + 1)
os.environ['TEMPERATURE'] = str(temperature)
os.system('irsend SEND_ONCE aircon aircon_{}'.format(temperature))
return redirect(url_for('initial'))
@app.route('/minus')
def minus_temperature():
on_off = int(os.getenv('POWER'))
if on_off:
temperature = int(os.getenv('TEMPERATURE'))
temperature = temp_limit(temperature - 1)
os.environ['TEMPERATURE'] = str(temperature)
os.system('irsend SEND_ONCE aircon aircon_{}'.format(temperature))
return redirect(url_for('initial'))
if __name__ == '__main__':
app.run(debug=True, port=5566, host='0.0.0.0')