5 Comments

Wireless Communication Between Raspberry Pi and Your Computer

Imagine for a moment that you want to develop a product that uses a Raspberry Pi. Let’s say that this product requires the ability to connect up to a wireless network. Maybe it’s a device that streams music from an online web-service, or maybe it hosts some type of webpage. This functionality should be easy to accomplish with the amount of power that the Pi has to offer, but how would a typical user configure it to connect up to their network? I can see several possible options.

Raspberry Pi Wireless Options

You could include a display and some type of keyboard input that customers can use to enter their network credentials. This option would be very simple to use, but it would require a lot of extra cost if the display and keyboard aren’t necessary for normal operation of the device.

A second option would be to require that the users provide their own display and keyboard for configuration. This would also work well but could be a major inconvenience for users who don’t happen to have an extra television or computer monitor laying around.

Wouldn’t it be nice if there was a third option that would allow allow users to temporarily connect up to your device wirelessly from their personal computer, and then configure the device using a simple web interface? Fortunately they can, using something called an ad hoc wireless network.

The typical LAN wireless network that we’re used to is called an infrastructure network. It uses a router or access point to allow devices to share a wired connection the internet or other resources. But in an ad-hoc network configuration (also known as a peer-to-peer network), devices connect directly to each other and communicate without the use of an access point or router.

Setting Up an Ad-hoc Wireless Network with Linux

In the rest of this post, I will demonstrate how to configure the Raspberry Pi to broadcast an ad hoc network using only the Linux command-line. I will also show how to enable DHCP so that users do not have to configure their computers with a static IP address.

Basic Setup

I am starting with a fresh installation of the Raspian Wheezy distro, which you can find on the Raspberry Pi downloads page.

You will also need a wireless ethernet adapter connected to your Pi. I use the Edimax ED-7811Un, which works really well and doesn’t require any driver installation when used on Linux!

Broadcasting an Ad-Hoc Network

First we need to modify the network interfaces file that Debian uses to configure the network. I will be using vi (the built-in text editor) to edit files. You can also use nano, which is simpler if you are unfamiliar with vi; just substitute “nano” for “vi” in the following commands.

Make a backup copy of the network interfaces file:

1
sudo cp /etc/network/interfaces /etc/network/interfaces_backup

Open the network interfaces file for editing:

1
sudo vi /etc/network/interfaces

Edit the file so that it contains the following and then save and close:

1
2
3
4
5
6
7
8
9
10
11
auto lo
iface lo inet loopback
iface eth0 inet dhcp
 
auto wlan0
iface wlan0 inet static
  address 192.168.1.1
  netmask 255.255.255.0
  wireless-channel 1
  wireless-essid RPiAdHocNetwork
  wireless-mode ad-hoc

Next, restart the wlan0 network connection with the following two commands:

1
2
sudo ifdown wlan0
sudo ifup wlan0

If you have already configured your Pi to connect to an infrastructure network in the past, you might have to use the -f option to force the new changes.

Now, on your computer, refresh the list of available wireless networks and you should see a new one called RPiAdHocNetwork! Pretty cool, eh?

Configuring the Raspberry Pi as a DHCP Server

Well, there is one thing left to do. With the current setup, the Raspberry Pi is assigning itself the IP address 192.168.1.1. Normally, when you connect your computer to a WiFi network it gets an IP addressed assigned to it from a machine that is configured as the DHCP server. Often times the DHCP server is your router. Well, in our ad-hoc network we don’t have a DHCP server. This means if you try to connect to the new network that the Raspberry Pi is broadcasting, no one will assign your computer an IP address, and your computer will likely assign itself one in the 169.254.x.x range. If that happens, you will not be on the same subnet as the Raspberry Pi, so you will not be able to communicate with it.

To resolve this problem, we will configure the Raspberry Pi as a DHCP server so that it assigns IP addresses to whoever connects to its ad hoc network.

Connect your Raspberry Pi to the internet by connecting an ethernet cable or other means and install the dhcp server package:

1
2
sudo apt-get update
sudo apt-get install dhcp3-server

Once that finishes, we just have to configure the DHCP server. The default configuration file is now located at /etc/dhcp/dhcpd.conf. Update the file so that it contains the following:

1
2
3
4
5
6
7
8
ddns-update-style interim;
default-lease-time 600;
max-lease-time 7200;
authoritative;
log-facility local7;
subnet 192.168.1.0 netmask 255.255.255.0 {
  range 192.168.1.5 192.168.1.150;
}

Now restart the Raspberry Pi and connect up to the ad hoc network RPiAdHocNetwork. The Pi will assign your computer an IP Address on the same subnet as itself so you can ping it and ssh into it for full control! To connect with ssh, try the following:

1
ssh -lpi 192.168.1.1

How Can I Use This?

I started off by suggesting that an ad hoc network could be used as a way for customers to configure a device to connect up their personal infrastructure network. To achieve that, you would simply set up a web server on the Raspberry Pi that serves up a configuration page. The configuration page could display a list of all the wireless networks in range. Users could connect up to the ad hoc network, view the configuration web page, select their personal wireless network, and then enter the appropriate network credentials. The Raspberry Pi could then switch the newly configured network through which it could get access to the internet or other resources.