-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
115 lines (84 loc) · 2.65 KB
/
app.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
from flask import Flask, render_template, request, redirect, url_for
from gpiozero import Button, LED, Buzzer
import Adafruit_DHT
from time import sleep
from threading import Thread
app = Flask(__name__)
# Initialize GPIO components and variables
led_state_out = False
led_state_in = False
led_state_ac = False
inside_light = LED(17) # Yellow LED
ac_indicator = LED(18) # Red LED
outside_light = LED(22) # Green LED
inside_button = Button(23)
ac_button = Button(25)
outside_button = Button(2)
buzzer = Buzzer(16)
sensor = Adafruit_DHT.DHT11
temp_pin = 21
temperature = None
# Function to toggle outside light
def toggle_outside():
global led_state_out
led_state_out = not led_state_out
outside_light.toggle()
# Function to toggle inside light
def toggle_inside():
global led_state_in
led_state_in = not led_state_in
inside_light.toggle()
# Function to toggle AC and sound buzzer
def toggle_ac():
global led_state_ac
led_state_ac = not led_state_ac
ac_indicator.toggle()
buzzer.on()
sleep(0.5)
buzzer.off()
def temperature_check():
sensor = Adafruit_DHT.DHT11
pin = 21
humidity, temperature = Adafruit_DHT.read_retry(sensor,pin)
# print("Temperature: {}".format(temperature))
return temperature
temp_value= temperature_check()
# Function to read temperature and humidity
def read_temperature_humidity():
global temperature
global humidity
while True:
humidity, temp = Adafruit_DHT.read_retry(sensor, temp_pin)
if temp is not None:
temperature = temp
#humidity =hum
def ac_auto_toggle():
if temp_value>=29.0 :
toggle_ac()
inside_button.when_pressed = toggle_inside
outside_button.when_pressed = toggle_outside
ac_button.when_pressed =toggle_ac
ac_auto_toggle()
# Start a thread to continuously read temperature and humidity
temp_thread = Thread(target=read_temperature_humidity)
temp_thread.daemon = True
temp_thread.start()
# Routes for the web interface
@app.route('/', methods=['GET', 'POST'])
def index():
led_states=[led_state_out,led_state_in,led_state_ac]
return render_template('index.html',led_states= led_states,temperature=temperature, humidity=humidity)
@app.route('/toggle_outside', methods=['POST'])
def toggle_outside_route():
toggle_outside()
return redirect("/")
@app.route('/toggle_inside', methods=['POST'])
def toggle_inside_route():
toggle_inside()
return redirect("/")
@app.route('/toggle_ac', methods=['POST'])
def toggle_ac_route():
toggle_ac()
return redirect("/")
if __name__ == '__main__':
app.run(debug= True, host='0.0.0.0' , port = 5001)