About this blog

The name of this blog is small alley. As in an alley, I hope to post a variety of things that I find interesting and as you walk through the alley, you would find things that you like.

I have posted some songs that I wrote. I am not a great singer. I sing and play the guitar purely for fun and I hope people who thinks they can't sing can be inspired by me. No former singing lessons and no former guitar training. Just pure fun......

The recording is also done at home with a drum machine and one guitar through a small amp. The sound file has been touched up slightly by removing the noise. Otherwise the songs are as original as it gets.

I practise Taiji regularly. For me, it is a great exercise requiring control of mind and body to perform well and for it to be enjoyable. You can get more videos of Taiji from YouTube.

Other posts include places and events that I think are interesting and more important, which I have time to upload.

Enjoy your walk through this small alley.....

Saturday, September 28, 2013

Raspberry Pi as a Wifi Hotspot

My intention was actually to set up a Raspberry Pi print server for my home.  After buying the Raspberry Pi, I realised that my printer does not support the ARM architecture.  However I am discovering other interesting ways to use the Pi and using it as a WiFi hotspot is one option.

While trying to set up the hotspot, I made reference to may wonderful posts found on the Internet.  However I found that the posts do not cover everything and there are assumptions that they made which does not apply to me.  Or they have missed posting some information/knowledge that I found was crucial in setting up the hotspot.  In posting my experience, I hope to fill in some gaps and help others just as I have been helped.

I set up my Pi hotspot using Raspbian and I am going to assume that you have got this set up on your SD card.

In my setup, I have the wired Ethernet cable connect to the router provided by my ISP.  I use the Edimax EW-7318USG dongle.  It is a couple of years old but it is supported by Linux out of the box.

Note that all commands are run from a terminal window.  I also assume you know how to use the nano editor.  You also need to know a bit about how networks are defined by IPs (Get this knowledge from the Internet if you have problems in this area).

Step 1

After you start up the Pi, update your system:-

sudo apt-get update
sudo apt-get upgrade


Type:-

ifconfig -a

and ensure that you see the eth0 and wlan0 listed.  Make a note of the IP address of eth0.  For example, mine says 192.168.1.77 and this IP is assigned by the DHCP server of my router.

You need to know this as your wlan0 needs to be set up on a different network - for example 192.168.0.0.  (Please look at the numbers carefully and compare with those assigned to eth0).

If you cannot see wlan0, your wifi adapter may not be detected or supported.  Search the Internet for solutions.

Step 2

Install iw:-

sudo apt-get install iw

After the installation, run:-

iw list


You should see some thing like this:-


Supported RX frame types:
* IBSS: 0xb0 0xc0 0xd0
* managed: 0x40 0xd0
* AP: 0x00 0x20 0x40 0xa0 0xb0 0xc0 0xd0
* AP/VLAN: 0x00 0x20 0x40 0xa0 0xb0 0xc0 0xd0
* mesh point: 0xb0 0xc0 0xd0
* P2P-client: 0x40 0xd0
* P2P-GO: 0x00 0x20 0x40 0xa0 0xb0 0xc0 0xd0



Note that you have "AP" and "AP/VLAN" which indicate that your wlan0 can support hotspot configuration.  If not, you may need to buy another adapter.

Step 3

Get the necessary software:-

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

Step 4

Configure your network interfaces (if you are worried, make a backup of all the files before changing them):-

sudo nano /etc/network/interaces

When the editor opens, change the entries to look something like this:-


auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0

iface wlan0 inet static
 address 192.168.0.1
 netmask 255.255.255.0

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

up iptables-restore < /etc/iptables.ipv4.nat



You can see that I have assigned the IP address 192.168.0.1 to my wlan0.  Remember, the wlan0 must be on a different network from eth0.

Step 5

Now you configure the DHCP server, isc-dhcp-server.

sudo nano /etc/dhcp/dhcpd.conf

Search through the file to find the location which says something like:-



default-lease-time 600;
max-lease-time 7200;

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;
authoritative;


Remove the hex sign "#" so that the line containing "authoritative" looks like this:-



default-lease-time 600;
max-lease-time 7200;

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;


Next go to the bottom of the file and enter the following:-


subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.10 192.168.0.20;
option broadcast-address 192.168.0.255;
option routers 192.168.0.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;
}



Note that the above are my figures.  Take note of those numbers starting with "192" and change them to suit your need.  But remember they must be on the same network but different from that of eth0.

After you are done, save the file.


Next you need to link isc-dhcp-server to wlan0.  Do the following:-

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

Scroll down to INTERFACES="" and change it to say INTERFACES="wlan0"

Save the changes.


Step 6

Configure the access point - hostapd by creating the hostapd configuration file:-


sudo nano /etc/hostapd/hostapd.conf



Type the following into the empty file:-



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



Replace "YourRaspberryPiRouter" and "YourPiRouterPhrase" with something suitable for your case.

Note that the nl80211 driver works for me.  Your wifi adapter may use another driver.  But I recommend you try nl80211 first as the hostapd software that comes with Raspbian works with this.  If you use another driver, you may need to tweak hostapd and I do not know how to do that.

Do not put in extra space at the end of the file.  hostapd can be picky.

Once you are done, save the file.


Now we will tell hostapd where to find this configuration file:-

sudo nano /etc/default/hostapd

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

Remember to remove the # in front of "DAEMON".

Then save the file



Step 7

You will now configure the iptables to ensure that eth0 communicates with wlan0:-

sudo nano /etc/sysctl.conf



Go to the bottom and add the following line to start IP forwarding on boot:-

 net.ipv4.ip_forward=1

Save the file.

Also run

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



Run the following (3 lines) to create the rules for iptables:-


sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state ELATED,ESTABLISHED -j ACCEPT

sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT



Save the rules by typing:-

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


Step 8


To ensure that hostapd and isc-dhcp-server start up on boot, type the following:-



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




The setup is completed.  Restart Pi:-

sudo shutdown -r now


After Pi boots up, use another device (tablet, HP, computer) to scan for the hotspot.  It should show up and allow you to connect to it.


If you encounter errors, try the following:-

Run hostapd and see what error messages are displayed and correct them.

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


Restart isc-dhcp-server to see if it display any errors and correct them.

sudo /etc/init.d/isc-dhcp-server restart


The iptables rules can be viewed by typing:-

iptables -L


Mine shows:-



Chain INPUT (policy ACCEPT)
target     prot opt source               destination        

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination        
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     all  --  anywhere             anywhere          

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination



I hope the above helps.  I am not an expert but just want to share my experience.  Good luck.