Runtime Wi-Fi configuration with fallback

wifi-set changes the network without reflashing (hex-encoded SSID),
wifi-connect tries new -> previous -> build default, and a switch that
fails reverts automatically.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 22:01:43 +01:00
co-authored by Claude Opus 5.5
parent 89bd21d1f2
commit 28d4a36b6b
3 changed files with 86 additions and 0 deletions

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
#!/bin/sh
# wifi-connect [--switch]: bring wlan0 up on the first config that associates.
# Order: /data/wpa_supplicant.conf, its .prev, /etc/config/wpa_supplicant.conf,
# /usr/local/wpa_default.conf (build-time). If none connects, stay on the first one
# (wpa_supplicant keeps retrying, e.g. router still booting after a power cut).
PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin
try() {
killall udhcpc wpa_supplicant 2>/dev/null; sleep 1
wpa_supplicant -B -iwlan0 -Dnl80211 -c "$1" || return 1
i=0; while [ $i -lt ${2:-30} ]; do
[ "$(wpa_cli -iwlan0 status 2>/dev/null | sed -n 's/^wpa_state=//p')" = COMPLETED ] && return 0
sleep 1; i=$((i+1)); done
return 1
}
LIST=
for c in /data/wpa_supplicant.conf /data/wpa_supplicant.conf.prev /etc/config/wpa_supplicant.conf /usr/local/wpa_default.conf; do
[ -f "$c" ] && LIST="$LIST $c"
done
[ -n "$LIST" ] || { echo "[wifi] no network configured (put wifi.txt on the SD card)"; exit 1; }
FIRST=; OK=
for c in $LIST; do
[ -f "$c" ] || continue # .prev may have been consumed by a revert
[ -n "$FIRST" ] || FIRST=$c
echo "[wifi] trying $c"
if try "$c" 40; then OK=$c; break; fi
echo "[wifi] $c did not connect"
# wifi-set ... now: a new network that fails is reverted immediately and retried
if [ "$1" = --switch ] && [ "$c" = /data/wpa_supplicant.conf ] && [ -f /data/wpa_supplicant.conf.prev ]; then
mv -f /data/wpa_supplicant.conf /data/wpa_supplicant.conf.failed
mv -f /data/wpa_supplicant.conf.prev /data/wpa_supplicant.conf; sync
echo "[wifi] reverted to previous network (bad one kept as .failed)"
FIRST=/data/wpa_supplicant.conf
if try /data/wpa_supplicant.conf 40; then OK=/data/wpa_supplicant.conf; break; fi
fi
done
if [ -z "$OK" ]; then
echo "[wifi] nothing connected; staying on $FIRST and retrying in background"
killall wpa_supplicant 2>/dev/null; sleep 1
wpa_supplicant -B -iwlan0 -Dnl80211 -c "$FIRST"
else
echo "[wifi] connected using $OK"
fi
udhcpc -i wlan0 -b
ifconfig wlan0
+13
View File
@@ -0,0 +1,13 @@
#!/bin/sh
# wifi-mkconf SSID [PSK] -> wpa_supplicant.conf on stdout. SSID is hex-encoded so
# any character (quotes, spaces, unicode) is safe; PSK quoted (wpa_supplicant takes the last ").
hex=$(printf '%s' "$1" | hexdump -v -e '/1 "%02x"')
[ -n "$hex" ] || exit 1
echo "ctrl_interface=/var/run/wpa_supplicant"
echo "update_config=0"
echo "network={"
echo " #ssid=\"$(printf '%s' "$1" | tr -d '\n')\""
echo " ssid=$hex"
echo " scan_ssid=1"
if [ -n "$2" ]; then echo " psk=\"$2\""; else echo " key_mgmt=NONE"; fi
echo "}"
+29
View File
@@ -0,0 +1,29 @@
#!/bin/sh
# wifi-set: change the Wi-Fi network without reflashing.
# wifi-set "SSID" "PASSWORD" save; used from next boot
# wifi-set "SSID" "" open network (no password)
# wifi-set "SSID" "PASSWORD" now save + switch immediately (SSH will drop; if the new
# network doesn't connect in 40s it reverts automatically)
# wifi-set --show | --forget show current network / delete saved network (-> default)
# Saved to /data/wpa_supplicant.conf; previous one kept as .prev. Boot tries:
# new -> previous -> build-time default, first that connects wins.
CONF=/data/wpa_supplicant.conf
case "$1" in
--show) for c in $CONF /usr/local/wpa_default.conf; do [ -f $c ] && { echo "$c:"; grep -E '^\s*(ssid|#ssid)' $c; }; done
wpa_cli -iwlan0 status 2>/dev/null | grep -E '^(ssid|wpa_state|ip_address)='; exit 0 ;;
--forget) rm -f $CONF $CONF.prev; echo "saved network removed (build default used next boot)"; exit 0 ;;
""|-h|--help) sed -n '2,10p' "$0"; exit 2 ;;
esac
SSID=$1; PSK=$2
[ ${#SSID} -ge 1 ] && [ ${#SSID} -le 32 ] || { echo "SSID must be 1-32 bytes"; exit 1; }
[ -z "$PSK" ] || { [ ${#PSK} -ge 8 ] && [ ${#PSK} -le 63 ]; } || { echo "password must be 8-63 chars (or empty for open)"; exit 1; }
/usr/sbin/wifi-mkconf "$SSID" "$PSK" > $CONF.new || exit 1
[ -f $CONF ] && cp -f $CONF $CONF.prev
mv -f $CONF.new $CONF; sync
echo "saved network \"$SSID\" to $CONF"
if [ "$3" = now ]; then
echo "switching now; if it fails the camera reverts to the previous network within ~40s"
setsid /usr/sbin/wifi-connect --switch </dev/null >/tmp/wifi-switch.log 2>&1 &
else
echo "takes effect on next boot (reboot), or rerun with 'now' as 3rd arg"
fi