Skip to content

Commit

Permalink
Merge pull request #42 from Open-STEM/WifiSecrets
Browse files Browse the repository at this point in the history
Webserver secrets.json file
  • Loading branch information
bradamiller authored Jul 30, 2023
2 parents 9dea3b6 + 00d6431 commit 6e5855d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.vscode/
.git/
.git/

secrets.json
10 changes: 6 additions & 4 deletions Examples/webserver_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ def log_time_and_range():
timer = Timer(-1)
timer.init(freq=4, mode=Timer.PERIODIC, callback=lambda t: log_time_and_range())

def connect_and_start_webserver(ssid, password, robot_id):
def connect_and_start_webserver():
# Connect to the network and start the webserver in bridge mode
webserver.connect_to_network(ssid, password, robot_id)
# Network ssid and password are stored in root/secrets.json
webserver.connect_to_network()
webserver.start_server()

def start_network_and_webserver(robot_id):
def start_network_and_webserver():
# Start the webserver in access point mode
webserver.start_network(robot_id)
# Network ssid and password are stored in root/secrets.json
webserver.start_network()
webserver.start_server()
46 changes: 42 additions & 4 deletions XRPLib/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gc
import network
import time
import json

logging.log_file = "webserverLog.txt"

Expand Down Expand Up @@ -32,20 +33,57 @@ def __init__(self):
self.FUNCTION_SUFFIX = "endfunction"
self.display_arrows = False

def start_network(self, robot_id:int):
def start_network(self, ssid:str=None, robot_id:int= None, password:str=None):
"""
Open an access point from the XRP board to be used as a captive host. The network password is "remote.xrp"
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
:param ssid: The ssid of the access point, defaults to value from secrets.json
:type ssid: str, optional
:param robot_id: Replaces "{robot_id}" in ssid, defaults to value from secrets.json
:type robot_id: int, optional
:param password: The password of the access point, defaults to value from secrets.json
:type password: str, optional
"""
self.access_point = access_point(f"XRP_{robot_id}", "remote.xrp")
if ssid is None:
try:
with open("../../secrets.json") as secrets_file:
secrets = json.load(secrets_file)
ssid = str(secrets["ap_ssid"])
password = str(secrets["ap_password"])
if robot_id is None:
robot_id = str(secrets["robot_id"])
ssid = ssid.replace("{robot_id}", robot_id)
except (OSError, KeyError, ValueError):
if robot_id is None:
robot_id = 1
ssid = f"XRP_{robot_id}"
password = "remote.xrp"
self.access_point = access_point(ssid, password)
self.ip = network.WLAN(network.AP_IF).ifconfig()[0]

def connect_to_network(self, ssid:str, password:str, timeout = 10):
def connect_to_network(self, ssid:str=None, password:str=None, timeout = 10):
"""
Connect to a wifi network with the given ssid and password.
If the connection fails, the board will disconnect from the network and return.
:param ssid: The ssid of the network, defaults to value from secrets.json
:type ssid: str, optional
:param password: The password of the network, defaults to value from secrets.json
:type password: str, optional
:param timeout: The amount of time to wait for the connection to succeed, defaults to 10
:type timeout: int, optional
"""
self.wlan = network.WLAN(network.STA_IF)
self.wlan.active(True) # configure board to connect to wifi
if ssid is None:
try:
with open("../../secrets.json") as secrets_file:
secrets = json.load(secrets_file)
ssid = str(secrets["wifi_ssid"])
password = str(secrets["wifi_password"])
except (OSError, KeyError, ValueError):
print("secrets.json not found or improperly formatted")
return False
self.wlan.connect(ssid,password)
start_time = time.time()
while not self.wlan.isconnected():
Expand Down
7 changes: 7 additions & 0 deletions secrets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"wifi_ssid": "YOUR_WIFI_SSID",
"wifi_password": "YOUR_WIFI_PASSWORD",
"robot_id": 1,
"ap_ssid": "XRP {robot_id}",
"ap_password": "remote.xrp"
}

0 comments on commit 6e5855d

Please sign in to comment.