55# license which can be found in the file 'LICENSE.md' in this package distribution
66
77import time
8+ import sys
89
910
1011def use_existing_network_connection ():
@@ -16,18 +17,30 @@ def get_mac():
1617 return '' .join (['{:02x}' .format ((uuid .getnode () >> ele ) & 0xff ) for ele in range (0 , 8 * 6 , 8 )][::- 1 ]).upper ()
1718
1819
20+ CONN_TIMEOUT = 20
21+
22+
1923def wifi_connect (ssid , password ):
2024 from network import WLAN , STA_IF
2125
2226 sta_if = WLAN (STA_IF )
2327 sta_if .active (True )
24- while not sta_if .isconnected ():
28+ start_time = time .time ()
29+ while True :
2530 print ('Connecting to network...' )
26- sta_if .connect (ssid , password )
27- if sta_if .isconnected ():
28- break
29- time .sleep (2 )
30- print ('Network config:' , sta_if .ifconfig ())
31+ try :
32+ sta_if .connect (ssid , password )
33+ time .sleep_ms (3000 )
34+ if sta_if .isconnected ():
35+ print ("WiFi connected successfully." )
36+ print ('Network config:' , sta_if .ifconfig ())
37+ break
38+ elif time .time () - start_time > CONN_TIMEOUT :
39+ print ("[ERROR] Wi-Fi not connected. Stopped." )
40+ sys .exit ()
41+ except OSError :
42+ print ("[ERROR] Wi-Fi not connected. Stopped." )
43+ sys .exit ()
3144
3245
3346def get_esp_mac ():
@@ -36,26 +49,23 @@ def get_esp_mac():
3649 return binascii .hexlify (WLAN ().config ('mac' )).decode ('ascii' ).upper ()
3750
3851
39- CONN_TIMEOUT = 20
40-
41-
4252def pycom_wifi_connect (ssid , password , hostname ):
4353 from network import WLAN
4454
4555 wlan = WLAN (mode = WLAN .STA )
4656 wlan .hostname (hostname )
4757 start_time = time .time ()
48- while 1 :
58+ while True :
4959 print ("Trying to connect..." )
5060 wlan .connect (ssid = ssid , auth = (WLAN .WPA2 , password ))
5161 time .sleep_ms (3000 )
5262 if wlan .isconnected ():
53- print ("WiFi connected successfully" )
63+ print ("WiFi connected successfully. " )
5464 print ('IPs:' , wlan .ifconfig (), 'Channel:' , wlan .channel ())
5565 break
5666 elif time .time () - start_time > CONN_TIMEOUT :
57- print ("WiFi not connected. Stopped." )
58- break
67+ print ("[ERROR] Wi-Fi not connected. Stopped." )
68+ sys . exit ()
5969
6070
6171def get_pycom_mac ():
0 commit comments