Setting up a fallback hotspot script in case of Wi-Fi disconnection

I’m currently preparing a conference in London entitled “Rust and Robotics.” For this occasion, I need to bring some of my robots for live demonstrations. The issue: I don’t have access to Wi-Fi ahead of the event to configure auto-network connection.

Use case

Imagine you have a robot, controllable remotely via a self-hosted interface (like Hermes-Studio), or a computer accessible via SSH. As long as you’re on a known network (home, office), everything works fine. But what happens if the network goes down? Or if you have to move your robot to an unknown place, without any network coverage? And what if you don’t have physical access to the computer, or the robot has neither screen nor control peripherals? You’re stuck.

That’s the problem we’re going to address: a script will check the connection every minute. In case of disconnection, it will automatically spawn a Wi-Fi hotspot, allowing you to reconnect to the robot — either to control it, or to configure a new network.

Goals

  1. If the robot is connected to the network, I must be able to connect to it via SSH using ssh user@machine.local.
  2. If the connection is lost, a hotspot named HotspotFallback must start automatically.
  3. Once connected to this hotspot, I must be able to access the robot via ssh user@machine.local or ssh user@192.168.42.1.
  4. If Wi-Fi comes back, the hotspot must stop and the robot must reconnect to the initial network.
  5. I must be able to manually switch between Wi-Fi and hotspot.

Installation

We’ll use NetworkManager (nmcli) to manage the connection, not hostapd like in older tutorials. For DNS resolution in hotspot mode, we’ll also use dnsmasq.

Prerequisites

  1. Enable SSH connection

    1
    2
    
    sudo apt update
    sudo apt instal openssh-server
  2. Check connection

    1
    2
    3
    
    hostname                  # Displays machine name  
    whoami                    # Displays user name  
    ssh user@machine.local    # SSH connection test
  3. Secure with firewall (UFW)

    1
    2
    
    sudo apt install ufw
    sudo ufw allow ssh

Creating the fallback script

  1. Install dependencies

    sudo apt install iw dnsmasq network-manager
  2. Create the script

    nano ~/hotspot_switcher.sh
  3. Copy the script content from:
    https://github.com/dclause/hotspot-fallback/blob/develop/hotspot_switcher.sh
In the section "Configuration Variables", adapt the values to your setup.
  1. Configure script variables
    • WIFI_INTERFACE → obtained via ip link | grep wl
    • WIFI_ID → exact name of the main Wi-Fi network to connect to
    • HOTSPOT_SSID et HOTSPOT_PASSWORD → hotspot SSID and password
    • HOTSPOT_CON_NAME → hotspot connection name
    • HOTSPOT_IP → fixed IP in hotspot mode
  2. Make the script executable

    chmod +x ~/hotspot_switcher.sh

Manual usage

Activate hotspot:

sudo ~/hotspot_switcher.sh start

Return to Wi-Fi:

sudo ~/hotspot_switcher.sh stop

Automatic usage

  1. Create a SystemD service

    sudo nano /etc/systemd/system/wifi-hotspot-check.service
  2. Edit content (WARNING: update the absolute path to your script)

    1
    2
    3
    4
    5
    6
    
    [Unit]
    Description=Checks main Wi-Fi connection  
     
    [Service]
    Type=oneshot
    ExecStart=/home/user/hotspot_switcher.sh check
  3. Start the service

    1
    2
    3
    
    sudo systemctl daemon-reexec
    sudo systemctl daemon-reload
    sudo systemctl start wifi-hotspot-check.service

Periodic check via Timer

  1. Create the timer file

    sudo nano /etc/systemd/system/wifi-hotspot-check.timer
  2. Edit content

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    [Unit]
    Description=Triggers Wi-Fi check every 60s  
     
    [Timer]
    OnBootSec=1min
    OnUnitActiveSec=1min
    Persistent=true
     
    [Install]
    WantedBy=timers.target
  3. Start the timer

    1
    2
    3
    
    sudo systemctl daemon-reexec
    sudo systemctl daemon-reload
    sudo systemctl start wifi-hotspot-check.timer

Configure DNS in hotspot mode

Without a router, you must manage DNS resolution yourself. This is what allows you in Hotspot mode to use the machine name instead of the IP for your SSH connection: ssh user@machine.local

However, in Hotspot mode, we no longer benefit from the DNS service of our home box or network provider. Our robot or remote computer must resolve DNS aliases itself, so we need to configure this:

  1. Create the dnsmasq config

    sudo nano /etc/dnsmasq.conf
  2. Edit the config (WARNING: update the network interface)

    1
    2
    3
    4
    5
    6
    
    interface=wlp2s0
    dhcp-range=192.168.42.100,192.168.42.200,12h
    dhcp-option=3,192.168.42.1
    dhcp-option=6,8.8.8.8,8.8.4.4
    server=8.8.8.8
    server=8.8.4.4
  3. Reboot the machine

    sudo reboot

Conclusion

Your machine is now capable of automatically switching to hotspot mode if the Wi-Fi connection is lost. This guarantees remote recovery via SSH.

WARNING: A machine cannot simultaneously be a Wi-Fi client and a hotspot. It is therefore essential to test this script in advance, because if Wi-Fi fails without a working fallback, you lose access.

Add new comment

Your name will be publicly displayed along with your comment.
Your email will be kept private and only used to notify you.
On internet, you can be who you want. Please be someone nice :)