1

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.

3

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. :)

  • 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 at 5: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 at 17:30
1

This answer is based on the Foundation tutorial Setting up a Raspberry Pi as an access point in a standalone network (NAT)

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.

  • Just for understanding, hostapd is always running and switching is done by "switching" the ip address and network settings? – Ingo Mar 30 at 11:56
  • 1
    @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 at 23:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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