Skip to content

Instantly share code, notes, and snippets.

@tomasinouk
Last active November 14, 2025 05:31
Show Gist options
  • Select an option

  • Save tomasinouk/683bcd4fb669dbefbc8a0aaf21702453 to your computer and use it in GitHub Desktop.

Select an option

Save tomasinouk/683bcd4fb669dbefbc8a0aaf21702453 to your computer and use it in GitHub Desktop.
WiFi STA and AP switching for application

Scripts and configuration snippets are stored in no volatile directory. /var/data/upload_wifi/

Step 1

so you need to create the directory first: mkdir /var/data/upload_wifi/

Step 2

Now we need to create scripts which will handle switching OFF and ON the WiFi AP, when the STA will connect and disconnect.

Create configuration snippets for enabling and disabling WiFi AP

file enable_ap.cfg:

WIFI_AP_ENABLED=1

file disable_ap.cfg:

WIFI_AP_ENABLED=0

Create logic scripts, which will be run when WiFi STA connects and disconnect.

file 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"                    
fi

file 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"                               
fi

Safeguard just in case we start without AP and STA does not connect

file 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                                                                    
done

Step 3

Configure router to use the created scripts. Navigate into Configuration --> Scripts --> Up/Down IPv4

Add below to Up Script

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.sh

Add below to Down Script

logger ">>>> 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
image

Navigate to Configuration --> Scripts --> Startup

Add below to Startup Script

/var/data/upload_wifi/check_wifi_ap_incase.sh &
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment