Skip to content

Commit e60c0c4

Browse files
committed
run as access point
1 parent faf4521 commit e60c0c4

File tree

2 files changed

+66
-26
lines changed

2 files changed

+66
-26
lines changed

gurgleapps_webserver.py

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ def __init__(self, wifi_ssid, wifi_password, port=80, timeout=20, doc_root="/www
3030
self.function_routes = []
3131
self.log_level = log_level
3232
# wifi client in station mode so we can connect to an access point
33-
self.wlan = network.WLAN(network.STA_IF)
33+
self.wlan_sta = network.WLAN(network.STA_IF)
34+
self.wlan_ap = network.WLAN(network.AP_IF)
3435
# activate the interface
35-
self.wlan.active(True)
36+
#self.wlan.active(True)
3637
# connect to the access point with the ssid and password
37-
self.wlan.connect(self.wifi_ssid, self.wifi_password)
38+
#self.wlan.connect(self.wifi_ssid, self.wifi_password)
3839
self.html = """<!DOCTYPE html>
3940
<html>
4041
<head> <title>GurgleApps.com Webserver</title> </head>
@@ -43,33 +44,56 @@ def __init__(self, wifi_ssid, wifi_password, port=80, timeout=20, doc_root="/www
4344
</body>
4445
</html>
4546
"""
46-
counter = self.timeout
47-
while counter > 0:
48-
if self.wlan.status() < 0 or self.wlan.status() >= 3:
49-
break
50-
counter -= 1
51-
print('waiting for connection...')
52-
time.sleep(1)
47+
# counter = self.timeout
48+
# while counter > 0:
49+
# if self.wlan_sta.status() < 0 or self.wlan_sta.status() >= 3:
50+
# break
51+
# counter -= 1
52+
# print('waiting for connection...')
53+
# time.sleep(1)
5354

5455
# if self.wlan.status() != 3:
55-
if self.wlan.isconnected() == False:
56-
raise RuntimeError('network connection failed')
56+
# if self.wlan_sta.isconnected() == False:
57+
# raise RuntimeError('network connection failed')
58+
# else:
59+
# print('connected')
60+
# status = self.wlan_sta.ifconfig()
61+
# print('ip = ' + status[0])
62+
if self.connect_wifi(self.wifi_ssid, self.wifi_password):
63+
print('point your browser to http://', self.ip_address)
5764
else:
58-
print('connected')
59-
status = self.wlan.ifconfig()
60-
print('ip = ' + status[0])
65+
raise RuntimeError('network connection failed')
6166
self.server_running = False
62-
self.ip_address = status[0]
63-
print('point your browser to http://', status[0])
64-
# asyncio.new_event_loop()
65-
print("exit constructor")
67+
# self.ip_address = status[0]
68+
69+
70+
def connect_wifi(self, ssid, password):
71+
# Deactivate AP mode
72+
self.wlan_ap.active(False)
73+
# Activate Wi-Fi mode and connect
74+
self.wlan_sta.active(True)
75+
self.wlan_sta.connect(ssid, password)
76+
# Wait for connection
77+
print("Connecting to Wi-Fi...")
78+
for _ in range(self.timeout):
79+
time.sleep(1)
80+
if self.wlan_sta.isconnected():
81+
self.ip_address = self.wlan_sta.ifconfig()[0]
82+
print(f"Connected to Wi-Fi. IP: {self.ip_address}")
83+
return True
84+
print("Failed to connect to Wi-Fi.")
85+
return False
86+
87+
def connect_access_point(self, ssid, password=None):
88+
# Deactivate Wi-Fi mode
89+
self.wlan_sta.active(False)
90+
# Activate AP mode
91+
self.wlan_ap.config(essid=ssid, password=password)
92+
self.wlan_ap.active(True)
93+
self.ip_address = self.wlan_ap.ifconfig()[0]
94+
print(f"AP Mode started. SSID: {ssid}, IP: {self.ip_address}")
95+
return True
6696

67-
# async def start_server(self):
68-
# print("start_server")
69-
# asyncio.create_task(asyncio.start_server(
70-
# self.serve_request, "0.0.0.0", 80))
71-
# while self.serving:
72-
# await asyncio.sleep(0.1)
7397

7498
async def start_server(self):
7599
print("start_server")

main.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ async def stop_server(request, response):
7878
await server.stop_server()
7979
shutdown = True
8080

81+
async def run_as_access_point(request, response):
82+
print("Running as access point")
83+
success = server.connect_access_point('gurgleapps', 'gurgleapps')
84+
if success:
85+
await response.send_html("Running as access point")
86+
else:
87+
await response.send_html("Failed to run as access point")
88+
8189

8290
async def main():
8391
global shutdown
@@ -93,7 +101,14 @@ async def main():
93101
led.off()
94102
await asyncio.sleep(0.2)
95103

96-
server = GurgleAppsWebserver(config.WIFI_SSID, config.WIFI_PASSWORD, port=80, timeout=20, doc_root="/www", log_level=2)
104+
server = GurgleAppsWebserver(
105+
config.WIFI_SSID,
106+
config.WIFI_PASSWORD,
107+
port=80,
108+
timeout=20,
109+
doc_root="/www",
110+
log_level=2
111+
)
97112
server.add_function_route("/set-delay/<delay>", set_delay)
98113
server.add_function_route(
99114
"/set-blink-pattern/<on_time>/<off_time>",
@@ -105,6 +120,7 @@ async def main():
105120
server.add_function_route("/example/func/<param1>/<param2>", example_func)
106121
server.add_function_route("/hello/<name>", say_hello)
107122
server.add_function_route("/stop-server", stop_server)
123+
server.add_function_route("/run-as-access-point", run_as_access_point)
108124

109125
asyncio.run(server.start_server_with_background_task(main))
110126
print('DONE')

0 commit comments

Comments
 (0)