|
4 | 4 | import gc
|
5 | 5 | import network
|
6 | 6 | import time
|
| 7 | +import json |
7 | 8 |
|
8 | 9 | logging.log_file = "webserverLog.txt"
|
9 | 10 |
|
@@ -32,20 +33,57 @@ def __init__(self):
|
32 | 33 | self.FUNCTION_SUFFIX = "endfunction"
|
33 | 34 | self.display_arrows = False
|
34 | 35 |
|
35 |
| - def start_network(self, robot_id:int): |
| 36 | + def start_network(self, ssid:str=None, robot_id:int= None, password:str=None): |
36 | 37 | """
|
37 |
| - Open an access point from the XRP board to be used as a captive host. The network password is "remote.xrp" |
| 38 | + Open an access point from the XRP board to be used as a captive host. The default network information can be set in secrets.json |
| 39 | +
|
| 40 | + :param ssid: The ssid of the access point, defaults to value from secrets.json |
| 41 | + :type ssid: str, optional |
| 42 | + :param robot_id: Replaces "{robot_id}" in ssid, defaults to value from secrets.json |
| 43 | + :type robot_id: int, optional |
| 44 | + :param password: The password of the access point, defaults to value from secrets.json |
| 45 | + :type password: str, optional |
38 | 46 | """
|
39 |
| - self.access_point = access_point(f"XRP_{robot_id}", "remote.xrp") |
| 47 | + if ssid is None: |
| 48 | + try: |
| 49 | + with open("../../secrets.json") as secrets_file: |
| 50 | + secrets = json.load(secrets_file) |
| 51 | + ssid = str(secrets["ap_ssid"]) |
| 52 | + password = str(secrets["ap_password"]) |
| 53 | + if robot_id is None: |
| 54 | + robot_id = str(secrets["robot_id"]) |
| 55 | + ssid = ssid.replace("{robot_id}", robot_id) |
| 56 | + except (OSError, KeyError, ValueError): |
| 57 | + if robot_id is None: |
| 58 | + robot_id = 1 |
| 59 | + ssid = f"XRP_{robot_id}" |
| 60 | + password = "remote.xrp" |
| 61 | + self.access_point = access_point(ssid, password) |
40 | 62 | self.ip = network.WLAN(network.AP_IF).ifconfig()[0]
|
41 | 63 |
|
42 |
| - def connect_to_network(self, ssid:str, password:str, timeout = 10): |
| 64 | + def connect_to_network(self, ssid:str=None, password:str=None, timeout = 10): |
43 | 65 | """
|
44 | 66 | Connect to a wifi network with the given ssid and password.
|
45 | 67 | If the connection fails, the board will disconnect from the network and return.
|
| 68 | +
|
| 69 | + :param ssid: The ssid of the network, defaults to value from secrets.json |
| 70 | + :type ssid: str, optional |
| 71 | + :param password: The password of the network, defaults to value from secrets.json |
| 72 | + :type password: str, optional |
| 73 | + :param timeout: The amount of time to wait for the connection to succeed, defaults to 10 |
| 74 | + :type timeout: int, optional |
46 | 75 | """
|
47 | 76 | self.wlan = network.WLAN(network.STA_IF)
|
48 | 77 | self.wlan.active(True) # configure board to connect to wifi
|
| 78 | + if ssid is None: |
| 79 | + try: |
| 80 | + with open("../../secrets.json") as secrets_file: |
| 81 | + secrets = json.load(secrets_file) |
| 82 | + ssid = str(secrets["wifi_ssid"]) |
| 83 | + password = str(secrets["wifi_password"]) |
| 84 | + except (OSError, KeyError, ValueError): |
| 85 | + print("secrets.json not found or improperly formatted") |
| 86 | + return False |
49 | 87 | self.wlan.connect(ssid,password)
|
50 | 88 | start_time = time.time()
|
51 | 89 | while not self.wlan.isconnected():
|
|
0 commit comments