Creating an Ad-hoc network for your Raspberry Pi

I have found this setup to be very useful for my Raspberry Pi.
Instead of connecting to an existing wireless network, you have the Pi create it’s own. That way you can login to the Pi even if a wi-fi network is not available. I’m using it for my Pi Powered robots outside.

Hopefully, this little snippet of code will be useful to others:

Here are the steps:
=====================

  1. From the command line (LX terminal or SSH) go to the following directory.
    cd /etc/network
  2. Copy the existing interfaces file as a backup.
    sudo cp interfaces interfaces-wifi
  3. Create a new file for our ad-hoc network.
    sudo nano interfaces-adhoc
  4. Once this file is open, copy the code snippet below so that it reads as below.
    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 RPiwireless
    wireless-mode ad-hoc
    
    Once you've entered this do a Ctrl-X, yes and enter to save the file.
  5. Now we have a interfaces-wifi and interfaces-adhoc files in the /etc/network directory along with your original interfaces file. Now, we can switch between using a wireless connection and our ad-hoc network by simply copying the appropriate file over to the interfaces file.
  6. For example, if you want to use the ad-hoc network enter the following command >
    sudo cp /etc/network/interfaces-adhoc interfaces

    To switch back to a wifi network do the following:

    sudo cp /etc/network/interfaces-wifi interfaces
  7. You’ll need to install a package to allow your Pi to assign a device connecting to it an IP address.
    sudo apt-get install isc-dhcp-server

    It creates a config file that we will need to edit. You can access it using the code below.

    sudo nano /etc/dhcp/dhcpd.conf

    Once you have this open, edit it so that it only contains the code below.

    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;
    }
  8. Save this config file and reboot.
  9. When you boot up now under ad-hoc mode you will need to use your computer’s wifi to search and find the new network called PiWireless. Connect to it and then use SSH or VNC to connect to the IP address we assigned of 192.168.1.1Hope this helps.

3 thoughts on “Creating an Ad-hoc network for your Raspberry Pi

  1. Hi! I did all the steps… now i can see the RPiwireless only with the smartphone. But i am not able to connect to it!
    Why? Isn’t necessary the hostapd?
    Many thanks!

    1. Is your smartphone Android or Iphone? The reason I ask is because with Android phones you cannot connect to an Ad-hoc network unless you ROOT the phone.
      Let me know.

Leave a Reply