Scripts and configuration snippets are stored in no volatile directory.
/var/data/upload_wifi/
so you need to create the directory first:
mkdir /var/data/upload_wifi/
Now we need to create scripts which will handle switching OFF and ON the WiFi AP, when the STA will connect and disconnect.
file enable_ap.cfg:
WIFI_AP_ENABLED=1file disable_ap.cfg:
WIFI_AP_ENABLED=0file check_wifi_ap.sh:
#!/bin/bash
CONFIG_FILE="/etc/settings.wifi_ap"
INTERFACE="wlan0"
# Check if config file exists
if [ ! -f "$CONFIG_FILE" ]; then
echo "Config file $CONFIG_FILE not found!"
exit 1
fi
# Check if WIFI_AP_ENABLED=0 is present
if grep -q "^WIFI_AP_ENABLED=0" "$CONFIG_FILE"; then
WIFI_DISABLED=true
else
WIFI_DISABLED=false
fi
# Check if wlan0 interface exists
if ip link show "$INTERFACE" | grep -q "state UP"; then
WLAN_EXISTS=true
else
WLAN_EXISTS=false
fi
# If both conditions are true, restart WiFi
if [ "$WIFI_DISABLED" = true ] && [ "$WLAN_EXISTS" = true ]; then
logger "$(date '+%Y-%m-%d %H:%M:%S') - Restarting WiFi service..."
# service wifi restart
# reboot router is better option
reboot
else
logger "$(date '+%Y-%m-%d %H:%M:%S') - Conditions not met: WIFI_AP_DISABLED=$WIFI_DISABLED, wlan0 state UP=$WLAN_EXISTS"
fifile check_wifi_ap_enable.sh:
#!/bin/bash
CONFIG_FILE="/etc/settings.wifi_ap"
INTERFACE="wlan0"
# Check if config file exists
if [ ! -f "$CONFIG_FILE" ]; then
echo "Config file $CONFIG_FILE not found!"
exit 1
fi
# Check if WIFI_AP_ENABLED=1 is present
if grep -q "^WIFI_AP_ENABLED=1" "$CONFIG_FILE"; then
WIFI_ENABLED=true
else
WIFI_ENABLED=false
fi
# Check if wlan0 interface exists
if ip link show "$INTERFACE" | grep -q "state DOWN"; then
WLAN_EXISTS=true
else
WLAN_EXISTS=false
fi
# If both conditions are true, restart WiFi
if [ "$WIFI_ENABLED" = true ] && [ "$WLAN_EXISTS" = true ]; then
logger "$(date '+%Y-%m-%d %H:%M:%S') - Restarting WiFi service...rebooting, ip-down"
# service wifi restart
# reboot is better option
reboot
else
logger "$(date '+%Y-%m-%d %H:%M:%S') - Conditions not met ip-down: WIFI_AP_ENABLED=$WIFI_ENABLED, wlan0=$WLAN_EXISTS"
fifile check_wifi_ap_incase.sh:
CONFIG_FILE="/etc/settings.wifi_ap"
INTERFACE="wlan1"
TARGET_IP="192.168.10.20"
# give me time after router starts
sleep 120
while true; do
# Check if config file exists
if [ ! -f "$CONFIG_FILE" ]; then
echo "Config file $CONFIG_FILE not found!"
sleep 300
continue
fi
# Check if WIFI_AP_ENABLED=0 is present
if grep -q "^WIFI_AP_ENABLED=0" "$CONFIG_FILE"; then
WIFI_DISABLED=true
else
WIFI_DISABLED=false
fi
# Get IPv4 of wlan1 (if any)
IP_ADDR=$(ip -4 addr show "$INTERFACE" | awk '/inet / {print $2}' | cut -d/ -f1)
# Determine WLAN_ACTIVE
if [ -z "$IP_ADDR" ]; then
WLAN_ACTIVE=true # No IP → OK
# elif [ "$IP_ADDR" = "$TARGET_IP" ]; then
# WLAN_ACTIVE=true # Expected IP → OK
else
WLAN_ACTIVE=false # Wrong IP
fi
# If both conditions are true, enable WiFi AP
if [ "$WIFI_DISABLED" = true ] && [ "$WLAN_ACTIVE" = true ]; then
logger "$(date '+%Y-%m-%d %H:%M:%S') - Enabling WiFi AP and rebooting..."
# restore /var/data/upload_wifi/enable_ap.cfg
reboot
else
logger "$(date '+%Y-%m-%d %H:%M:%S') - Conditions not met: WIFI_AP_DISABLED=$WIFI_DISABLED, wlan0 IP=${IP_ADDR:-none}"
fi
# Run every 5 minutes
sleep 300
doneConfigure router to use the created scripts.
Navigate into Configuration --> Scripts --> Up/Down IPv4
logger ">>>> Running for ip-up"
restore /var/data/upload_wifi/disable_ap.cfg
# need to restart wifi service to disable wifi AP
# need to wait for wifi to finish initiation
sleep 10
/var/data/upload_wifi/check_wifi_ap.shlogger ">>>> Running for ip-down"
restore /var/data/upload_wifi/enable_ap.cfg
# need to restart wifi service to disable wifi AP
# need to wait for wifi to finish initiation
sleep 10
/var/data/upload_wifi/check_wifi_ap_enable.sh
Navigate to Configuration --> Scripts --> Startup
/var/data/upload_wifi/check_wifi_ap_incase.sh &