panthema / 2019 / 0802-BlinkenSort-The-Sound-of-LED-Sorting-Algorithms-with-RaspberryPi3-and-APA102-or-SK9822 / wifi-ap-station

BlinkenSort with Sound - Optional: Set Up Both WiFi Station and an Access Point

Posted on 2019-07-27 19:00 by Timo Bingmann at Permlink.

Connecting the Raspberry Pi to a WiFi can easily be done using the raspi-config tool. However, since I take the BlinkenSort art installations to various places I like it to also open an access point (AP) simultaneously. That is much harder.

The following tutorial worked on Your mileage may vary in future versions.

# set up wifi using raspi-config
$ sudo raspi-config
# 2 Network Options > N2 Wi-fi

# install hostapd to advertise a WiFi AP
$ sudo apt install hostapd dnsmasq

Check which WiFi channel your local access point is on. The access point MUST be configured to use the same channel. My system uses channel 3.

$ iw wlan0 info

Create the uap0 interface by writing the file
sudo nano /etc/udev/rules.d/90-wireless.rules

ACTION=="add", SUBSYSTEM=="ieee80211", KERNEL=="phy0", \
    RUN+="/sbin/iw phy %k interface add uap0 type __ap"

Statically configure the uap0 interface by writing the file
sudo nano /etc/network/interfaces.d/uap0

allow-hotplug uap0
auto uap0
iface uap0 inet static

Configure hostapd by creating the following file:
sudo nano /etc/hostapd/hostapd.conf
Maybe change the WiFi SSID and key wpa_passphrase below.

# This is the name of the WiFi interface we configured above

# Use the nl80211 driver with the brcmfmac driver

# This is the name of the network

# Use the 2.4GHz band

# Use channel 3

# Enable 802.11n

# Enable WMM

# Enable 40MHz channels with 20ns guard interval

# Accept all MAC addresses

# Use WPA authentication

# Require clients to know the network name

# Use WPA2

# Use a pre-shared key

# The network passphrase

# Use AES, instead of TKIP

Configure dnsmasq for the uap0 interface by creating the following file:
sudo nano /etc/dnsmasq.d/uap0.conf

# If you want dnsmasq to listen for DHCP and DNS requests only on
# specified interfaces (and the loopback) give the name of the
# interface (eg eth0) here.

# If you want dnsmasq to provide only DNS service on an interface,
# configure it as shown above, and then use the following line to
# disable DHCP and TFTP on it.


# Enable the integrated DHCP server

# Add other name servers here, with domain specs if they are for
# non-public domains.

# Override the default route supplied by dnsmasq and send no default
# route at all.

Disable DHCP client on uap0 by appending the following to its config file:
sudo nano /etc/dhcpcd.conf

interface uap0
nohook wpa_supplicant

Reboot once with disabled hostapd and dnsmasq to test WiFi client and interface configurations, then enable services.

# disable hostapd and dnsmasq for now
$ sudo systemctl disable hostapd dnsmasq

# first reboot to get the network devices working
$ sudo reboot

# check that the WiFi client connects by watching the interfaces
$ ifconfig
(uap0 should exist, wlan0 should exist and be connected to your WiFi router)

# unmask service
$ sudo systemctl unmask hostapd dnsmasq

# enable services
$ sudo systemctl enable hostapd dnsmasq

Delay start-up of the DHCP client on wlan0 until after hostapd launches.
sudo nano /etc/systemd/system/dhcpcd5.service

Remove the following line from systemd's dhcpcd5.service file in the existing sections:


Add the following lines to systemd's dhcpcd5.service file in the existing sections:


ExecStartPre=/bin/sleep 10

Then reboot and check that hostapd and dnsmasq are running correctly by watching ifconfig.

$ sudo reboot