14

I want to automatically create an access point, if there is no network found, so that I can connect to my Raspberry Pi everywhere.

If nobody is connected to the hotspot for a while it should search for the networks defined in wpa_supplicant.conf again.

I don't want to install any additional software and to use only wpa_supplicant, wpa_cli and systemd-networkd.

1 Answer 1

10

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.

23
  • Since Raspbian 2019-04-08 you do not need to install rng-tools anymore. There is a typo. For network files you use /lib/systemd/network/ but in the script you use /etc/systemd/network/. You do not need network.target if you use network-online.target but it doesn't matter. Type=simple is default. There are some very nice ideas. Thanks for it.
    – Ingo
    Jul 2, 2019 at 10:55
  • Thanks, @Ingo, I edited it! I'm using /etc/systemd/network/08-CLI.network for masking the file in /lib/.... You think it is better to rename it?
    – jake
    Jul 2, 2019 at 11:16
  • I tend to use systemctl to control services due to stability. So I haven't had a detailed look at the links and I cannot say much about them but I avoid to touch things in /lib/.
    – Ingo
    Jul 2, 2019 at 11:40
  • @Ingo But you can't manage .network files with systemctl, can you? But maybe it is better to disable 08-Client.network with a drop-in file and move everything to /etc/systemd/network. I'll test this.
    – jake
    Jul 2, 2019 at 11:54
  • 1
    This is great! Thanks for the write-up. I've got it running here. A couple of comments: 1) Switching to networkd-resolved doesn't seem mandatory based on my (limited) testing. I haven't sorted how to get search-domain settings passed through with my dhcp-to-preassigned addresses. Still investigating, because I'd like to get there. 2) The 'ln' command is missing an 'f' at the end (should be '.conf'). 3) Typo in the comments: wpa_supllicant-wlan0.conf. 4) Lastly, some sort of naming convention for scripts and conf files might be helpful, but not mandatory.
    – bls
    Jul 3, 2019 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.