@@ -31,15 +31,35 @@ def __init__(self):
31
31
self .FUNCTION_SUFFIX = "endfunction"
32
32
self .display_arrows = False
33
33
34
- def start_server (self , robot_number :int ):
34
+ def start_network (self , robot_number :int ):
35
35
"""
36
- Start the webserver. The network password is "remote.xrp"
37
-
38
- :param robot_number: The number of the robot, used to generate the access point name
39
- :type robot_number: int
36
+ Open an access point from the XRP board to be used as a captive host. The network password is "remote.xrp"
40
37
"""
41
38
self .access_point = access_point (f"XRP_{ robot_number } " , "remote.xrp" )
42
39
self .ip = network .WLAN (network .AP_IF ).ifconfig ()[0 ]
40
+
41
+ def connect_to_network (self , ssid :str , password :str , timeout = 10 ):
42
+ """
43
+ Connect to a wifi network with the given ssid and password.
44
+ If the connection fails, the board will disconnect from the network and return.
45
+ """
46
+ self .wlan = network .WLAN (network .STA_IF )
47
+ self .wlan .active (True ) # configure board to connect to wifi
48
+ self .wlan .connect (ssid ,password )
49
+ start_time = time .time ()
50
+ while not self .wlan .isconnected ():
51
+ print ("Connecting to network, may take a second" )
52
+ if time .time () > start_time + timeout :
53
+ print ("Failed to connect to network, please try again" )
54
+ self .wlan .disconnect ()
55
+ return False
56
+ time .sleep (0.25 )
57
+ self .ip = self .wlan .ifconfig ()[0 ]
58
+
59
+ def start_server (self ):
60
+ """
61
+ Begin the webserver on whatever network is configured, at the ip 'remote.xrp'
62
+ """
43
63
logging .info (f"Starting DNS Server at { self .ip } " )
44
64
dns .run_catchall (self .ip )
45
65
server .run ()
0 commit comments