Why a Wireless Access Point?
Although a Wireless Access Point (WAP) is fairly common and ubiquitious for connecting a laptop or personal device (phone, tablet) to the public Internet at home, coffee shops, restaurants, retail stores, etc., there are times when a WAP is not accessible or within range, or when access is not permitted (e.g., for security reasons), but there is a need to connect nearby wireless computers and devices to each other.
The Raspberry Pi (RPi) provides a low cost way to build a WAP using free open source software (although other similiar single board computers running Linux can also be used in a similar fashion). However, in addition to setting up a WAP for wireless connections, I also had a need for a network bridge to connect to a cluster of local Ethernet computers, as well as access to services on the computer acting as a WAP. What I am providing here are the steps that I took to combine:
o Wireless Access Point (WAP, for Wireless connections)
o DHCP Server (for assiging IP addresses)
o Network Bridge (for connecting Wireless with Ethernet)
o Access to computer running WAP (Wireless & Ethernet)
I have found instructions for each of these individually, but with some minor tweaks, I was able to get them all running together on the same Rapberry Pi.
In addition, since acting as a Wireless Access Point probably means that you are not connected to the public Internet, I am also including a custom solution (shell scripting) for switching the Raspberry Pi between a Wireless Access Point (WAP mode) and a regular WiFi client (WiFi mode, so that it can connect to the Internet through another WAP or Ethernet connection).
For hardware, I used a RPi model 4B running Raspberry Pi OS / Bullseye, but other Raspberry Pi models and Raspberry Pi OS versions should be compatible (specific commands and configuration are for Debian based Linux distributions, and translating to other flavours is left to the reader).
Network Topology
Although the network topogy can vary, this specific example uses the following configuration. Note that although this configuration shows a cluster of four (4) Ethernet connected Raspberry Pi computers, these of course could be any devices with an Ethernet connection, as the main objective is to demonstrate how to connect wireless devices to one or more wired Ethernet devices -- and each other -- through a Wireless Access Point.
Build a Raspberry Pi Wireless Access Point
1. Install Software for Wireless Access Point, Network Bridge and DHCP
$ sudo apt-get install hostapd bridge-utils dnsmasq
2. Edit the network configuration to prevent network assignment and permit bridging
$ sudo vi /etc/dhcpcd.conf
and then add:
denyinterfaces eth0
denyinterfaces wlan0
3. Create a new host access point configuration file (in the US)
$ sudo vi /etc/hostapd/hostapd.conf
and then add:
country_code=US
bridge=br0
interface=wlan0
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
ssid=MyAccessPoint <== Update for your desired name
wpa_passphrase=****** <== Add your password
4. Point to the new host access point configuration file
$ sudo vi /etc/default/hostapd
and then add:
DAEMON_CONF="/etc/hostapd/hostapd.conf"
5. Enble network fowarding
$ sudo vi /etc/sysctl.conf
and then uncomment (remove '#'):
net.ipv4.ip_forward=1
6. Define the bridge between wan0 and eth0
$ sudo brctl addbr br0
$ sudo brctl addif br0 eth0
$ sudo vi /etc/network/interfaces
and then add:
auto br0
iface br0 inet manual
bridge_ports eth0 wlan0
7. Add startup of the host access point and configuration of the network bridge at boot
$ sudo systemctl unmask hostapd
$ sudo vi /etc/rc.local
and then add (before exit 0):
echo "Starting as Wireless Access Point (WAP)"
systemctl start hostapd
ifconfig br0 192.168.4.110
route add default gw 192.168.4.110 br0
8. Edit the DHCP configuration to define IP network leases
$ sudo vi /etc/dnsmasq.conf
and then add:
# DHCP Leases (start IP, end IP, netmask, lifetime)
interface=br0
dhcp-range=192.168.4.101,192.168.4.109,255.255.255.0,24h
9. Reboot the Raspberry Pi
$ sudo reboot
10. Test the Wireless Access Point by connecting from a different WiFi computer and pinging 192.168.4.110
The wireless name and password will be the values used for ssid and wpa_passphrase respectively, in step #3 above
$ ping 192.168.4.110
Switch Raspberry Pi Between WAP and WiFi
To switch the Raspberry Pi between being a Wireless Access Point (WAP) and connecting to a WiFi network (at home, etc.) for other uses, and/or for updates, the following scripts may be used.
1. Create a script to switch between WAP and WiFi
$ mkdir -p $HOME/bin
$ touch $HOME/bin/network_mode
$ chmod +x $HOME/bin/network_mode
$ echo 'PATH=$HOME/bin:$PATH' >> $HOME/.bash_profile
$ . $HOME/.bash_profile
$ vi $HOME/bin/network_mode
Contents of network_mode script:
#! /usr/bin/bash
# network_mode {mode}
#
# Change the current network mode between WiFi and WAP
#
WAP=/etc/dhcpcd.conf.wap
WIFI=/etc/dhcpcd.conf.wifi
CONF=/etc/dhcpcd.conf
DNSWAP=/etc/resolv.conf.wap
DNSWIFI=/etc/resolv.conf.wifi
DNSCONF=/etc/resolv.conf
HOSTSWAP=/etc/hosts.wap
HOSTSWIFI=/etc/hosts.wifi
HOSTSCONF=/etc/hosts
if [ -z "$1" ]
then
echo ""
echo "Usage: network_mode [wap | wifi]"
echo ""
if [ -r ~pi/network_mode ]
then
mode=`cat ~pi/network_mode`
else
echo "Current mode is NOT DEFINED!"
exit 4
fi
if [ "$mode" = "wifi" ]
then
echo "Current mode is WiFi (Wireless 10.0.1.150)"
elif [ "$mode" = "wap" ]
then
echo "Current mode is WAP (Ethernet 192.168.4.201)"
else
echo "Current mode is UNDEFINED!"
fi
echo ""
elif [ "$1" = "wap" ]
then
diff $CONF $WAP > /dev/null
if [ $? -eq 0 ]
then
echo "Already configured for WAP"
exit 3
else
sudo cp $WAP $CONF
# sudo cp $DNSWAP $DNSCONF
# sudo cp $HOSTSWAP $HOSTSCONF
echo "wap" > ~pi/network_mode
echo -n "[Press Enter to Reboot]"
read ans
sudo reboot
fi
elif [ "$1" = "wifi" ]
then
diff $CONF $WIFI > /dev/null
if [ $? -eq 0 ]
then
echo "Already configured for WiFi"
exit 5
else
sudo cp $WIFI $CONF
# sudo cp $DNSWIFI $DNSCONF
# sudo cp $HOSTSWIFI $HOSTSCONF
echo "wifi" > ~pi/network_mode
echo -n "[Press Enter to Reboot]"
read ans
sudo reboot
fi
else
echo "Usage: nework_mode [wap | wifi]"
exit 2
fi
2. Update local startup to differentiate between WAP and WiFi modes
$ sudo vi /etc/rc.local
and then edit (replacing the lines added in step in #7 above):
# Configure network mode
mode=`cat ~pi/network_mode`
if [ "$mode" = "wifi" ]
then
echo "Starting as WiFi"
ifdown br0
ifconfig eth0 192.168.4.110 netmask 255.255.255.0
elif [ "$mode" = "wap" ]
then
echo "Starting as Wireless Access Point (WAP)"
systemctl start hostapd
ifconfig br0 192.168.4.110
route add default gw 192.168.4.110 br0
fi
3. Create alternate versions of the network configuration
$ cd /etc
$ sudo cp dhcpcd.conf dhcpcd.conf.wap
$ sudo cp dhcpcd.conf dhcpcd.conf.wifi
$ sudo vi /etc/dhcpcd.conf.wifi
and then remove these two lines:
denyinterfaces eth0
denyinterfaces wlan0
4. Select WiFi mode and reboot for normal network access
$ network_mode wifi
[Press Enter to Reboot]
5. Select WAP mode and reboot to return to a Wireless Access Point
$ network_mode wap
[Press Enter to Reboot]