Turning the Raspberry Pi into a Wireless Access Point

Hi! Following my latest posts on how to get started with the Raspberry Pi "headless mode" and Using Bluetooth with the Raspberry Pi now I'll show how to turn the Raspberry Pi into a Wireless Access Point. Most of this post follows what is described here but I added a few more steps. Moreover, I ran into some troubles and had to do some troubleshooting. I used a D-Link DWA-140 Rangebooster N Adapter which contains a Ralink RT2870 chipset.

(Click the photo for better resolution in Picasa Photostream)
 The process looks a little long but it can be done in under 10 minutes :) . Every time you need to edit or create some file (for example /etc/udhcpd.conf) among this process you can use "sudo nano /etc/udhcpd.conf".
  1. So, first thing to do is to check if the dongle is being properly recognized. Open the terminal and type:
  2. lsusb
    and if it is being recognized you should see something regarding a wireless adapter. Mine says:
  3. Now you'll have to instal the "iw" tool to see which modes are supported by your wireless dongle. Open the terminal and type:
    sudo apt-get install iw
    After it finishes installing enter the following command:
    iw list
    Then you should see lots of specs being printed and somewhere in the middle you should find something like this (the modes below are the ones supported by the USB dongle that I'm using):
    Supported interface modes:
       * IBSS
       * managed
       * AP
       * AP/VLAN
       * WDS
       * monitor
       * mesh point
    
    If there is the AP option then you can keep going. Otherwise that mode is not supported by your wireless dongle and you should get a different one.
  4. The next step will be to install "hostapd" and "udhcpd":
    sudo apt-get install hostapd udhcpd
  5. Now let's configure DHCP so that the Raspberry Pi will be able to hand out IP addresses to the clients. Edit the file /etc/udhcpd.conf to contain the following:
    start 192.168.42.2 # This is the range of IPs that the hostspot will give to client devices.
    end 192.168.42.20
    interface wlan0 # The device uDHCP listens on.
    remaining yes
    opt dns 8.8.8.8 4.2.2.2 # The DNS servers client devices will use.
    opt subnet 255.255.255.0
    opt router 192.168.42.1 # The Pi's IP address on wlan0 which we will set up shortly.
    opt lease 864000 # 10 day DHCP lease time in seconds
  6. The next step is to edit the file /etc/default/udchpd and change the line
    DHCPD_ENABLED="no"
    to
    #DHCPD_ENABLED="no"
  7. Now let's give the Pi a static IP address by entering the following command:
    sudo ifconfig wlan0 192.168.42.1
    To set this up automatically we must edit the file /etc/network/interfaces and replace the line "iface wlan0 inet dhcp" to:
    iface wlan0 inet static
      address 192.168.42.1
      netmask 255.255.255.0
    If the line "iface wlan0 inet dhcp" does not exist, add the referred lines at the bottom of the file. You will also have to change the lines (they may not be next to each other)
    allow-hotplug wlan0
    wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
    iface default inet manual
    
    to
    #allow-hotplug wlan0
    #wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
    #iface default inet dhcp
    
  8.  Now we'll need to configure hostapd, which is used to create the network. The network may be an open network or a WPA-secured network for authorised access only. To do that we'll have to create some files and edit others. The first file that we'll need to edit/create is "/etc/hostapd/hostapd.conf". For a WPA-secured network make it look like this:
    interface=wlan0
    driver=nl80211
    ssid=My_AP
    hw_mode=g
    channel=6
    macaddr_acl=0
    auth_algs=1
    ignore_broadcast_ssid=0
    wpa=2
    wpa_passphrase=My_Passphrase
    wpa_key_mgmt=WPA-PSK
    wpa_pairwise=TKIP
    rsn_pairwise=CCMP
    You can change the SSID, channel and wpa_passphrase to whatever you want. SSID is the name of the hotspot and the wpa_passphrase is the password that will be required to access the network. The channel is used to select the operating frequency of the wireless access point. In case you have another AP or router at home, make sure that you choose a free channel so that they won't interfere with each other. If you prefer to create an open network just enter the following text in the file instead:
    interface=wlan0
    ssid=My_AP
    hw_mode=g
    channel=6
    auth_algs=1
    wmm_enabled=0
    
  9. Now we'll have to change a variable inside the file /etc/default/hostapd so that it points to the file that we have just created/edited. Change this line:
    #DAEMON_CONF=""
    to
    DAEMON_CONF="/etc/hostapd/hostapd.conf"
  10. The next thing to do is to configure NAT (Network Address Translation) which is a technique that allows several devices to use a single connection to the internet. Edit the file /etc/sysctl.conf and add the following line to the end of the file:
    net.ipv4.ip_forward=1
    It will enable NAT in the kernel. Now, run the following commands:
    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
    
    And this concludes the NAT configuration. To make it permanent so you don't have to run the commands after each reboot, run the command
    sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
    and edit the file /etc/network/interfaces and add the following lines to the end of the file:
    up iptables-restore < /etc/iptables.ipv4.nat
  11. We're almost finishing! Now it's time to get it running! Run the following commands to start the Access Point:
    sudo service hostapd start
    sudo service udhcpd start
    
    And your Pi should finally be hosting a wireless hotspot! :) To get it to start on boot everytime, run these two last commands:
    sudo update-rc.d hostapd enable
    sudo update-rc.d udhcpd enable
    

  12. And you're done! If you followed every step correctly everything should be working as expected... Unless you're familiar to the Laws of Murphy (like me :P) and nothing can just work at first time! Everytime I turned on the Raspberry Pi I could see the wireless network appearing but with a very week signal and after less then a minute it would be gone. Strange thing, huh? So I tried to restart hostapd to see if it would reappear but nothing would happen. The only way to bring the wireless network to life again was by rebooting the Pi. I checked if Rasbpian Wheezy came with some kind of network manager that could be messing with the wireless dongle but once again I couldn't find the problem. Then I remembered that it could be a power issue! When it would show the signal was very low and the fact that nothing would bring it to life except by cutting and restoring power to the Raspberry Pi could indicate some sort of hardware malfunction. Another fact that could make my hypothesis to be true was the 140mA current limit on each USB port of the Raspberry Pi. 140mA may not be enough to power some USB Wi-Fi dongles and other devices. So I picked the USB Hub that I had previously modified to allow an external power source and connected everything to it, including the Pi. Turned the 5V, 1A power supply on and voilá! Everything was finally working as expected! In fact, this whole post has been written using the Raspberry Pi as a Wireless Access Point. It has been turned on for a while now and keeps quite stable and fast. If you run into some problems you can also check the Rasperry Pi forum thread that originated this tutorial here. I hope this post was useful for you and that you enjoyed reading it.

    Take care! :)



    Did you find this post helpful? Do you wish to contribute to other projects regarding computer science, electronics, robotics or mechatronics that will be posted in this blog? If you wish to do so, you can donate via paypal using the button below. Thanks! :)

    Donate

0 Comments:

top