The Zero Law of Robotics:
A robot may not harm humanity, or, by inaction, allow humanity to come to harm.

- Isaac Asimov, I, Robot

Follow us on

follow_the_Project_on_facebook

Share this page with friends via

email icon facebook icon twitter icon
linkedin icon google plus icon blogger icon

Turning the Raspberry Pi into Wi-Fi router

Preparing

In this section we will learn how to turn our Raspberry Pi into a Wi-Fi access point and present simple scripts to set and unset the Wi-Fi router to make our work easier.

Before we start, we have to be sure that our USB Wi-Fi module supports the function of an access point (AP). We use the module Tenda W311U+ in our project but there is a plenty on various Linux supported Wi-Fi modules with AP. A list of various adapters along with their properties supported by the Raspberry can be found here.

Further, we need to install these programs:

  • hostapd
    - a user space daemon for wireless access point and authentication servers
  • isc-dhcp-server
    - assigns IP addresses to client devices

We can either use the script from previous tutorial or install them manually.


There are tons of english tutorials on the internet how to turn your Raspberry Pi into a Wi-Fi router(e.g.,elinux.org, raspberrypihq.com, qcktech.blogspot.cz, itsacleanmachine.blogspot.cz, maketecheasier.com and others).
Therefore, we will introduce only a simple scripts which turn our Raspberry Pi into a Wi-Fi router and back again. We will concentrate only on the very basic since all details can be found in the tutorials.

Scripts

We can set the router with script setrouter.sh:
#!/bin/bash
date

echo Set Raspberry Pi as Wi-Fi router

echo COPY CONF FILES

echo COPY - INTERFACES
sudo cp New_files/interfaces /etc/network/interfaces

echo COPY - hostapd.conf
sudo cp New_files/hostapd.conf /etc/hostapd/hostapd.conf

echo COPY - DHCP
sudo cp New_files/dhcpd.conf /etc/dhcp/dhcpd.conf

echo COPY - STARTING SCRIPT FOR HOSTAPD
sudo cp New_files/runhostapd.sh /etc/init.d/runhostapd.sh
sudo chmod 755 /etc/init.d/runhostapd.sh
sudo update-rc.d runhostapd.sh defaults

echo REBOOT
sudo reboot

and unset it again with script unsetrouter.sh:
#!/bin/bash
date
echo Reset Raspberry Pi  Wi-Fi router to default
echo COPY OLD FILES TO RASPI

sudo cp Old_files/interfaces /etc/network/interfaces
sudo cp Old_files/hostapd.conf /etc/hostapd/hostapd.conf
sudo cp Old_files/dhcpd.conf /etc/dhcp/dhcpd.conf


echo Stop HOSTAPD
sudo hostapd stop

echo DELETE - STARTING SCRIPT FOR HOSTAPD
sudo rm /etc/init.d/runhostapd.sh
sudo update-rc.d runhostapd remove

echo COPY FILES - FINNISHED

echo REBOOT
sudo reboot

The scripts copy four new files: interfaces, hostapd.conf, dhcpd.conf and runhostapd.sh from folder New_files or Old_files into the proper directories in the Raspberry Pi. Moreover, the first script setrouter.sh creates a daemon service runhostapd which will start automatically the Wi-Fi router at the start of the Raspberry Pi.

All files can be downloaded in one archive here.

We will briefly discuss the individual files and basic properties on following lines.

interfaces

The file /etc/network/interfaces contains network interface configuration information for the both Ubuntu and Debian Linux. This is where we configure how our system is connected to the network (extensive info can be found here or in the official documentation).

We present two files: new and old. The new file on the left defines the new static network of IP address 10.10.0.1 with submask 255.255.255.0 for interface wlan0. On the other hand, the old file releases the interface wlan0 to its default settings and we can connect our Raspberry Pi again to an arbitrary Wi-Fi network providing access to the internet.


New file
auto lo

iface lo inet loopback
iface eth0 inet dhcp

iface wlan0 inet static
address 10.10.0.1
netmask 255.255.255.0

wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
Old file
auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet manual


wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

hostapd.conf

The file /etc/hostapd/hostapd.conf is a configuration file for the program hostapd. Here we can define all parameters of the created network. This new file is linked to the program when it starts(this ensures the daemon runhostapd). The old file can be empty or contain any random text since we do not run the hostapd in the standard configuration of the Raspberry Pi at all.


New file
interface=wlan0
driver=nl80211
ssid=BlueSeal
hw_mode=g
channel=1

wpa=1
wpa_passphrase=bsproject
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
wpa_ptk_rekey=600
auth_algs=1
macaddr_acl=0
Old file
#
# Empty
#

The parameters ssid and wpa_passphrase contain the name of the created Wi-Fi and its password. The full documentation for hostapd can be found here.

dhcpd.conf

Since the hostapd cannot assign IP addresses to the connected devices, we need to use the program isc-dhcp-server which can do so. The configuration file /etc/dhcp/dhcpd.conf tells the program how to do so.

The configuration file can be very large with many subnet definitions. The entry with our definition is shown below and tells the program to distributed IP addresses from range 10.10.0.25-50, defines DNS servers, routers and interface. Full documentation can be found here.


New file
- contains these additional lines:
# The Blue Seal Project

subnet 10.10.0.0 netmask 255.255.255.0 {
 range 10.10.0.25 10.10.0.50;
 option domain-name-servers 8.8.8.8, 8.8.4.4;
 option routers 10.10.0.1;
 interface wlan0;
}

placed right below the line
...
#This is a very basic subnet declaration
Download full file here.
Old file
- is the same as the new one, but without the entry marked by the blue color.

runhostapd.sh

The very last thing is to ensure that the network will be created automatically at the start of the Raspberry Pi. This can be done by a very simple daemon service: runhostapd. This service is defined in the file runhostapd.sh which is copied by the script setrouter.sh into the location /etc/init.d/runhostapd.sh and added to the list of services to be executed at the start of the computer.

#!/bin/sh
#/etc/init.d/runhostapd

### BEGIN INIT INFO
# Provides: 		runhostapd
# Required-Start:	$remote_fs $syslog
# Required-Stop:	$remote_fs $syslog
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Decription:	Starts hostapd with hostaps.conf as daemon.
### END INIT INFO


#Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting HOSTAPD"
    sudo hostapd -B /etc/hostapd/hostapd.conf
    sudo /etc/init.d/isc-dhcp-server restart
    ;;
  stop)
    echo "Stopping HOSTAPD"
    sudo hostapd stop
    ;;
  *)
    echo "Usage: /etc/init.d/hostapd {start|stop}"
    exit 1
    ;;
esac

exit 0

Download all files here.

Conclusion

In this article we have introduced easy script which sets a Wi-Fi router and turn the Raspberry Pi into an access point. Moreover, we also introduces script lifting all changes and returning the Raspberry Pi to its default settings.



Blue Seal Project created by Jiří Daněk (2013-2016)