Saturday, February 13, 2016

Raspberry Pi - WiFi AP

Setting up Raspberry Pi as a WiFi access point:

As we know, our Raspberry Pi can used for lot of purposes, today we are going to do an experiment to make it as an access point of WiFi (Interner Hub or WiFi router). It is an advanced feature to use raspberry pi as an access point rather as a client, with spending very less time on configuration. To set up raspberry pi as an access point, follow below configuration steps.

Installing software to make pi as host access point:

  • First update the existing packages: 'sudo apt-get update'
  • To install hostap: 'sudo apt-get install hostapd isc-dhcp-server'
  • DHCP Server: Open and edit dhcpd.conf file to setup DHCP server on pi by using the following step;
  • To open file in edit mode: sudo nano /etc/dhcp/dhcpd.conf
  • Find and comment the two lines in the file:                                                                          option domain-name "example.org";         option domain-name-servers ns1.example.org, ns2.example.org;
  • Remove # from the lines:                                                                                                      # If this DHCP server is the official DHCP server for the local         # network, the authoritative directive should be uncommented.
             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: ctrl + X, then Y then return.

  • Now, 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.
  • wlan0 for static IP: Before moving farward make sure that wlan0 is not active. Make it inactive using command: 'sudo ifdown wlan0'
  • set up the wlan0 connection to be static and incoming:                                                                    'sudo nano /etc/network/interfaces'
  • Comment(#) the line in the file 'auto wlan0' and comment the lines afterwards the current line.
  •  add the lines into the file: iface wlan0 inet static
       address 192.168.42.1


       netmask 255.255.255.0
  • Save the file and return.
  • Assign a static IP address to the wifi adapter: '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 file using command: 'sudo nano /etc/hostapd/hostapd.conf'
  •  Write the following code into file:    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=Bhargav
    wpa_key_mgmt=WPA-PSK
    wpa_pairwise=TKIP
    rsn_pairwise=CCMP
  • Save the file and return 
  • Now we need to setup configuration file for Raspberry Pi:                                                             'sudo nano /etc/default/hostapd'
  • Find the line #DAEMON_CONF="" and edit it as:                                                                             DAEMON_CONF="/etc/hostapd/hostapd.conf"                
  • Save the file and return          

Configure Network Address Translation:

This will allow multiple clients to connect to the WIFI network. Follow below steps;

  • Open the file sysctl.conf: 'sudo nano /etc/sysctl.conf'
  • Add the line "net.ipv4.ip_forward=1" at the bottom of the file
  • Save the file and return
This will start IP forwarding on boot up.

  • Now run: sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
  • 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
  • Configuration to start the WiFi connection: sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
  • Open the file iptables.ipv4.nat: 'sudo nano /etc/network/interfaces'
  • Add the line at end of file: up iptables-restore < /etc/iptables.ipv4.nat
  • Save the file and return

Update the software to support WiFi hardware. Follow the below steps to setup software;

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


  • To install:   unzip adafruit_hostapd_14128.zip                                                                                                  sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.ORIG                                                                        sudo mv hostapd /usr/sbin
  • Give permission for execution: sudo chmod 755 /usr/sbin/hostapd

Now we are done with setting up our own WiFi on Raspberry Pi. This can be hosted using following command:    'sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf'
It displays the name of our WiFi on other devices. To connect to our router users should provide the password.

Network Name: Krishna_PI
Password: Krishna


Krishna_PI appears on the display (List of available wifi networks)




Connecting to Krishna_PI successfully
(Note: Sometimes video attached is not working properly in blogspot. In such case please visit youtube link: https://www.youtube.com/watch?v=RoahdQBKG0c)


References:

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

No comments:

Post a Comment