I'd like to create a script that tries to connect me to the WiFi specified by my wpa_supplicant conf, and if it fails, create an ad-hoc network. But I'm struggling with it.

Here is what I have done so far:


# create adhoc network function
    echo "Creating ad-hoc network"
    ifconfig wlan0 down
    iwconfig wlan0 mode ad-hoc
    iwconfig wlan0 essid AccessPoint
    ifconfig wlan0 netmask up
    echo "Ad-hoc network created"

# connect to wifi function
    echo "Trying to connect to configured wifi"
    ifdown wlan0
    wpa_supplicant -B -i wlan0 -c /home/pi/pics/wpa_supplicant.conf
    ifup wlan0
    echo "Wifi configured"

echo "================================="
echo "Wifi setup"
echo "================================="
echo "Checking connectivity..."
sleep 5s
ping -c 5 -I wlan0 google.com > /dev/null 2>&1
echo "Status"
if [ $PINGSTATUS -eq 0 ];
        echo "Connected to WiFi"
        echo "Wifi not connected, fallback to ad-hoc"

exit 0

It seems it connects to the configured Wi=Fi, but not for the ad-hoc.

Here is my /etc/network/interfaces :

# start interfaces upon start of the system
auto lo wlan0

# register loopback interface
iface lo inet loopback

# use dhcp and allow interface to be started when kernel detects a hotplug event
allow-hotplug eth0
iface eth0 inet dhcp

# use manual ip configuration for wlan0 interface and allow hotplug as well
allow-hotplug wlan0
iface wlan0 inet dhcp

And my /etc/dhcp/dhcpd.conf:

ddns-update-style interim;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet netmask {

I can see the ad-hoc network from another computer, but I cannot connect to it, any ideas?

  • just curious, have you been able to make this work? It's such a cool idea and there's really no clear docs or examples on how to get it working right.
    – Andres
    Apr 8, 2016 at 5:17
  • @Andres Well, no... Apr 8, 2016 at 6:45

1 Answer 1


It is an issue of your WiFi Dongle. If you are using Edimax 7811UN it will not work with Linux Kernels past 3.8+ versions for ad-hoc mode functions. This has been examined by me and this query will assure you of it.

I suggest to use Ralink Based RT5370 Chipset dongles. They work well with ad-hoc mode as I use Ad-hoc mode everyday with almost 10 RPis.

  • I use rapsberry pi 3 with built in wifi Apr 4, 2016 at 22:44
  • what is the on board WiFi adapter manufacture? can you do an lsusb and let me know?
    – Shan-Desai
    Apr 5, 2016 at 14:46
  • there's nothing wrong with this adapter in ad-hoc mode since I can configure it OK while outside my script. Apr 5, 2016 at 15:51
  • wait. you want to connect your Pi in ad hoc mode in order to use it as an Access-Point? You do know that ad-hoc mode has no access-point feature right?
    – Shan-Desai
    Apr 5, 2016 at 17:02
  • I just want the Pi to connect the wifi configured in my wpa_supplicant conf. And if this wifi is not reachable, fallback to ad-hoc so I can connect to this wifi from another computer and go to a special page served on the Pi to configure another wifi. Apr 5, 2016 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.