This tutorial is adapted from a tutorial that you can find [here](https://www.raspberryconnect.com/projects/65-raspberrypi-hotspot-accesspoints/157-raspberry-pi-auto-wifi-hotspot-switch-internet).
All the files modified in this document are also available in the repository, in the folder `scripts/raspbian_configuration`. The architecture of this folder shows where each file belong.
In order to work as an access point, the Raspberry Pi will need to have access point software installed, along with DHCP server software to provide connecting devices with a network address.
To create an access point, we'll need DNSMasq and HostAPD. Install all the required software in one go with this command::
```sh
sudo apt install dnsmasq hostapd
```
Since the configuration files are not ready yet, turn the new software off as follows::
```sh
sudo systemctl unmask hostapd
sudo systemctl disable dnsmasq
sudo systemctl disable hostapd
```
## Configuring HostAPD
Using a text editor edit the hostapd configuration file. This file won't exist at this stage so will be blank: `sudo nano /etc/hostapd/hostapd.conf`
```txt
#2.4GHz setup wifi 80211 b,g,n
interface=wlan0
driver=nl80211
ssid=PlanktoScope-Bababui_Tuogaore
hw_mode=g
channel=8
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=copepode
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP TKIP
rsn_pairwise=CCMP
#80211n - Change GB to your WiFi country code
country_code=FR
ieee80211n=1
ieee80211d=1
```
The interface will be wlan0. The driver nl80211 works with the Raspberry RPi 4, RPi 3B+, RPi 3 & Pi Zero W onboard WiFi but you will need to check that your wifi dongle is compatable and can use Access Point mode.
For more information on wifi dongles see elinux.org/RPi_USB_Wi-Fi_Adapters
The SSID is the name of the WiFi signal broadcast from the RPi, which you will connect to with your Tablet or phones WiFi settings.
Channel can be set between 1 and 13. If you are having trouble connection because of to many wifi signals in your area are using channel 8 then try another channel.
Wpa_passphrase is the password you will need to enter when you first connect a device to your Raspberry Pi's hotspot. This should be at least 8 characters and a bit more difficult to guess than my example.
The country_code should be set to your country to comply with local RF laws. You may experience connection issues if this is not correct. Your country_code can be found in /etc/wpa_supplicant/wpa_supplicant.conf or in Raspberry Pi Configuration - Localisation settings
To save the config file press `CTRL+O` and to exit press `CTRL+X`.
We also use a special function to change the network name to the machine name. Add this to `/etc/rc.local` with `sudo nano /etc/rc.local`:
```sh
# Replace wifi hostname
sed -i "s/^ssid.*/ssid=PlanktoScope-$(python3 -c "import planktoscope.uuidName as uuidName; print(uuidName.machineName(machine=uuidName.getSerial()).replace(' ','_'))")/" /etc/hostapd/hostapd.conf
```
Now the defaults file needs to be updated to point to where the config file is stored.
In terminal enter the command `sudo nano /etc/default/hostapd`
Change `#DAEMON_CONF=""` to `DAEMON_CONF="/etc/hostapd/hostapd.conf"`
Check the `DAEMON_OPTS=""` is preceded by a #, so is `#DAEMON_OPTS=""`.
And save.
## DNSmasq configuration
Next dnsmasq needs to be configured to allow the Rpi to act as a router and issue ip addresses. Open the dnsmasq.conf file with `sudo nano /etc/dnsmasq.conf`
Go to the bottom of the file and add the following lines:
```txt
#AutoHotspot config
interface=wlan0
bind-dynamic
server=1.1.1.1
domain-needed
bogus-priv
dhcp-range=192.168.4.100,192.168.4.200,12h
#AutoEthernet config
interface=eth0
bind-dynamic
server=1.1.1.1
domain-needed
bogus-priv
dhcp-range=192.168.5.100,192.168.5.200,12h
```
and then save `CTRL+O` and exit `CTRL+X`.
Reload dnsmasq to use the updated configuration:
```sh
sudo systemctl reload dnsmasq
```
## DHCPCD
DHCPCD is the software that manages the network setup. The next step is to stop dhcpcd from starting the wifi network so the autohotspot script in the next step takes control of that. Ethernet will still be managed by dhcpcd.
This will also create a fallback configuration to a static IP if no DHCP server is present on the Ethernet network.
Just add this line to the end of /etc/dhcpcd.conf with `sudo nano /etc/dhcpcd.conf`:
```txt
nohook wpa_supplicant
# define static profile
profile static_eth0
static ip_address=192.168.5.1/24
static routers=192.168.5.1
static domain_name_servers=192.168.5.1
# fallback to static profile on eth0
interface eth0
fallback static_eth0
```
Save and exit.
For the fallback Ethernet network to work, we also need to add a hook to DHCPCD so it starts up the local DHCP server (dnsmasq). Edit the file `/etc/dhcpcd.enter-hook` with `sudo nano /etc/dhcpcd.enter-hook`:
```sh
if [ "$interface" = "eth0" ] && [ "$if_up" ]; then
Next we have to create a service which will run the autohotspot script when the Raspberry Pi starts up.
Create a new file with the command `sudo nano /etc/systemd/system/autohotspot.service`
Then enter the following text:
```txt
[Unit]
Description=Automatically generates a Hotspot when a valid SSID is not in range
After=multi-user.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/autohotspotN
[Install]
WantedBy=multi-user.target
```
Save and exit.
For the service to work it has to be enabled. To do this enter the command `sudo systemctl enable autohotspot.service`.
## Service Timer
Create the timer with `sudo nano /etc/systemd/system/autohotspot.timer`:
```txt
# /etc/systemd/system/autohotspot.timer
[Unit]
Description=Run autohotspot every 5 minutes, starting 10 seconds after system boot
[Timer]
OnBootSec=5sec
OnUnitActivateSec=5min
[Install]
WantedBy=timers.target
```
Save and exit.
Activate with `sudo systemctl enable autohotspot.timer`.
### AutoHotspot Script
This is the main script that will manage your wifi connections between a wifi router and an Access Point.
It will search for any wifi connection that is setup on you Raspberry Pi by using the details found in /etc/wpa_supplicant/wpa_supplicant.conf
If no wifi signal is found for a known SSID then the script will shutdown the wifi network setup and create a Hotspot. If an ethernet cable that allows internet access is connect then the Hotspot will become a full internet access point. Allowing all connected devices to use the Internet. Without an ethernet connect the Raspberry Pi can be accessed from a wifi device using SSH or VNC.
The script works with SSID's that contain spaces and by entering your routers MAC address it can be used with hidden SSID's.