The following can also easily be installed from a github repository that I created here .
First we need to change over completely to systemd
(which might be the future anyway), as Ingo has explained here:
# deinstall classic networking
sudo -Es # if not already done
apt --autoremove purge ifupdown dhcpcd5 isc-dhcp-client isc-dhcp-common rsyslog
apt-mark hold ifupdown dhcpcd5 isc-dhcp-client isc-dhcp-common rsyslog raspberrypi-net-mods openresolv
rm -r /etc/network /etc/dhcp
# setup/enable systemd-resolved and systemd-networkd
apt --autoremove purge avahi-daemon
apt-mark hold avahi-daemon libnss-mdns
apt install libnss-resolve
ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
systemctl enable systemd-networkd.service systemd-resolved.service
1. Configure wpa_supplicant
Your wpa_supplicant-wlan0.conf
should look something like this:
country=FR
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
ap_scan=1
### your access point/hotspot ###
network={
ssid="RaspberrypiAP" # your hotspot's name
mode=2
key_mgmt=WPA-PSK
psk="passphrase" # your hotspot's password
frequency=2462
}
### your network(s) ###
network={
priority=10 # add a priority higher then 0 to any networks
ssid="yourWifi" # except the access point's one!
psk="passphrase"
}
We have to add a priority=
higher then 0 to any network section except the hotspot's one, so wpa_supplicant
will prefer them. Only if none of these networks is found, wpa_supplicant
will create an access point/hotspot. If wpa_supplicant
has created a hotspot the interface has to be given a static address and we need a DHCP server, so that we can connect our devices to it. This will be done by systemd-networkd
.
2. Configure wireless interface with systemd-networkd
We need to create the following files. The first one will configure your device as client, the second one as access point. The first one is the default due to the smaller number.
sudoedit /etc/systemd/network/08-CLI.network
[Match]
Name=wlan0
[Network]
DHCP=yes
LinkLocalAddressing=yes
MulticastDNS=yes
sudoedit /etc/systemd/network/12-AP.network
[Match]
Name=wlan0
[Network]
Address=192.168.4.1/24
DHCPServer=yes
LinkLocalAddressing=yes
MulticastDNS=yes
3. Setup a systemd.service
to automatically configure the interface based on wpa_supplicant
events
This service will run wpa_cli
, which executes the script below on certain events.
Run sudo systemctl edit --full --force [email protected]
and paste the following lines to it:
Description=Wpa_cli to Automatically Create an Accesspoint if no Client Connection is Available
After=wpa_supplicant@%i.service
BindsTo=wpa_supplicant@%i.service
[Service]
ExecStart=/sbin/wpa_cli -i %I -a /usr/local/bin/autoAP.sh
Restart=on-failure
RestartSec=1
[Install]
WantedBy=multi-user.target
4. The script needed for the service
This script has to be saved to the path defined in the ExecStart=
section. It will configure the device as client if connected to some wifi, or as access point if wpa_supplicant
has created one, which it will do automatically if no other network is found.
If nobody is connected to the access point for a while it will restart wpa_supplicant
to make it search for wifi networks again.
sudoedit /usr/local/bin/autoAP.sh
#!/bin/bash
device=wlan0
configure_ap () {
if [ -e /etc/systemd/network/08-CLI.network ]; then
mv /etc/systemd/network/08-CLI.network /etc/systemd/network/08-CLI.network~
systemctl restart systemd-networkd
fi
}
configure_client () {
if [ -e /etc/systemd/network/08-CLI.network~ ] && wpa_cli -i$device status | grep -q "mode=station"; then
mv /etc/systemd/network/08-CLI.network~ /etc/systemd/network/08-CLI.network
systemctl restart systemd-networkd
fi
}
reconfigure_wpa_supplicant () {
sleep "$1"
if [ "$(wpa_cli -i $device all_sta)" = ""]; then
wpa_cli -i $device reconfigure
fi
}
case "$2" in
# Configure access point if one is created
AP-ENABLED)
configure_ap
reconfigure_wpa_supplicant 2m &
;;
# Configure as client, if connected to some network
CONNECTED)
configure_client
;;
# Reconfigure wpa_supplicant to search for your wifi again,
# if nobody is connected to the ap
AP-STA-DISCONNECTED)
reconfigure_wpa_supplicant 20 &
;;
esac
Make the script executable chmod +x /path/to/script/autoAP.sh
.
Now we have to run sudo systemctl enable --now [email protected]
, reboot the Pi and everything should work.
I'll be glad for any suggestions on how to improve this setup.