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:

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

Open the network interfaces file for editing:

sudo vi /etc/network/interfaces

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

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:

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:

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:

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:

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.
 

Conversation
  • Matt Shirilla says:

    I’ve been curious about the Pi and Wi-Fi for a while, thanks for the read. Have you been able to put the interface into monitor mode? The Pi would make a good low cost Wi-Fi sensor.

  • Thomas says:

    Nice post, definitely a good tutorial for beginners. You might want to add a section about avahi though, to make the setup truely ad-hoc, and eliminate the need for a dhcp server, which a) can be a hassle at times and b) is really somewhat overkill just for an ad-hoc network

  • Mahmoud Zalt says:

    I’ve been looking for such solution for the past 3 days.
    Thank you :D

  • Ollie says:

    I liked the tutorial, but got stuck a step one. Ad-Hoc is not visible !

    pi@raspberrypi /etc/network $ cat interfaces
    auto lo
    iface lo inet loopback

    auto eth0
    iface eth0 inet dhcp

    auto wlan0
    iface wlan0 inet static
    address 192.168.1.1
    netmask 255.255.255.0
    wireless-channel 4
    wireless-essid PiNet
    wireless-mode ad-hoc

  • Shyam says:

    Hi,

    Nice post. But nowhere have you mentioned how to communicate on wlan0 between the two (isolated PI’s) [as per your post here (http://www.raspberrypi.org/phpBB3/viewtopic.php?t=39927&p=331286)

    In my case, I have two RPis configured with same configuration. Configured in Adhoc, with ESSID channel 10 settings. Both are on the same subnet.

    The problem is I cannot see one RPi from the other. Atleast, they should be able to ping each other am I correct? Where am i going wrong here?

    Thanks, hope to have your help in here!

  • Talha says:

    Thank you so much for this tutorial. It saved us hours of debugging.

  • Nasim says:

    Dear Sir,

    Can you please tell me how to broadcast the UDP data between two Raspberry Pi’s.
    Both raspberry Pi’s are pinging each other through sudo arp -s but they are not able to send broadcast message to each other

    Best Regards
    Nasim Abbas

  • Shane Kirkbride says:

    This is out of date and does not work….

  • tanzeel rana says:

    I can’t get this working

  • Juan Castellanos-Smith says:

    This is working for me between a Dell laptop and a Raspberry Pi 3. I have been able to manipulate Pi 3 files. I also wrote a program and executed a C program on my laptop onto the Pi 3 through the wireless SSH connection.

    The “dhcp-server” package name has changed. (See https://help.ubuntu.com/community/isc-dhcp-server)
    To install:
    >> sudo apt-get install isc-dhcp-server

    After following the rest of the tutorial and opening PUTTY on your laptop, type in the IP address of your Pi:
    192.168.1.1

    Type in your username (default is: pi)
    Type in your password (default is: raspberry)

    At this point you should be in your “pi” directory and there you go!
    Oh and neither the computer nor Pi is connected to the internet if all you have is a wireless connection between the 2, which is a pain. You can disconnect your laptop from the adhoc network and reconnect to the internet or do the same on the Pi.

    To reconnect the Pi to the internet, I simply replace the “interface” file with the original, unedited “backup” version:
    >> sudo cp /etc/network/interfaces_backup /etc/network/interfaces
    >> sudo ifdown wlan0
    >> sudo ifup wlan0

    To reestablish the adhoc network, I simply replace the “interface” with the modified adhoc interface file:
    >> sudo cp /etc/network/interfaces_juan /etc/network/interfaces
    >> sudo ifdown wlan0
    >> sudo ifup wlan0

  • Juan Castellanos-Smith says:

    Edit:
    To go from adhoc to internet(no sudo prefix is needed):
    cp /etc/network/interfaces_backup /etc/network/interfaces

  • John Hadley says:

    Yes this a cool post Jordan
    Pi as a WiFi hot-spot makes great sense we need waiting room customer access outside our Guest network.

  • Sid says:

    I’m trying to control an RPi 2 (jesse) with an ad hoc wireless connection from an iPad (IOS 10). I set up the RPi according to your directions, except I’ve made the subnet 192.168.2 because there’s a router 192.168.1.1 in my vicinity. Scanning for wifi sources with the iPad I see RPiAdHocNetwork and connect to it, but when I try to log on my terminal software says “cannot connect to host 192.168.2.1 port 23”. Incidentally I have Fing software on the iPad which reports RPiAdHocNetwork with two devices: 192.168.2.1 and 168.192.2.5 (the iPad). If I try to connect a windows 10 laptop I see RPiAdHocNetwork in the list of sources but get “can’t connect to this network”.

    I’d really appreciate your thoughts on how to get past this.

  • Nigel Trewartha says:

    A nice article, but due to Jessie the /etc/interfaces are outdated an systemd needs to be changed.

    I was looking for a wireless – not using Wlan – solution. I am looking at XBee as carrrier.

    I would like some advice on this (conform to EU regulations).

  • revenger says:

    Hi thank you for this great tuto ! Regarding “How Can I Use This?” part, could you please provide an example of how to setup this web server and create the web page wifi configuration ?

    Thanks a lot !

  • Comments are closed.