-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
263 lines (223 loc) · 7.66 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import machine
import network
import socket
import time
import wifi_credentials
from machine import Pin
import _thread
from html_template import HTML_TEMPLATE
# Access the SSID and password from the credentials module
ssid = wifi_credentials.SSID
password = wifi_credentials.PASSWORD
relay1 = Pin(19, Pin.OUT) #change according to your need
relay2 = Pin(20, Pin.OUT) #change according to your need
relay3 = Pin(21, Pin.OUT) #change according to your need
relay1.value(0)
relay2.value(0)
relay3.value(0)
relayState = "Relays are OFF"
led = Pin("LED", Pin.OUT)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('connected')
status = wlan.ifconfig()
print('ip = ' + status[0])
for i in range(6):
led.toggle()
time.sleep_ms(200)
# Function to get RSSI
def get_wifi_strength():
if wlan.isconnected():
rssi = wlan.status('rssi')
return rssi
else:
return None
# Print WiFi signal strength
signal_strength = get_wifi_strength()
if signal_strength is not None:
print("WiFi Signal Strength:", signal_strength)
else:
print("WiFi not connected")
addr = socket.getaddrinfo('0.0.0.0', 1025)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
loop_running = False
global_timer_running = False
global_timer_stop = False
def loop_relay(on_duration, off_duration, relay):
global loop_running
loop_running = True
while loop_running:
relay.value(0)
time.sleep(on_duration)
relay.value(1)
time.sleep(off_duration)
print("Loop stopped")
def stop_loop():
global loop_running
loop_running = False
print("Stopping loop...")
def start_loop(on_duration, off_duration):
global loop_running
if not loop_running:
_thread.start_new_thread(loop_relay, (on_duration, off_duration, relay3))
print("Loop started")
else:
print("Loop is already running")
def global_timer(hours, minutes):
global global_timer_running, loop_running, global_timer_stop
global_timer_running = True
global_timer_stop = False
total_seconds = (hours * 3600) + (minutes * 60)
for _ in range(total_seconds):
if global_timer_stop:
break
time.sleep(1)
# Stop the relay loop if running
if loop_running:
stop_loop()
time.sleep(1) # Ensure the loop stops completely before turning off relays
# Turn off all relays
relay1.value(1)
relay2.value(1)
relay3.value(1)
global_timer_running = False
def stop_global_timer():
global global_timer_stop
global_timer_stop = True
def reset_all():
global loop_running, global_timer_stop, global_timer_running
if loop_running:
stop_loop()
time.sleep(1) # Ensure the loop stops completely
if global_timer_running:
stop_global_timer()
time.sleep(1) # Ensure the global timer stops completely
relay1.value(1)
relay2.value(1)
relay3.value(1)
loop_running = False
global_timer_running = False
global_timer_stop = False
def parse_query(query_string):
params = {}
pairs = query_string.split('&')
for pair in pairs:
key, value = pair.split('=')
params[key] = value
return params
def turn_all_relays(on):
if on:
relay1.value(0)
relay2.value(0)
relay3.value(0)
return "All relays are ON"
else:
relay1.value(1)
relay2.value(1)
relay3.value(1)
return "All relays are OFF"
while True:
try:
cl, addr = s.accept()
print('client connected from', addr)
request = cl.recv(1024)
print(request)
request = str(request)
relay1_on = request.find('/relay1/on')
relay1_off = request.find('/relay1/off')
relay2_on = request.find('/relay2/on')
relay2_off = request.find('/relay2/off')
relay3_on = request.find('/relay3/on')
relay3_off = request.find('/relay3/off')
relays_on = request.find('/relays/on')
relays_off = request.find('/relays/off')
loop = request.find('/loop')
stop_loop_request = request.find('/stop-loop')
global_timer_request = request.find('/global-timer')
stop_global_timer_request = request.find('/stop-global-timer')
reset_request = request.find('/reset')
if relay1_on == 6:
print("relay 1 on")
relay1.value(0)
relayState = "Relay 1 is ON"
elif relay1_off == 6:
print("relay 1 off")
relay1.value(1)
relayState = "Relay 1 is OFF"
if relay2_on == 6:
print("relay 2 on")
relay2.value(0)
relayState = "Relay 2 is ON"
elif relay2_off == 6:
print("relay 2 off")
relay2.value(1)
relayState = "Relay 2 is OFF"
if relay3_on == 6:
print("relay 3 on")
relay3.value(0)
relayState = "Relay 3 is ON"
elif relay3_off == 6:
print("relay 3 off")
relay3.value(1)
relayState = "Relay 3 is OFF"
if relays_on == 6:
relayState = turn_all_relays(True)
elif relays_off == 6:
relayState = turn_all_relays(False)
if loop == 6:
params_start = request.find('?') + 1
params_end = request.find(' ', params_start)
query_string = request[params_start:params_end]
params = parse_query(query_string)
on_duration = int(params.get('on-duration', 1)) * 60
off_duration = int(params.get('off-duration', 1)) * 60
stop_loop() # Stop the loop before starting a new one
start_loop(on_duration, off_duration)
relayState = f"Loop started with on duration {on_duration//60} min and off duration {off_duration//60} min"
if stop_loop_request == 6:
stop_loop()
relayState = "Pump loop stopped"
if global_timer_request == 6:
params_start = request.find('?') + 1
params_end = request.find(' ', params_start)
query_string = request[params_start:params_end]
params = parse_query(query_string)
hours = int(params.get('hours', 0))
minutes = int(params.get('minutes', 0))
if global_timer_running:
stop_global_timer()
time.sleep(1) # Ensure the previous timer stops completely
_thread.start_new_thread(global_timer, (hours, minutes))
relayState = f"Global timer set for {hours} hours and {minutes} minutes"
if stop_global_timer_request == 6:
stop_global_timer()
relayState = "Global timer stopped"
if reset_request == 6:
reset_all()
relayState = "All settings reset"
response = HTML_TEMPLATE % relayState
cl.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n')
cl.send(response)
cl.close()
except OSError as e:
cl.close()
print('connection closed')
except Exception as e:
print('Error:', str(e))
cl.close()