How to improve the performance of a Raspberry Pi zero-based Wi-Fi access point

I need to create a Wi-Fi access point for my car to use as a redundant connection while traveling.

I’ve already tried using a pocket 4G Wi-Fi router, but it takes my phone a few minutes to realize that the Wi-Fi network it’s connected to has lost its internet connection.
Hence the idea of ​​creating a Wi-Fi router capable of disabling Wi-Fi if it loses its internet connection.

For development, I used the code in this answer as a basis and am running it on a Raspberry Pi Zero with Raspberry Pi OS.

When the system boots, the run.sh script is automatically launched, which pings the Google servers (8.8.8.8) every 5 seconds.

If the ping is successful, it activates Wi-Fi (start.sh), otherwise it deactivates it (stop.sh).

To avoid purchasing more powerful hardware, is it possible to further optimize the disconnection speed of Wi-Fi clients?

run.sh

#!/bin/bash

INTERFACE="lan1"
TARGET="8.8.8.8"

# State variable: false = stopped, true = running
is_running=false

echo "Monitoring started on $INTERFACE..."

while true; do
    # Check connectivity
    if ping -I "$INTERFACE" -c 1 -W 1 -w 1 "$TARGET" &> /dev/null; then
        
        # If internet is available BUT the service is not running yet
        if [ "$is_running" = false ]; then
            echo "$(date '+%H:%M:%S') - Internet OK: Starting service..."
            /home/vm/start.sh
            is_running=true
        else
            # Internet is available and the service is already running: do nothing
            echo "$(date '+%H:%M:%S') - Internet OK: Service already running"
        fi

    else
        # If internet is NOT available BUT the service is still running
        if [ "$is_running" = true ]; then
            echo "$(date '+%H:%M:%S') - Internet DOWN: Stopping service..."
            /home/vm/stop.sh
            is_running=false
        else
            # Internet down and service already stopped: do nothing
            echo "$(date '+%H:%M:%S') - Internet DOWN: Waiting for signal..."
        fi
    fi

    sleep 5
done

stop.sh

#!/bin/bash
service hostapd stop
service dnsmasq stop

start.sh

sudo rfkill unblock wlan
sudo systemctl stop wpa_supplicant
sudo systemctl mask wpa_supplicant
sudo ip link set wifi0 down
sudo ip link set wifi0 up
sudo sysctl -w net.ipv4.ip_forward=1
service hostapd start

# Forza IP statico sulla scheda Wi-Fi
sudo ip addr flush dev wifi0
sudo ip addr add 192.168.1.1/24 dev wifi0

service dnsmasq start

install.sh

#!/bin/bash

# Update package list and install required packages
# - hostapd: access point daemon
# - dnsmasq: DHCP and DNS server
# - iptables / iptables-persistent: firewall + persistence
apt-get update
apt-get install -y hostapd dnsmasq wireless-tools iw iptables iptables-persistent netfilter-persistent wvdial

# Configure hostapd to use the main configuration file
sed -i 's#^DAEMON_CONF=.*#DAEMON_CONF=/etc/hostapd/hostapd.conf#' /etc/init.d/hostapd

# Configure dnsmasq
cat <<EOF > /etc/dnsmasq.conf
# Log dnsmasq activity
log-facility=/var/log/dnsmasq.log

# Bind dnsmasq to the WiFi interface
interface=wifi0

# DHCP range for WiFi clients
dhcp-range=192.168.1.1,192.168.1.200,12h

# Default gateway for clients
dhcp-option=3,192.168.1.1

# Google DNS servers
dhcp-option=6,8.8.8.8,8.8.4.4

# Log DNS queries
log-queries
EOF

# Start dnsmasq to apply changes
service dnsmasq start

# Bring up WiFi interface and assign static IP
ifconfig wifi0 up
ifconfig wifi0 192.168.1.1/24

# Flush existing iptables rules
iptables -F
iptables -t nat -F

# Enable NAT (masquerading) on outgoing LAN interface
iptables -t nat -A POSTROUTING -o lan1 -j MASQUERADE

# Allow forwarding from WiFi to LAN
iptables -A FORWARD -i wifi0 -o lan1 -j ACCEPT

# Allow established/related traffic from LAN back to WiFi
iptables -A FORWARD -i lan1 -o wifi0 -m state --state RELATED,ESTABLISHED -j ACCEPT

# Enable IPv4 forwarding (runtime)
echo 1 > /proc/sys/net/ipv4/ip_forward

# Save iptables rules so they persist after reboot
netfilter-persistent save

# Configure hostapd (WiFi access point)
cat <<EOF > /etc/hostapd/hostapd.conf
# Wireless interface used by the access point
interface=wifi0

# Driver type
driver=nl80211

# WiFi channel
channel=1

# WiFi network name (SSID)
ssid=wifiCar

# Enable WPA2 security
wpa=2

# WiFi password
wpa_passphrase=myPassword

# Key management and encryption
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP

# Group key rekey interval (seconds)
wpa_group_rekey=600

# Master key rekey interval (seconds)
wpa_gmk_rekey=86400
EOF

# Restart and enable services
systemctl enable hostapd
systemctl enable dnsmasq