Skip to content

Commit 8e1c113

Browse files
committed
reconnect wifi if it is down
1 parent b87040f commit 8e1c113

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

gurgleapps_webserver.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def __init__(self, wifi_ssid, wifi_password, port=80, timeout=20, doc_root="/www
2626
self.timeout = timeout
2727
self.wifi_ssid = wifi_ssid
2828
self.wifi_password = wifi_password
29+
self.ap_ssid = None
30+
self.ap_password = None
2931
self.doc_root = doc_root
3032
self.function_routes = []
3133
self.log_level = log_level
@@ -68,6 +70,8 @@ def __init__(self, wifi_ssid, wifi_password, port=80, timeout=20, doc_root="/www
6870

6971

7072
def connect_wifi(self, ssid, password):
73+
self.wifi_ssid = ssid
74+
self.wifi_password = password
7175
# Deactivate AP mode
7276
self.wlan_ap.active(False)
7377
if self.wlan_sta.isconnected():
@@ -97,11 +101,20 @@ def start_access_point(self, ssid, password=None):
97101
# self.wlan_sta.active(False)
98102
# Set the IP configuration for the AP mode
99103
#self.wlan_ap.ifconfig((ip, subnet, gateway, dns))
104+
self.ap_ssid = ssid
105+
self.ap_password = password
100106
self.wlan_ap.config(essid=ssid, password=password)
101107
self.wlan_ap.active(True)
102108
self.ip_address = self.wlan_ap.ifconfig()[0]
103109
print(f"AP Mode started. SSID: {ssid}, IP: {self.ip_address}")
104110
return True
111+
112+
async def maintain_connection(self):
113+
while True:
114+
if self.wlan_sta.isconnected() == False:
115+
print("Lost connection to Wi-Fi. Attempting to reconnect...")
116+
self.connect_wifi(self.wifi_ssid, self.wifi_password)
117+
await asyncio.sleep(10)
105118

106119

107120
async def start_server(self):
@@ -119,7 +132,11 @@ async def stop_server(self):
119132

120133
async def start_server_with_background_task(self, background_task):
121134
async def main():
122-
await asyncio.gather(self.start_server(), background_task())
135+
await asyncio.gather(
136+
self.start_server(),
137+
background_task(),
138+
self.maintain_connection()
139+
)
123140
await main()
124141

125142

0 commit comments

Comments
 (0)