by default, the Raspberry Pi's network management system (wpa_supplicant
or NetworkManager
, depending on your setup) will automatically attempt to connect to known Wi-Fi networks during boot. If you’ve configured known networks in the wpa_supplicant.conf
file or using NetworkManager or raspi-config
, the Pi will try to connect to these networks without requiring additional scanning logic in a script.
The key idea here is to detect whether the Pi successfully connected to any network and, if not, start the hotspot. This simplifies the script, as you don't need to handle Wi-Fi scanning yourself—just check the connection status.
This script assumes the Raspberry Pi will attempt to connect to known networks on its own. It checks if the Pi is connected to a network, and if not, starts the hotspot.
#!/bin/bash
# Wi-Fi hotspot configuration
HOTSPOT_SSID="printit"
STATIC_IP="192.168.4.1"
DHCP_CONFIG="/etc/dhcpcd.conf"
HOTSPOT_CONFIG="/etc/dhcpcd_hotspot.conf"
DEFAULT_CONFIG="/etc/dhcpcd_default.conf"
function backup_default_config() {
# Back up the original DHCP configuration if not already backed up
if [ ! -f "$DEFAULT_CONFIG" ]; then
echo "Backing up default DHCP configuration..."
sudo cp "$DHCP_CONFIG" "$DEFAULT_CONFIG"
fi
}
function configure_static_ip() {
echo "Configuring static IP for hotspot..."
sudo bash -c "cat > $HOTSPOT_CONFIG" <<EOL
interface wlan0
static ip_address=$STATIC_IP/24
nohook wpa_supplicant
EOL
sudo cp "$HOTSPOT_CONFIG" "$DHCP_CONFIG"
sudo systemctl restart dhcpcd
}
function configure_dhcp() {
echo "Reverting to DHCP configuration..."
if [ -f "$DEFAULT_CONFIG" ]; then
sudo cp "$DEFAULT_CONFIG" "$DHCP_CONFIG"
sudo systemctl restart dhcpcd
else
echo "Default DHCP configuration not found. Skipping..."
fi
}
function is_connected() {
echo "Checking Wi-Fi connection status..."
if ip addr show wlan0 | grep -q "inet "; then
echo "Wi-Fi is connected."
return 0
else
echo "Wi-Fi is not connected."
return 1
fi
}
function start_hotspot() {
echo "Starting Wi-Fi hotspot..."
configure_static_ip
# Stop conflicting services
sudo systemctl stop hostapd dnsmasq
# Configure dnsmasq
sudo bash -c "cat > /etc/dnsmasq.conf" <<EOL
interface=wlan0
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
EOL
# Configure hostapd for an open network
sudo bash -c "cat > /etc/hostapd/hostapd.conf" <<EOL
interface=wlan0
driver=nl80211
ssid=$HOTSPOT_SSID
hw_mode=g
channel=7
wmm_enabled=0
auth_algs=1
ignore_broadcast_ssid=0
EOL
sudo sed -i 's|#DAEMON_CONF=""|DAEMON_CONF="/etc/hostapd/hostapd.conf"|' /etc/default/hostapd
# Enable NAT
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
sudo bash -c "echo 'iptables-restore < /etc/iptables.ipv4.nat' >> /etc/rc.local"
# Restart services
sudo systemctl restart dnsmasq
sudo systemctl unmask hostapd
sudo systemctl restart hostapd
echo "Open hotspot started: SSID=$HOTSPOT_SSID"
}
function main() {
backup_default_config
is_connected
if [ $? -eq 0 ]; then
echo "Wi-Fi is connected. Reverting to DHCP configuration."
configure_dhcp
exit 0
fi
echo "Wi-Fi is not connected. Starting open hotspot..."
start_hotspot
}
main
-
Install Required Packages: Ensure
hostapd
anddnsmasq
are installed:sudo apt update && sudo apt install -y hostapd dnsmasq
-
Save and Make the Script Executable: Save the script as
/usr/local/bin/wifi_hotspot_manager.sh
:sudo nano /usr/local/bin/wifi_hotspot_manager.sh chmod +x /usr/local/bin/wifi_hotspot_manager.sh
-
Run Script on Startup: Add the script to the system's startup sequence:
sudo crontab -e
Add the following line:
@reboot /usr/local/bin/wifi_hotspot_manager.sh
-
Reboot to Test: Restart your Raspberry Pi:
sudo reboot
- Default Wi-Fi Handling: The Raspberry Pi attempts to connect to known networks as per its default behavior.
- Connection Check: The script checks if the
wlan0
interface has a valid IP address. - Hotspot Activation: If no IP address is assigned (no connection), the script starts the hotspot.
This approach leverages the Pi's default Wi-Fi scanning behavior, simplifying the logic.