Monday, February 15, 2016

Pi as WiFi Access Point


Have your ever imagine that is it possible to set up your own access point?
Don't worry. Yes, we can set up our own adorable WiFi router using Raspberry Pi.

Here in this experiment, I'm trying my hand to set up Pi as an access point more advanced than using it as a client. This tutorial will help to make it so the Pi broadcasts a WiFi service and then routes internet traffic to an Ethernet cable. Since it's all Linux we can go in and update or configure it however we like.


Things Required:


1. Raspberry Pi with Raspbian OS
2. Ethernet Cable
3. WiFi Adapter

First make sure to have Raspbian installed in Pi.
Then connect to Pi using putty.
Shutdown the Pi and plug in the WiFi module when the Pi is off.

Software Installation:


Make sure the Ethernet connection is up to have internet access to the Pi.

Install software onto Pi that will act as host access point.

sudo apt-get update
sudo apt-get install hostapd isc-dhcp-server

Setup DHCP Server


Next we will edit /etc/dhcp/dhcpd.conf, a file that sets up our DHCP server - this allows wifi connections to automatically get IP addresses, DNS, etc.

Run this command to edit the file

 sudo nano /etc/dhcp/dhcpd.conf

Comment the below lines by adding # at the beginning

option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

uncomment the line below by removing #
#authoritative;

Then scroll down to the bottom and add the following lines

subnet 192.168.42.0 netmask 255.255.255.0 {
range 192.168.42.10 192.168.42.50;
option broadcast-address 192.168.42.255;
option routers 192.168.42.1;
default-lease-time 600;
max-lease-time 7200;
option domain-name "local";
option domain-name-servers 8.8.8.8, 8.8.4.4;
}

Save the file by typing in Control-X then Y then return

Run  sudo nano /etc/default/isc-dhcp-server

and scroll down to INTERFACES="" and update it to say INTERFACES="wlan0" 

close and save the file

Set up wlan0 for static IP:


If you happen to have wlan0 active because you set it up, run
sudo ifdown wlan0

Next we will set up the wlan0 connection to be static and incoming. Run
sudo nano /etc/network/interfaces to edit the file

Find the line auto wlan0 and comment all the lines from it by adding # at the beginning of each line.
Basically, just remove any old wlan0 configuration settings, we'll be changing them up.

Add the lines

  iface wlan0 inet static
  address 192.168.42.1
  netmask 255.255.255.0

Save the file (Control-X Y

Assign a static IP address to the wifi adapter by running
sudo ifconfig wlan0 192.168.42.1

Configure Access Point


Now we can configure the access point details. We will set up a password-protected network so only people with the password can connect.

Create a new file by running sudo nano /etc/hostapd/hostapd.conf

Paste the following in, you can change the text after ssid= to another name, that will be the network broadcast name. The password can be changed with the text after wpa_passphrase=

interface=wlan0
driver=rtl871xdrv
ssid=Pi_AP
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=Raspberry
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Now we will tell the Pi where to find this configuration file. Run sudo nano /etc/default/hostapd

Find the line #DAEMON_CONF="" and edit it so it says DAEMON_CONF="/etc/hostapd/hostapd.conf"

Then save the file

Configure Network Address Translation


Setting up NAT will allow multiple clients to connect to the WiFi and have all the data 'tunneled' through the single Ethernet IP. (But you should do it even if only one client is going to connect)

Run sudo nano /etc/sysctl.conf

Scroll to the bottom and add
 net.ipv4.ip_forward=1

on a new line. Save the file. This will start IP forwarding on boot up

Also run

 sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

to activate it immediately


Run the following commands to create the network translation between the ethernet port eth0 and the wifi port wlan0

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

You can check to see whats in the tables with

sudo iptables -t nat -S
sudo iptables -S

To make this happen on reboot (so you don't have to type it every time) run

 sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

run sudo nano /etc/network/interfaces and add
 up iptables-restore < /etc/iptables.ipv4.nat
to the very end

Update hostapd


Before we can run the access point software, we have to update it to a version that supports the WiFi adapter.
First get the new version by typing in

wget http://adafruit-download.s3.amazonaws.com/adafruit_hostapd_14128.zip

to download the new version (check the next section for how to compile your own updated hostapd) then

unzip adafruit_hostapd_14128.zip

to uncompress it. Move the old version out of the way with

sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.ORIG

And move the new version back with

sudo mv hostapd /usr/sbin

set it up so its valid to run with

sudo chmod 755 /usr/sbin/hostapd

First test!


Finally we can test the access point host! Run

 sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf

To manually run hostapd with our configuration file. You should see it set up and use wlan0 then you can check with another wifi computer that you see your SSID show up. If so, you have successfully set up the access point.




Username : Jyothi_Pi_AP
Password: SaiJyothi
You can try connecting and disconnecting from the Pi_AP with the password you set before (probably Raspberry if you copied our hostapd config), debug text will display on the Pi console but you won't be able to connect through to the Ethernet connection yet.
Cancel the test by typing Control-C in the Pi console to get back to the Pi command line


Second Test!


Also we can test simple HTTP get webclient test using NodeMCU using the available access point.


Username : Jyothi_Pi_AP
Password: SaiJyothi

Using the Arduino programming we can establish this connection. Run below code in Arduino to test.


Code:https://github.com/jyothi-lanka/IoT/commit/48c6e008dd960fc2719650671c64c42e68305a79







Finishing up!


OK now that we know it works, time to set it up as a 'daemon' - a program that will start when the Pi boots.

Run the following commands

 sudo service hostapd start 
sudo service isc-dhcp-server start

you can always check the status of the host AP server and the DHCP server with

 sudo service hostapd status
sudo service isc-dhcp-server status

To start the daemon services. Verify that they both start successfully (no 'failure' or 'errors')
Then to make it so it runs every time on boot

 sudo update-rc.d hostapd enable 
sudo update-rc.d isc-dhcp-server enable



References: https://learn.adafruit.com/setting-up-a-raspberry-pi-as-a-wifi-access-point/overview



No comments:

Post a Comment