OpenWrt Wi-Fi Failover and Idle Shutdown for Raspberry Pi

This follow-up to my Raspberry Pi OpenWrt travel router guide adds two practical automations: upstream Wi-Fi recovery and idle shutdown. The router keeps its current connection while it works. After a disconnect, it checks eligible saved upstream Wi-Fi profiles; if no device uses its private Wi-Fi for 15 minutes, it powers off.

Project Highlights

  • Upstream Wi-Fi recovery: Keeps a working connection. If disconnected, scans for configured saved profiles and switches to one in range without reloading the local AP.
  • Idle Auto-Poweroff: To save energy and reduce component wear, the device automatically shuts down after 15 minutes of inactivity (no connected Wi-Fi clients).

Core Features Explained

1. Intelligent Network Roaming

The script automatically discovers saved station-mode Wi-Fi profiles assigned to wwan on radio2. You can add another upstream network through LuCI without editing a hard-coded profile list in roam.sh. It leaves a working upstream connection alone.

How it works:

  1. Connection event: The hotplug script watches wwan. An ifdown or ifup-failed event schedules a roaming check after five seconds.
  2. Periodic check: Cron runs /usr/bin/roam.sh every three minutes. The script exits immediately if connected. Otherwise it scans for a saved network.
  3. Profile selection: If the enabled profile is visible, OpenWrt is allowed to reconnect. Otherwise the script discovers eligible saved station profiles from OpenWrt’s wireless configuration, chooses the first visible profile, enables it, and reloads only radio2. This version does not use priority numbers. Its phy2-sta0 station and radio names are specific to this Pi.

2. Idle Auto-Poweroff

After 15 minutes with no clients connected to the private AP, the Pi shuts down. The included script checks phy1-ap0; use iw dev to verify that name on your own device.

  • Script: /usr/bin/idle-poweroff.sh
  • Trigger: Runs every minute via a cron job.
  • Logic: If the script detects zero connected clients, it starts a 15-minute timer. If a client connects, the timer is cancelled. If 15 minutes pass with no clients, it performs a final check and then powers off the device.

Download & Installation

The updated archive contains roam.sh, 99-roam-trigger, and idle-poweroff.sh. Download the updated OpenWrt roaming scripts ZIP.

Update: The revised roaming logic has now passed real-world field testing. The Raspberry Pi automatically switched from the home Wi-Fi profile to the saved work Wi-Fi profile on arrival, then switched back to the home Wi-Fi profile after returning home.

Installation Instructions

Unzip the current archive, place each script in the location below, run sh -n on each before installing it, and make it executable. In Windows PowerShell, use scp -O if OpenWrt lacks an SFTP server.

  1. Roaming Script:
    Place roam.sh in /usr/bin/ and run: chmod +x /usr/bin/roam.sh
  2. Roaming Trigger:
    Place 99-roam-trigger in /etc/hotplug.d/iface/ and run: chmod +x /etc/hotplug.d/iface/99-roam-trigger
  3. Idle Power-Off Script:
    Place idle-poweroff.sh in /usr/bin/ and run: chmod +x /usr/bin/idle-poweroff.sh

Configuration

Adding a New Wi-Fi Network

The updated version automatically detects saved station-mode profiles on radio2 assigned to the wwan network. You no longer need to edit a PROFILES list when adding another upstream Wi-Fi network. In Network → Wireless:

  1. Scan and join the new network as a client (station) on the correct radio device. Make sure it’s assigned to the correct firewall zone.
  2. Assign the new station profile to wwan and the upstream radio specified by RADIO in the script (in this example, radio2). The roaming script discovers it automatically; no manual profile-list edit is needed.
  3. Keep the profile for the current location enabled. Other profiles may be disabled; the script enables one when a switch is needed.
  4. Save and apply your changes.

Verifying Cron Jobs

Check scheduled jobs with crontab -l via SSH. The two scheduled scripts use:

*/3 * * * * /usr/bin/roam.sh
* * * * * /usr/bin/idle-poweroff.sh

Conclusion

These three scripts provide upstream connection recovery and idle shutdown. The roaming logic has now been field-tested between two locations: the Pi automatically switched to the saved work Wi-Fi when it came into range and switched back to the saved home Wi-Fi after returning home.