5
5
# license which can be found in the file 'LICENSE.md' in this package distribution
6
6
7
7
import time
8
+ import sys
8
9
9
10
10
11
def use_existing_network_connection ():
@@ -16,18 +17,30 @@ def get_mac():
16
17
return '' .join (['{:02x}' .format ((uuid .getnode () >> ele ) & 0xff ) for ele in range (0 , 8 * 6 , 8 )][::- 1 ]).upper ()
17
18
18
19
20
+ CONN_TIMEOUT = 20
21
+
22
+
19
23
def wifi_connect (ssid , password ):
20
24
from network import WLAN , STA_IF
21
25
22
26
sta_if = WLAN (STA_IF )
23
27
sta_if .active (True )
24
- while not sta_if .isconnected ():
28
+ start_time = time .time ()
29
+ while True :
25
30
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 ()
31
44
32
45
33
46
def get_esp_mac ():
@@ -36,26 +49,23 @@ def get_esp_mac():
36
49
return binascii .hexlify (WLAN ().config ('mac' )).decode ('ascii' ).upper ()
37
50
38
51
39
- CONN_TIMEOUT = 20
40
-
41
-
42
52
def pycom_wifi_connect (ssid , password , hostname ):
43
53
from network import WLAN
44
54
45
55
wlan = WLAN (mode = WLAN .STA )
46
56
wlan .hostname (hostname )
47
57
start_time = time .time ()
48
- while 1 :
58
+ while True :
49
59
print ("Trying to connect..." )
50
60
wlan .connect (ssid = ssid , auth = (WLAN .WPA2 , password ))
51
61
time .sleep_ms (3000 )
52
62
if wlan .isconnected ():
53
- print ("WiFi connected successfully" )
63
+ print ("WiFi connected successfully. " )
54
64
print ('IPs:' , wlan .ifconfig (), 'Channel:' , wlan .channel ())
55
65
break
56
66
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 ()
59
69
60
70
61
71
def get_pycom_mac ():
0 commit comments