@@ -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" )
0 commit comments