Raspberry Pi 4 Hotspot

Hey guys! I've written down here whatever I did while trying to setup a toy product that I was making which I thought would be the start of cheap home robotics. Well, business aside, there was some really cool stuff that I did. One of them was setting up auto hotspot for the RPi, which would help customers write the home wifi credentials into the RPi. Once the WiFi SSID has been written, the RPi (Product) restarts and starts running the system service scripts.

I’ll walk you through the entire process using a couple of bash scripts that I developed during my startup. These scripts make your Pi switch between a WiFi client and a hotspot, which is super handy for initial configuration when you ship a device to customers.

Setting Up the Pi as a Hotspot

When your Raspberry Pi boots up and can’t find a known WiFi network, it should set itself up as a WiFi hotspot. This allows users to connect directly to the Pi and configure it through a custom interface (like an Android app).

Here’s the script for setting up the Raspberry Pi as a hotspot:

Install Necessary Packages

First, we need to install hostapd and dnsmasq. These are essential for creating the WiFi hotspot. Now what is hostapd? Its a simple software that lets us convert a network interface into a WiFi access point. It supports protocols like WPA, WPA2 & WPA3. Essentially, hostapd is what enables the device(in this case, the Raspberry PI) to act as a central connection point for other wireless devices.

Next is the dnsmasq - this is a lightweight easy to configure DNS forwarder and DHCP server. It is designed to provide DNS services to a small scale network. It can be configured to automatically assign IP addresses to client devices and handle their DNS requests.

sudo apt install hostapd dnsmasq -y
if [ $? -ne 0 ]; then
    echo “Error installing packages. Exiting.”
    exit 1
fi

Stop Services

Before making changes, stop the services to avoid conflicts.

sudo systemctl stop hostapd
sudo systemctl stop dnsmasq

Configure Network and DHCP

Set a static IP for the wlan interface and configure dnsmasq for IP address management.

# Configure dhcpcd
sudo cp /etc/dhcpcd.conf /etc/dhcpcd.conf.backup
printf "\ninterface wlan0\nstatic ip_address=192.168.4.1/24\nnohook wpa_supplicant" | sudo tee -a /etc/dhcpcd.conf

# Configure dnsmasq
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
printf “interface=wlan0\ndhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h” | sudo tee /etc/dnsmasq.conf

Configure and Start Hostapd

Set up the hotspot’s SSID and security settings.

Replace Hotspot Name with your own, like “My Product” or something and give it a passphrase, same goes for HotspotPassword

# Configure hostapd
printf “interface=wlan0\ndriver=nl80211\nssid=HotspotName\nhw_mode=g\nchannel=7\nwmm_enabled=0\nmacaddr_acl=0\nauth_algs=1\nignore_broadcast_ssid=0\nwpa=2\nwpa_passphrase=HotspotPassword\nwpa_key_mgmt=WPA-PSK\nwpa_pairwise=TKIP\nrsn_pairwise=CCMP” | sudo tee /etc/hostapd/hostapd.conf

# Update and start hostapd
sudo sed -i 's/#DAEMON_CONF=""/DAEMON_CONF="\/etc\/hostapd\/hostapd.conf"/' /etc/default/hostapd
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl start hostapd

Masking a service in systemd effectively disables it so that it cannot be started manually or automatically, regardless of any attempts to start it (whether by dependencies, system reboots, or manual intervention). When a service is masked, its service unit file is symbolically linked to /dev/null, making it completely unstartable. So unmasking the service removes this link and restores the original service unit file. This allows the service to be managed again, meaning it can be started, stopped, or enabled/disabled as required.

Enable Routing and NAT

Allow connected devices to access the internet through the Pi.

# Enable IP forwarding
sudo sed -i '/^#net.ipv4.ip_forward=1/s/^#//' /etc/sysctl.conf
sudo sysctl -p

# Set up NAT
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo sh -c “iptables-save > /etc/iptables.ipv4.nat”

Make sure to handle the persistence of these settings across reboots, which might involve editing /etc/rc.local or another startup script depending on your system configuration.

Switching Back to Normal WiFi Client Mode

Once the user configures the network credentials through the hotspot, we need to revert the changes and connect the Pi as a normal WiFi client.

# Stop the hotspot services
sudo systemctl stop hostapd
sudo systemctl stop dnsmasq

# Restore previous configurations
sudo sed -i '/interface wlan0/,+2d' /etc/dhcpcd.conf
sudo mv /etc/dnsmasq.conf.orig /etc/dnsmasq.conf
sudo sed -i 's/DAEMON_CONF="\/etc\/hostapd\/hostapd.conf"/#DAEMON_CONF=""/' /etc/default/hostapd

# Disable IP forwarding and remove NAT rules
sudo sed -i '/^net.ipv4.ip_forward=1/s/^/#/' /etc/sysctl.conf
sudo sysctl -p
sudo iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
sudo sh -c “iptables-save > /etc/iptables.ipv4.nat”

# Restart networking services
sudo systemctl restart dhcpcd

And thats it! With these scripts, you can make your Raspberry Pi-based products a lot more user-friendly when it comes to initial setup.

Both the complete scripts are available here

the two scripts discussed in this blog are setup_access_point.sh and scrap_access_point.sh

I hope this guide helps you understand and implement the WiFi setup for your own projects. Happy coding and innovating!

Cheers!

— Rishi Banerjee
September 2023