You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
wificonf: add auto_connect.py
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
# Automatically connect to the WiFi, based on the saved networks
|
||||
|
||||
import network
|
||||
import ujson
|
||||
import os
|
||||
import time
|
||||
|
||||
|
||||
access_points={}
|
||||
|
||||
|
||||
def load_config():
|
||||
print("load_config: Checking for /data directory")
|
||||
try:
|
||||
os.stat('/data')
|
||||
print("load_config: /data exists")
|
||||
except OSError:
|
||||
print("load_config: Creating /data directory")
|
||||
os.mkdir('/data')
|
||||
print("load_config: Checking for /data/com.example.wificonf directory")
|
||||
try:
|
||||
os.stat('/data/com.example.wificonf')
|
||||
print("load_config: /data/com.example.wificonf exists")
|
||||
except OSError:
|
||||
print("load_config: Creating /data/com.example.wificonf directory")
|
||||
os.mkdir('/data/com.example.wificonf')
|
||||
print("load_config: Loading config from conf.json")
|
||||
try:
|
||||
with open('/data/com.example.wificonf/conf.json','r') as f:
|
||||
global access_points
|
||||
access_points=ujson.load(f)
|
||||
print(f"load_config: Loaded access_points: {access_points}")
|
||||
except OSError:
|
||||
access_points={}
|
||||
print("load_config: No config file found, using empty access_points")
|
||||
|
||||
|
||||
def auto_connect():
|
||||
# TODO: scan for wifi networks first, and only try to connect to the ones that are found
|
||||
print("auto_connect: Attempting to connect to known networks")
|
||||
for ssid,password in access_points.items():
|
||||
print(f"auto_connect: Trying SSID: {ssid}")
|
||||
if attempt_connecting(ssid,password):
|
||||
print(f"auto_connect: Connected to {ssid}")
|
||||
return True
|
||||
print("auto_connect: No known networks connected")
|
||||
return False
|
||||
|
||||
|
||||
def attempt_connecting(ssid,password):
|
||||
print(f"attempt_connecting: Attempting to connect to SSID: {ssid}")
|
||||
try:
|
||||
wlan.connect(ssid,password)
|
||||
for i in range(10):
|
||||
if wlan.isconnected():
|
||||
print(f"attempt_connecting: Connected to {ssid} after {i+1} seconds")
|
||||
return True
|
||||
print(f"attempt_connecting: Waiting for connection, attempt {i+1}/10")
|
||||
time.sleep(1)
|
||||
print(f"attempt_connecting: Failed to connect to {ssid}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"attempt_connecting: Connection error: {e}")
|
||||
return False
|
||||
|
||||
print("auto_connect.py running...")
|
||||
load_config()
|
||||
|
||||
wlan=network.WLAN(network.STA_IF)
|
||||
wlan.active(True)
|
||||
|
||||
if auto_connect():
|
||||
print("WiFi auto-connect managed to connect.")
|
||||
else:
|
||||
print("WiFi auto connect did not manage to connect.")
|
||||
@@ -88,18 +88,6 @@ def attempt_connecting(ssid,password):
|
||||
show_error("Connection failed")
|
||||
return False
|
||||
|
||||
def auto_connect():
|
||||
# scan for wifi networks
|
||||
# for each one that is known, try to connect
|
||||
print("auto_connect: Attempting to connect to known networks")
|
||||
for ssid,password in access_points.items():
|
||||
print(f"auto_connect: Trying SSID: {ssid}")
|
||||
if attempt_connecting(ssid,password):
|
||||
print(f"auto_connect: Connected to {ssid}")
|
||||
return True
|
||||
print("auto_connect: No known networks connected")
|
||||
return False
|
||||
|
||||
def show_error(message):
|
||||
print(f"show_error: Displaying error: {message}")
|
||||
global error_label
|
||||
|
||||
@@ -355,6 +355,10 @@ def start_app(app_dir, is_launcher=False):
|
||||
# Execute this if it exists
|
||||
execute_script_new_thread("/autorun.py", True, False, False)
|
||||
|
||||
# A generic "start at boot" mechanism hasn't been implemented yet, so do it like this:
|
||||
execute_script_new_thread("/builtin/apps/com.example.wificonf/assets/auto_connect.py", True, False, False)
|
||||
|
||||
|
||||
try:
|
||||
import freezefs_mount_builtin
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user