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
- If the robot is connected to the network, I must be able to connect to it via SSH using
ssh user@machine.local
. - If the connection is lost, a hotspot named
HotspotFallback
must start automatically. - Once connected to this hotspot, I must be able to access the robot via
ssh user@machine.local
orssh user@192.168.42.1
. - If Wi-Fi comes back, the hotspot must stop and the robot must reconnect to the initial network.
- 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
Enable SSH connection
1 2
sudo apt update sudo apt instal openssh-server
Check connection
1 2 3
hostname # Displays machine name whoami # Displays user name ssh user@machine.local # SSH connection test
Secure with firewall (UFW)
1 2
sudo apt install ufw sudo ufw allow ssh
Creating the fallback script
Install dependencies
sudo apt install iw dnsmasq network-manager
Create the script
nano ~/hotspot_switcher.sh
- Copy the script content from:
https://github.com/dclause/hotspot-fallback/blob/develop/hotspot_switcher.sh
- Configure script variables
WIFI_INTERFACE
→ obtained viaip link | grep wl
WIFI_ID
→ exact name of the main Wi-Fi network to connect toHOTSPOT_SSID
etHOTSPOT_PASSWORD
→ hotspot SSID and passwordHOTSPOT_CON_NAME
→ hotspot connection nameHOTSPOT_IP
→ fixed IP in hotspot mode
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
Create a SystemD service
sudo nano /etc/systemd/system/wifi-hotspot-check.service
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
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
Create the timer file
sudo nano /etc/systemd/system/wifi-hotspot-check.timer
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
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:
Create the
dnsmasq
configsudo nano /etc/dnsmasq.conf
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
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.
Add new comment