Skip to content

Commit 6e5855d

Browse files
authored
Merge pull request #42 from Open-STEM/WifiSecrets
Webserver secrets.json file
2 parents 9dea3b6 + 00d6431 commit 6e5855d

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.vscode/
2-
.git/
2+
.git/
3+
4+
secrets.json

Examples/webserver_example.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ def log_time_and_range():
3333
timer = Timer(-1)
3434
timer.init(freq=4, mode=Timer.PERIODIC, callback=lambda t: log_time_and_range())
3535

36-
def connect_and_start_webserver(ssid, password, robot_id):
36+
def connect_and_start_webserver():
3737
# Connect to the network and start the webserver in bridge mode
38-
webserver.connect_to_network(ssid, password, robot_id)
38+
# Network ssid and password are stored in root/secrets.json
39+
webserver.connect_to_network()
3940
webserver.start_server()
4041

41-
def start_network_and_webserver(robot_id):
42+
def start_network_and_webserver():
4243
# Start the webserver in access point mode
43-
webserver.start_network(robot_id)
44+
# Network ssid and password are stored in root/secrets.json
45+
webserver.start_network()
4446
webserver.start_server()

XRPLib/webserver.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import gc
55
import network
66
import time
7+
import json
78

89
logging.log_file = "webserverLog.txt"
910

@@ -32,20 +33,57 @@ def __init__(self):
3233
self.FUNCTION_SUFFIX = "endfunction"
3334
self.display_arrows = False
3435

35-
def start_network(self, robot_id:int):
36+
def start_network(self, ssid:str=None, robot_id:int= None, password:str=None):
3637
"""
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
3846
"""
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)
4062
self.ip = network.WLAN(network.AP_IF).ifconfig()[0]
4163

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):
4365
"""
4466
Connect to a wifi network with the given ssid and password.
4567
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
4675
"""
4776
self.wlan = network.WLAN(network.STA_IF)
4877
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
4987
self.wlan.connect(ssid,password)
5088
start_time = time.time()
5189
while not self.wlan.isconnected():

secrets.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"wifi_ssid": "YOUR_WIFI_SSID",
3+
"wifi_password": "YOUR_WIFI_PASSWORD",
4+
"robot_id": 1,
5+
"ap_ssid": "XRP {robot_id}",
6+
"ap_password": "remote.xrp"
7+
}

0 commit comments

Comments
 (0)