Using your Raspberry Pi as a Wireless Router and Web Server

I got an email earlier in the week from some students at SoonChunHyang University, about 100km from Seoul, Korea. They are working on a project where they are using Raspberry Pi’s as a web server, but also wanted to be able to control the Pi’s via their smart phones and desktops. They ran into issues because they were using the (previously) unsupported WiFi Adapter (RTL8188CUS chipset) that I wrote about in this Raspberry Pi WiFi Hotspot post.

A computer science graduate myself, I offered to write a tutorial for them to help them out. This entire tutorial might not be relevant for everyone, but I cover a lot of issues that people struggle with.

  • -Setting up a DHCP Server
  • -Setting up a Wireless Access Point
  • -Setting up an rPi as a Router
  • -Enabling IP Forwarding

I’ll begin with a fresh image of the Raspbian and just a regular ethernet cable to configure it. I’ve always recommended Raspbian because it is an optimized version of Debian built specifically for Raspberry Pi’s. The last stable downloads can be found here.

To flash your SD Card, you will need to unzip the image and write it your SD card using Win32DiskImager. This tool can also be used to after our initial setup to create an image of our finalized implementation(very useful as a backup).

windiskimager

After the image is flashed, you can boot your device. At this point you can use your HDMI Cable/Mouse/Keyboard for your initial configuration, or you can use an SSH Client like Putty to connect. The default hostname, login, and password are as follows:

host:raspberrypi
Username: pi
Password: raspberry

raspberry pi putty

For this tutorial, I will be using putty. On first boot, you will be prompted with a configuration tool called Raspi-Config. If the raspi-config doesnt load automatically, just enter the following command from the shell to get started.

sudo raspi-config

raspi-config-screenshot

The settings I recommend you update are

update
expand_rootfs
change_pass
change_timezone
memory_split

The usual distribution images are 2 GB. When you copy the image to a larger SD card you have a portion of that card unused. expand_rootfs expands the initial image to expand to fill the rest of the SD card, giving you more space. By default, 64mb is reserved for the Graphical UI. Since we plan on using this as a web server, I reduce this to 32mb with the memory_split command.

After you finish your changes to the raspi-config, you should reboot your pi using the following command:

sudo shutdown -r now

At this point we have a fully functional linux server, but we still need to install apache and get the hotspot/routing functions working. The first part is pretty easy. Run the following commands from the shell to install Apache.

sudo apt-get update
sudo apt-get install apache2

The location of the Apache configuration file is

/etc/apache2/apache2.conf

After this is completed, give it a try! Navigate to the IP or Hostname of your Rasberry Pi in your browser, and you should see the Apache Splash “It works!” Screen.

it works apache

I believe this is what the students at SoonChunHyang already have working. But, they want to be able to access this web server over a WiFi Hotspot running on the same Raspberry Pi. In order to do this, we need to install hostapd.

sudo apt-get install hostapd

The following steps are only for people who have WiFi adapters with the RTL8188CUS Chipset, if you don’t, please skip ahead to the section titled configuring Hostapd.
I believe the whole crux of their problem so far is that it is the apt hosted copy of hostapd is not compatible with the RTL8188CUS chipset they are using. But, thanks to the Edimax team, I’ve got a replacement hostapd binary to resolve this issue. I don’t think they will be able to get this working without it.

To download and replace the installed binary version of hostapd we just installed, they need to issue the following commands:

wget http://www.daveconroy.com/wp3/wp-content/uploads/2013/07/hostapd.zip
unzip hostapd.zip 
sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.bak
sudo mv hostapd /usr/sbin/hostapd.edimax 
sudo ln -sf /usr/sbin/hostapd.edimax /usr/sbin/hostapd 
sudo chown root.root /usr/sbin/hostapd 
sudo chmod 755 /usr/sbin/hostapd

Configuring Hostapd

Now that we’ve updated the binary copy of hostapd, we need to configure it. To do so, create the following file

sudo nano /etc/hostapd/hostapd.conf

with the following contents:

interface=wlan0
driver=rtl871xdrv
ssid=DaveConroyPi
channel=6
wmm_enabled=1
wpa=1
wpa_passphrase=0000000000
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
auth_algs=1
macaddr_acl=0

 

In previous tutorials, I have used bridge-utils here to help bridge the connection between the interfaces wlan0 and eth0, but in this case we want the wireless card and rPi to handle the routing and dhcp. So , we may be able to connect to the access point, but we won’t get an IP just yet. To do that, we need to define a subnet for our wireless card.

sudo nano /etc/network/interfaces

and add the following 3 lines

iface wlan0 inet static
address 10.10.0.1
netmask 255.255.255.0

 

Then we have to enable dhcp on the pi for this network so that any devices that connect can get an IP addresss

sudo apt-get install isc-dhcp-server
sudo nano /etc/dhcp/dhcpd.conf

and make sure you have the following contents:


authoritative; #be careful with this setting
ddns-update-style none;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;


#for the wireless network on wlan0
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;
}

 

At this point we should reboot to test HostAPD and DHCP.

sudo reboot

After the rPi comes back up, start hostapd in the background, then reboot the dhcp server

sudo hostapd -B /etc/hostapd/hostapd.conf
sudo /etc/init.d/isc-dhcp-server restart

At this point you should be able to join the wireless network, get an IP, and also hit the local apache web server. If it connects, we will want to start this wireless access point on boot with the following commands

sudo nano /etc/default/hostapd

and uncommenting and updating the following line

DAEMON_CONF="/etc/hostapd/hostapd.conf"

So, if you have followed these steps correctly, you can now provide DHCP servers to any clients, and also allow serve those clients your Apache web pages.

it works apache raspberry pi

There is only one problem remaining. The clients who have connected to your access point can not get further than your Raspberry Pi, therefore have no internet access.

To solve this, we need to enable IP forwarding on the rPi. You can not reboot at this point forward or all future changes will be lost. To save time, we are going to run the final commands as root.

sudo su
echo 1 > /proc/sys/net/ipv4/ip_forward

Then, update /etc/sysctl.conf and uncomment this line

nano /etc/sysctl.conf

net.ipv4.ip_forward=1

Save the file.

The final step is to insert an iptables rule to allow NAT. (eth0 being the interface which is connected to the internet)

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

We also need to save these IP Table settings so they are enabled every time you boot the rPi. To save these settings to a file, use the following command.

iptables-save > /etc/iptables.up.rules

To load them at boot, we need to create a script in /etc/network/if-pre-up.d/

nano /etc/network/if-pre-up.d/iptables

with the following contents:

#!/bin/sh
#This script restores iptables upon reboot

iptables-restore < /etc/iptables.up.rules exit 0

Then, verify the permissions so that it will start on boot.

chown root:root /etc/network/if-pre-up.d/iptables 
chmod +x /etc/network/if-pre-up.d/iptables
chmod 755 /etc/network/if-pre-up.d/iptables

And we are done! When you reboot your Raspberry Pi should be working as an Access Point, a Web Server, a DHCP Server, and an Internet Ready Router!

internet working raspberry pi

Read More

Verified: Raspberry Pi and 3G Sierra Wireless 250u Aircard from Sprint

I am writing this quick post as I recently had a very difficult time getting the Sierra Wireless 250u 3G USB modem from Sprint working on my Raspberry Pi. I also had seen anyone else on the web or forums having success with this combination. It turns out my main issue was that I was not using a powered USB hub. The Raspberry Pi’s only output about 100mA out of their USB ports and the 3G modem requires much more than that.

sierra wireless 250u and raspberry pi

After I started using a powered USB hub, the configuration was straight forward. I am using the Wheezy version of Raspbian as my OS. This Belkin 7 Port Hub works out of the box.

The first thing I needed to do was check to make sure my device was recognized.

lsusb

You should get a print out similar to this,

lsusb-sierra

The important line we are looking for is:

Bus 001 Device 010: ID 1199:0301 Sierra Wireless, Inc.

Now that we have verified that the device is being recognized by the rPi, we need to install our dialer, WvDial. WvDial is a Point-to-Point Protocol dialer, it will enable a modem and start pppd in order to connect to the Internet.

sudo apt-get install wvdial

Then, edit the configuration file:

sudo nano /etc/wvdial.conf

I replace the entire contents of my file with the following:

[Dialer Defaults]
Modem = /dev/ttyUSB0
Baud = 460800
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
ISDN = 0
Modem Type = USB Modem
Phone = #777
Username = ''
Password = ''
Carrier Check = no
Stupid Mode = 1

Now, test the connection:

sudo wvdial

A succesful connection looks like this:

--> WvDial: Internet dialer version 1.61
--> Cannot get information for serial port.
--> Initializing modem.
--> Sending: ATZ
OK
--> Sending: ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
OK
--> Modem initialized.
--> Sending: ATDT#777
--> Waiting for carrier.
ATDT#777
CONNECT
--> Carrier detected. Starting PPP immediately.
--> Starting pppd at Thu Jul 11 18:46:50 2013
--> Pid of pppd: 3395
--> Using interface ppp0
--> local IP address xxx.xxx.xxx.xxx
--> remote IP address xx.x.xx.xxx
--> primary DNS address xx.x.xxx.x
--> secondary DNS address xx.xx.33.x

There is a manual from Sprint located here if you have any troubles connecting, or just comment on the blog and I’d be happy to help debug with you. Thanks for reading.

Read More

Turn Your Raspberry Pi Into a WiFi Hotspot with Edimax Nano USB EW-7811Un (RTL8188CUS chipset)

I’m writing this blog to help anyone with an Edimax Nano USB WiFi adapter (EW-7811Un) configure a Wireless Accesss Point. The main reason I wrote this post is because the Edimax was the first wireless adapter I ever recommended(turning a Raspberry Pi into a fully functional web server). I chose this adapter because it works out of the box, is cheap, and has a really low profile. But when I started the process of trying to turn my rPi into a WiFi hotspot, it seemed that every tutorial out there claimed that the Edimax card I had recommended “doesn’t support Access Point”.

Luckily for you and me, this is not the case.

edimax-dime-ss

Given the varied level of experience of my readers, this tutorial will start from scratch. If you already have your operating system running, skip forward as needed.

For this tutorial I am using Raspbian. I recommend it because it is an optimized version of Debian built specifically for Raspberry Pi’s. The last stable downloads can be found here.

To flash your SD Card, you will need to unzip the image and write it your SD card using Win32DiskImager. This tool can also be used to after our initial setup to create an image of our finalized implementation(very useful as a backup).

windiskimager

After the image is flashed, you can boot your device. At this point you can use your HDMI Cable/Mouse/Keyboard for your initial configuration, or you can use an SSH Client like Putty to connect. The default hostname, login, and password are as follows:

host:raspberrypi
Username: pi
Password: raspberry

raspberry pi putty

For this tutorial, I will be using putty. On first boot, you will be prompted with a configuration tool called Raspi-Config. If the raspi-config doesnt load automatically, just enter the following command from the shell to get started.

sudo raspi-config

raspi-config-screenshot

The settings I recommend you update are

update
expand_rootfs
change_pass
change_timezone
memory_split

The usual distribution images are 2 GB. When you copy the image to a larger SD card you have a portion of that card unused. expand_rootfs expands the initial image to expand to fill the rest of the SD card, giving you more space. By default, 64mb is reserved for the Graphical UI. Since we plan on using this as a WiFi Hotspot, we can reduce this to 32mb with the memory_split command.

After you finish your changes to the raspi-config, you should reboot your pi using the following command:

sudo shutdown -r now

At this point you have a fully functional linux server, but we still need to check our network card and setup our WiFi hotspot.

Prerequisites

The first thing you need to do is make sure you have an existing wired connection to your rPi. After that, you need to install the following packages.

sudo apt-get install bridge-utils hostapd

The whole crux of the issue is that it is the apt hosted copy of hostapd that is not compatible with the RTL8188CUS chipset. But, thanks to the Edimax team, I’ve got a replacement hostapd binary to resolve this issue. This tutorial will not work without it.

To download and replace the installed binary version of hostapd we just installed, issue the following commands:

wget http://www.daveconroy.com/wp3/wp-content/uploads/2013/07/hostapd.zip
unzip hostapd.zip 
sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.bak
sudo mv hostapd /usr/sbin/hostapd.edimax 
sudo ln -sf /usr/sbin/hostapd.edimax /usr/sbin/hostapd 
sudo chown root.root /usr/sbin/hostapd 
sudo chmod 755 /usr/sbin/hostapd

*Note, some people don’t like the idea of installing from untrusted 3rd parties, so if If would rather compile your own binary, you can download the realtek driver here . You will have to navigate to the ~/RTL8188C_8192C_USB_linux_v3.4.4_4749.20121105/wpa_supplicant_hostapd/wpa_supplicant_hostapd-0.8/hostapd and run a make, make install, then hostapd like i did above.

Bridge the Network Connections

Now that we have the proper hostapd installed, we need to create a bridge between our ethernet connection and our Edimax Wireless card. To do this, we need to edit our network interfaces:

sudo nano /etc/network/interfaces

To avoid any conflicts, you need to delete or comment out(#) any lines conatining wlan0 and then add the following text to bridge the connections, assuming your ethernet and wireless adapters are named eth0 and wlan0( use ifconfig -a to check)

auto br0
iface br0 inet dhcp
bridge_ports eth0 wlan0

The resulting file should look like this:

#loopback adapter
auto lo
iface lo inet loopback

#wired adapter
iface eth0 inet dhcp

#bridge
auto br0
iface br0 inet dhcp
bridge_ports eth0 wlan0

Configuring Hostapd

Now that our interfaces are setup, we need to configure hostapd. To do so, create the following file

sudo nano /etc/hostapd/hostapd.conf

with the following contents:

interface=wlan0
driver=rtl871xdrv
bridge=br0
ssid=DaveConroyPi
channel=1
wmm_enabled=0
wpa=1
wpa_passphrase=ConroyPi
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
auth_algs=1
macaddr_acl=0

Wrapping Up

We should be finished now, and afer a reboot

sudo reboot

you can test your configuration using the following command

sudo hostapd -dd /etc/hostapd/hostapd.conf

If it runs as expected, you can add it to startup by editing

sudo nano /etc/default/hostapd

and uncommenting and updating the following line

DAEMON_CONF="/etc/hostapd/hostapd.conf"

That should do it! I’m happy to answer any questions, just comment on this post. Thanks for reading!Raspberry Pi as WiFi Hotspot

Read More

How to Tether Your Raspberry Pi with your iPhone 5

If you have been reading my blog lately you know that I have done a lot of projects involving Raspberry Pi‘s, the $35 dollar credit card sized Linux computer. Because of its small size, I recently started of thinking of projects that would allow me to be mobile with the Pi.

That led me to this post.

There are plenty of resources online about tethering the iPhone 5 with the graphical interface, but I wanted to be able ‘hotswap’ my Raspberry Pi’s Internet connection without command line, and without the graphical interface. Plug and play, essentially.

I was able to get this working last night using Raspian (a free operating system based on Debian optimized for the Raspberry Pi hardware) and a few scripts.

Prerequisites

Make sure your iPhone is disconnected and proceed to install the following iPhone and file system utilities. Open up a terminal or SSH into your Pi and run the following commands:

sudo apt-get install gvfs ipheth-utils
sudo apt-get install libimobiledevice-utils gvfs-backends gvfs-bin gvfs-fuse

After these are installed, and with your iPhone still not plugged in, lets take a look at our available interfaces. Run the following commands:

ifconfig -s

If you have a standard installation, you should see two interfaces, eth0 and lo.

tether iphone 5 raspberry pi

The reason we did this step was to make sure we correctly identified the name of the iPhone’s network interface. Now that you have this list you can turn on your iPhone’s Personal Hotspot and then plug it into your Raspberry Pi’s USB port, and issue the same command.

ifconfig -s

iphone raspberry pi

Your Pi should now recognize your iPhone is plugged in and assign its network interface a name, usually eth1. Now we need to edit our interfaces file to allow for hotswapping and dchp.

Unplug your iPhone and then open up your network interfaces file

sudo nano /etc/network/interfaces

and add the following lines, where eth1 is the name identified above.

allow-hotplug eth1
iface eth1 inet dhcp

Save and close the interfaces file.

Graphical Interface

If you are using the graphical interface, you are now done and the above commands were all that were necessary to use your iPhone as an Internet connection!

You can test this by starting your desktop environment

startx

Then unplug your Ethernet connection, start your Personal Hotspot, and plug in your iPhone. A popup message saying a “Removable medium is inserted” , that is safe to close. Then open a terminal, and run the following command:

ping google.com

You should be connected to the Internet!

Command Line

In my testing, tethering with command line only will only work when the iPhone is mounted. To do this, we need the ifuse package:

sudo aptitude install ifuse

and create a mount directory for the iPhone

sudo mkdir /media/iPhone

Now we can disconnect the ethernet of the Pi, turn on the iPhone’s Hotspot , and plug in then mount the iPhone.

sudo ifuse /media/iPhone

The Internet should now be tethered from your iPhone. Remember, while using command prompt only, the iPhone must manually be mounted each time it is plugged in using the ifuse command.

Putting It All Together

Now that we know how to tether the phone in the graphical interface, and the command line interface, we can add a script(thanks to users djpod and benebeck on the raspberrypi.org forums) that will automatically mount the iPhone it is plugged in.

To automate the process, I put the following script named iphoneconnect in /lib/udev/:

sudo nano /lib/udev/iphoneconnect

#!/bin/bash
umount /media/iPhone #when the iPhone is unplugged, it is not automatically unmounted.
ifuse /media/iPhone
ipheth_pair

Remember to make the script executable

sudo chmod 755 /lib/udev/iphoneconnect

The final step is to edit the file ’90-iphone-tether.rules’ in the ‘/lib/udev/rules.d’ directory.

sudo nano /lib/udev/rules.d/90-iphone-tether.rules

This is the script that runs automatically when ever we plug in the iPhone, and we want to edit it to call the script we created (/lib/udev/iphoneconnect) instead of what it currently calls (/lib/udev/ipheth_pair). To do this, we just edit the 5th line of the file replacing ipheth_pair with iphoneconnect.

The file should look like this


# udev rules for setting correct configuration and pairing on tethered iPhones
ATTR{idVendor}!="05ac", GOTO="ipheth_rules_end"

# Execute pairing program when appropriate
ACTION=="add", SUBSYSTEM=="net", ENV{ID_USB_DRIVER}=="ipheth", SYMLINK+="iphone", RUN+="iphoneconnect"

LABEL="ipheth_rules_end"

Thats it! Your Raspberry Pi will now automatically Tether your iPhone without Graphical Interface and without Command line!

 

 

 

iPhone Tethered Raspberry Pi

 

My command line speed tests averaged at about 1.5mb/s download over Verizon LTE neat Boston, MA.

wget --output-document=/dev/null http://speedtest.wdc01.softlayer.com/downloads/test500.zip
Read More