wifi switcher

seamlessly choose between saved wifi options on cardputer boot. write ssid and password combinations in settings.toml then just add them to the (list of) list "wifi_options". the menu will adjust accordingly so add as many or as few as you like. menu will in theory go off the screen if it is a long list
This commit is contained in:
2026-02-04 21:32:37 +00:00
parent c61dc3e301
commit 0094d9502c

View File

@@ -0,0 +1,146 @@
from keyb import Keyboard # type: ignore
import time, os, rtc, json, wifi, supervisor # type: ignore
import board, busio, sdcardio, storage, terminalio, displayio # type: ignore
from adafruit_display_text import label # type: ignore
from adafruit_display_shapes.rect import Rect # type: ignore
#import pytumblr # type: ignore
import tumblrapi # type: ignore
### DEFAULT HOME PAGE # # # # # # # # # #
def home():
title_label.text = "Home"
title_label.x = 10
title_label.y = 14
ip_label.text = my_ip
ip_label.x = 160
ip_label.y = 20
page_label.text = """
1 - User Info
2 - Make Post
"""
page_label.x = 5
page_label.y = 30
# # # # # # # # # # # # # # # # # #
keyb = Keyboard()
# SET UP DISPLAY & WIFI
display = board.DISPLAY
display_group = displayio.Group()
display.root_group = display_group
wifi_options = [[os.getenv("WIFI1"),os.getenv("WIFIPW1")],
[os.getenv("WIFI2"),os.getenv("WIFIPW2")],
[os.getenv("WIFI3"),os.getenv("WIFIPW3")]]
h1 = label.Label(terminalio.FONT, text="Hello!", color=0xffffff, scale=2)
h1.x = 10
h1.y = 15
count = 0
menu_labs = []
for option in wifi_options:
if count == 0:
menu_lab = label.Label(terminalio.FONT, text = "> " + option[0], color=0xffffff, scale=1)
else:
menu_lab = label.Label(terminalio.FONT, text = option[0], color=0xffffff, scale=1)
menu_lab.x = 10
menu_lab.y = 35 + count * 10
display.root_group.append(menu_lab)
menu_labs.append(menu_lab)
count+=1
display.root_group.append(h1)
pos = 0
while True:
key = keyb.scan()
if key == "UP":
if pos == 0:
pass
else:
menu_labs[pos].text = wifi_options[pos][0]
pos -= 1
menu_labs[pos].text = "> " + wifi_options[pos][0]
elif key == "DOWN":
if pos == len(wifi_options)-1:
pass
else:
menu_labs[pos].text = wifi_options[pos][0]
pos += 1
menu_labs[pos].text = "> " + wifi_options[pos][0]
elif key == "\n":
WIFI_SSID = wifi_options[pos][0]
WIFI_PASS = wifi_options[pos][1]
break
# SET UP WIFI
try:
wifi.radio.connect(ssid=WIFI_SSID,
password=WIFI_PASS)
my_ip = str(wifi.radio.ipv4_address).strip()
display.root_group.remove(h1)
for lab in menu_labs:
display.root_group.remove(lab)
except ConnectionError as e:
txt = label.Label(terminalio.FONT, text = "", color=0xff0000, scale=1)
txt.text = "Could not connect to " + WIFI_SSID
txt.x = 10
txt.y = 100
display.root_group.append(txt)
time.sleep(10)
supervisor.reload()
# # # # # # # # # # # # # # # # # #
# SET UP SD CARD
spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
cs = board.SD_CS
try:
sdcard = sdcardio.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd") # access files on sd card here
except OSError:
pass # SD card not inserted/found
# Header Bar
rect = Rect(0, 0, 238, 30, fill=0x1f4476)
display.root_group.append(rect)
# Page Title
title_font = terminalio.FONT
title_color = 0xffffff
title_label = label.Label(title_font, text="Home", color=title_color, scale=2)
display.root_group.append(title_label)
# Page Text
page_font = terminalio.FONT
page_color = 0xffffff
page_label = label.Label(page_font, text="page_text", color=page_color, scale=1)
display.root_group.append(page_label)
# IP Label
ip_font = terminalio.FONT
ip_color = 0x89a0a8
ip_label = label.Label(ip_font, text="0.0.0.0", color=ip_color, scale=1)
display.root_group.append(ip_label)
home()
## PROGRAM MAIN LOOP (WORKS FROM HOME PAGE IF OTHER PAGES HAVE THEIR OWN LOOPS)
while True:
key = keyb.scan()