2

There are many guides for setting up a Raspberry Pi as an Access Point, and even some which allow switching between Access Point and WiFi client.

I wanted a setting which would work as a normal WiFi Client (using dhcpcd) when one of my networks is available and as an Access Point otherwise.

Milliways
  • 59,890
  • 31
  • 101
  • 209

2 Answers2

4

In case you want to save some hassle as mentioned by Milliways. I suggested a short way. Command for installing RaspAP:

wget -q https://git.io/voEUQ -O /tmp/raspap && bash /tmp/raspap

Default SSID - raspi-webgui
Default Wifi Password - ChangeMe

Default Login:
user - admin
passwd - secret

Reference: https://www.youtube.com/watch?v=RvOyafQeOoY&t=33s

Let us know if it works. :)

Ingo
  • 42,107
  • 20
  • 85
  • 197
  • Trust me - download this code and run it on your system! Not likely. If you have a solution why not post it here? – Milliways May 18 '19 at 05:03
  • @Milliways. It is doing the same thing what you have done but it automates all the process. This works for my lab setup. Since you pretty much mention every step in detail there was nothing to add to the solution. By the way great details answers of yours. :) – Sahil Gupta May 22 '19 at 17:30
3

Update While the below did work with the onboard WiFi interfaces it is a kludge, so I can not recommend it as a reliable solution.

This answer is based on the Foundation tutorial Setting up a Routed Wireless Access Point

If no networks can be found (by dhcpcd) the "Fallback Profile" is used to set a Static IP Address (and disable WiFi gateway).

This enables the Pi to be accessed headlessly by WiFi - which is handy when travelling without keyboard, monitor etc, but allows full network functionality when a known network is available.

The steps below are a modified (and simplified) version of the tutorial. You are urged to read the full tutorial before proceeding.

Install Software 1

sudo apt update
sudo apt install dnsmasq hostapd
sudo systemctl stop dnsmasq
sudo systemctl stop hostapd
sudo reboot

1. The hostapd .service file is now automatically masked every time the package is upgraded with no valid configuration. After configuring you should unmask with sudo systemctl unmask hostapd.service

Configuring a static IP Fallback Profile

Add the following to the end of /etc/dhcpcd.conf (This step differs from the Foundation tutorial)

# It is possible to fall back to a static IP if DHCP fails:
# define static profile
profile static_wlan0
  static ip_address=10.1.4.1/24
  nogateway

fallback to static profile on wlan0

interface wlan0 fallback static_wlan0

You can test this by restarting dhcpcd - to see the static fallback you need to rename the network in /etc/wpa_supplicant/wpa_supplicant.conf so it will not be found by dhcpcd

sudo systemctl restart dhcpcd

Configuring the DHCP server (dnsmasq)

Type or copy the following information into the /etc/dnsmasq.conf configuration file and save it: NOTE the dhcp-range should match the static IP set in /etc/dhcpcd.conf

interface=wlan0
dhcp-range=10.1.4.2,10.1.4.20,255.255.255.0,24h

Configuring the Access Point host software (hostapd)

Save the following in /etc/hostapd/hostapd.conf

interface=wlan0
driver=nl80211
ssid=NameOfNetwork
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=AardvarkBadgerHedgehog
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Edit /etc/default/hostapd and replace the line with #DAEMON_CONF with this:

DAEMON_CONF="/etc/hostapd/hostapd.conf"

Start it up

sudo systemctl start hostapd
sudo systemctl start dnsmasq

The latest update to Raspbian (2019-03-09) has changed hostapd "wpa (2:2.6-10)". You should run sudo systemctl unmask hostapd and sudo systemctl enable hostapd to ensure it runs on boot.

#Add routing and masquerade The Foundation tutorial includes instructions to add routing and masquerade. This is not necessary for my use case (and if I wanted to do this would probably write a systemd service, rather than the clumsy rc.local approach)

Strictly, when running as a WiFi client hostapd and dnsmasq are not needed, but seem to do no harm.

NOTE there are other tutorials which do a similar job. http://www.raspberryconnect.com/network has a number of different options and can automatically switch between Access Point and WiFi client, but is more complex and uses wpa_cli to switch between networks. This contains lots of helpful information.

300D7309EF17
  • 1,372
  • 1
  • 11
  • 17
Milliways
  • 59,890
  • 31
  • 101
  • 209
  • Just for understanding, hostapd is always running and switching is done by "switching" the ip address and network settings? – Ingo Mar 30 '19 at 11:56
  • 2
    @Ingo The "switching" is done by dhcpcd. As I noted in the Answer "hostapd and dnsmasq … seem to do no harm". They are running, and use a small amount of memory for buffers/cache. I had originally intended to stop them if a normal network was detected, which would be simple enough, but as I can detect no adverse effects this seemed superflous. (I might add I did this just before going on a trip where I wanted to use the Pi as AP.) – Milliways Mar 30 '19 at 23:27
  • The /etc/default/hostapd complains "WARNING: The DAEMON_CONF setting has been deprecated and will be removed in future package releases." What is the new way of setting this up? The linked readme mentions something about service templates or an ifupdown method for /etc/network/interfaces, neither of which I know about. – Bergi Feb 14 '20 at 04:07