Building a Raspberry Pi 4 WiFi Access Point + Airplay Server

Raspberry Pi Access Point and Airplay

This tutorial will detail how to build a wireless access point using a Raspberry Pi 4 with hostapd. I’ll also set up an Airplay server using shairport-sync so that my access point can play music.

There are a number of tutorials for setting up a Raspberry Pi as an access point, but many are outdated or don’t work – this one does!

The Setup

I want to set up my Pi as an access point – supplying internet via ethernet cable and broadcasting a wireless network.

I’ll be using this with a pair of powerline ethernet adapters to bring wifi anywhere I need it – in the house, or out in the shed.

Install Raspberri Pi OS

First, the Pi needs an operating system. I installed Raspberry Pi OS lite 64 bit using the Raspberry Pi OS Imager.

Enable SSH

Next, I connected the Pi to a screen and keyboard, logged in, and enabled SSH.

Connect to the Wired Network

The Pi only needs an ethernet connection at this point. Plug in to your network, and leave WiFi unconfigured.

Update!

Update your system.

sudo apt update
sudo apt upgrade

Setting Up Wireless Acesss Point

Install packages

sudo apt install dnsmasq hostapd iptables

Configure Static IP

interface wlan0
static ip_address=10.1.4.1/24
nohook wpa_supplicant

I’m using a static IP address of 10.1.4.1 on the wireless network for my Raspberry Pi access point. Once the network is set up, you can SSH to it at this address.

Configure dnsmasq as DHCP Server

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak
sudo nano /etc/dnsmasq.conf

Add the following lines to the file:

interface=wlan0      # Use the wireless interface - wlan0
dhcp-range=10.1.4.100,10.1.4.200,255.255.255.0,24h

Configuring hostapd as a WiFi access point

Create and edit the file /etc/hostapd/hostapd.conf:

sudo nano /etc/hostapd/hostapd.conf

Add the following to the new file:

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=network_name
wpa_passphrase=network_passphrase

The last 2 lines need to be edited with your desired network name and passphrase.

The system must now be made aware of this new configuration file.

sudo nano /etc/default/hostapd

Uncomment the line beginning with #DAEMON_CONF, and edit it with the path to the above configuration file:

DAEMON_CONF="https://cd.linuxscrew.com/etc/hostapd/hostapd.conf"

Now that hostapd has a valid configuration file, it needs to be enabled:

sudo systemctl unmask hostapd
sudo systemctl enable hostapd

Set up Routing

Edit the file /etc/sysctl.conf and uncomment the following line:

net.ipv4.ip_forward=1

Then, masquerade outbound traffic for eth0:

sudo iptables -t nat -A  POSTROUTING -o eth0 -j MASQUERADE

iptables rules are not permanent, so they must be saved:

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

Edit the file /etc/rc.local and add the following line above “exit 0”:

iptables-restore < /etc/iptables.ipv4.nat

This will load the saved rules when your Pi boots.

Check everything is running

Check that hostapd and dnsmasq are running by issuing the following command:

sudo systemctl status hostapd
sudo systemctl status dnsmasq

If there are any errors, go back and check through your configuration files to make sure they match the above steps.

Reboot and connect!

Reboot your Pi:

sudo reboot

And you should be able to connect to your new hotspot!

As your Pi has a static IP on the wireless network you can now disconnect your screen and keyboard and simply SSH to it when you need to fix something.

Setting Up Airplay

shairport-sync is a program which emulates an Apple airport service, allowing you to stream music to it from your Apple devices. Install it by running:

sudo apt install shairport-sync

Now you need to configure the audio path on the Raspberry Pi. It’s generally set to “auto” but you need to force it to go to the 3.5mm jack. Run raspi-config:-

Raspberry Pi’s are a bit finnicky about their audio output. It’s supposed to be automatic, but I’ve found it’s a bit unreliable in which output it chooses to use.

Run:

sudo raspi-config

… and set the audio output to use the 3.5mm headphone jack.

Next, set the volume for the headphone jack to full volume (actual volume will be controlled via Airplay from your mobile device):

amixer -q -M sset Headphone 100%

I was still having trouble getting audio output on the 3.5mm plug. I figured it was due to the Pi trying to output audio via HDMI (again, the auto output option isn’t too bright).

I’m not entirely sure why setting these options helped, but after setting these options I was getting audio out of the headphone jack. Edit the file /boot/config.txt and edit/uncomment the following lines:

hdmi_group=1
hdmi_drive=2
hdmi_force_hotplug=1
config_hdmi_boost=4

Reboot, and you should have a dual-purpose wireless access point and Airplay server. Neat!

I’ll be strapping my Pi to a robust speaker and taking it out to the shed where there’s a weak signal and a dire need for some loud music.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *