If you’ve been doing your Raspberry Pi 3 projects where you need to connect your ESP8266 modules to your Pi 3 for sensor data collection, chances are you might be using a separate access point to route your data. This article will show you how to eliminate the use of a router when accessing your Raspberry Pi by making it as a standalone access point itself. In this way, you can also operate your Raspberry Pi in headless set up using SSH (no more cables for HDMI monitor, mouse and keyboard).
Before starting, make sure your setup is ready:
- A raspberry pi with keyboard, mouse and monitor
- A raspberry Pi with internet connection (either via WiFi or Ethernet)
Step 1: Update your OS
To start, we need to update our Raspberry Pi 3. Try opening up the terminal (Ctrl + Alt + T) and type the following commands:
sudo apt-get update sudo apt-get dist-upgrade
Note: The upgrade may take minutes or hours depending on your device or internet connection.
Step 2: Install Required Packages
Next step is to install the packages. Type the following:
sudo apt-get install dnsmasq hostapd
Note: These packages enable your device to establish its own standalone network (hostapd) and automate IP assignment (dnsmasq) to the devices connected to your Raspberry Pi.
Step 3: Set a Static IP for WLAN0
Configure the /etc/network/interfaces to have a static IP by invoking the following command:
sudo nano /etc/network/interfaces
Next, edit the file to have the following configuration:
allow-hotplug wlan0 iface wlan0 inet static address 172.24.1.1 netmask 255.255.255.0 network 172.24.1.0 broadcast 172.24.1.255
Out of this, your Pi will have a static IP of 172.24.1.1. Of course, you can select for your own private IP address (i.e. ranging 10.0.0.0 to 10.255.255.255 and 192.168.0.0 to 192.168.255.255).
Type the following command to restart the network service.
sudo service networking restart
Step 4: Configure DNS/DHCP Server
Next step is to configure the dnsmasq. This service is responsible for assigning the IP address of your device when it connects to the Pi. The default configuration contains a lot of information and thus should not be overwritten should we decide to revert back to this configuration in the future. So instead, we can rename this file to:
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
and create a new configuration file:
sudo nano /etc/dnsmasq.conf
Add the following lines to the file:
interface=wlan0 listen-address=172.24.1.1 bind-interfaces server=184.108.40.206 domain-needed bogus-priv dhcp-range=172.24.1.10,172.24.1.90,12h
This enables your service to listen to your static IP address in your wlan0 interface. The dhcp-range allows your Pi to hand out the range of IPs to your connected devices. The 12h is the lease/reservation time of dwelling an IP address given by the DHCP server to the device connected. For a full documentation of dnsmasq service, check out this link:
Step 5: Configure Hostapd
Next is to configure your access point by editing the hostapd configuration file.
sudo nano /etc/hostapd/hostapd.conf
Copy and paste the following settings:
interface=wlan0 driver=nl80211 ssid=RaspberryPi3AP hw_mode=g channel=6 ieee80211n=1 wmm_enabled=1 ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40] macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_key_mgmt=WPA-PSK wpa_passphrase=BurgerWithEggAndCheese
You may want to create your own SSID, passphrase and channel for your access point.
A daemon can autorun this setting when the Pi boots by editing the configuration file so that you will never reconfigure your Pi whenever the system restarts. Type these commands:
sudo nano /etc/default/hostapd
Look for the line containing “DAEMON_CONF” and edit this (remove hash if there is) by inserting the following:
Step 6: Enable IPv4 Forwarding
By the end of this activity, you should be able to connect your Pi 3 to a modem via the Ethernet cable to access the internet. Through packet forwarding, your Pi will allow your connected devices to communicate with the Ethernet thus sharing your Pi’s internet connection.
So the next step is to enable IPv4 forwarding by editing the sysctl.conf
sudo nano /etc/sysctl.conf
and uncomment the IPv4 forwarding section. It should look like this:
# Uncomment the next line to enable packet forwarding for IPv4 net.ipv4.ip_forward=1
Step 7: Set the Rules for IPTables
Open the file containing the IPTables by typing:
Sudo nano /etc/iptables.ipv4.nat
The following rules are currently what I am using. You may copy these for your own configuration.
# Generated by iptables-save v1.4.21 on Wed Dec 21 11:28:05 2016 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT -A FORWARD -i wlan0 -o eth0 -j ACCEPT COMMIT # Completed on Wed Dec 21 11:28:05 2016 # Generated by iptables-save v1.4.21 on Wed Dec 21 11:28:05 2016 *nat :PREROUTING ACCEPT [0:0] :INPUT ACCEPT [0:0] :OUTPUT ACCEPT [0:0] :POSTROUTING ACCEPT [0:0] -A POSTROUTING -o eth0 -j MASQUERADE COMMIT # Completed on Wed Dec 21 11:28:05 2016
Also, we need these rules to auto run on each boot. To do this, we will edit the /etc/rc.local
sudo nano /etc/rc.local
and just above the ‘exit 0’, add the following line
iptables-restore < /etc/iptables.ipv4.nat
Step 8: Start the Services
Now, all the configurations are complete! All we need to do is to start the services:
sudo service hostapd start sudo service dnsmasq start
And finally, reboot the system:
And you’re done! When your Pi has turned on, try to check your Wi-Fi networks and you should be able to connect to your access point.
Let us know if you have successfully turned your Pi 3 into a router through the comments section below. Share this article with your friends who are also working with the same project. Enjoy!