I have a Wifi dongle for my Raspberry Pi and have it automatically connecting to 2 different Wifi networks via the wpa_config GUI.

How can I get my raspberry pi to set up an AD HOC network if it can't connect to any of the Wifi networks on the list?


See my answer here to a similar question.

I don't think you are going to be able to make an "auto-sensing" device that can switch from wifi client to wifi hotspot, but maybe you can put together a script that, on boot, checks to see if wifi client has connected, and if not, re-configures the necessary .conf files and forces a reboot.

  • Do you know how I would go about checking to see if the pi is connected and setting up the wifi hotspot? I am rather new to this kind of thing :) – starbeamrainbowlabs Jun 18 '13 at 18:59

I wrote a script to do something like this a while ago, I ran the script every minute with cron.

# Interface checker
# Checks to see whether interface has an IP address, if it doesn't assume it's down and start hostapd
# Author : SirLagz
echo "-----------------------------------"
echo "Checking connectivity of $Interface"
NetworkUp=`/sbin/ifconfig $Interface`
IP=`echo "$NetworkUp" | grep inet | wc -l`
if [[ $IP -eq 0 ]]; then
echo "Connection is down"
hostapd=`pidof hostapd`
if [[ -z $hostapd ]]; then
# If there are any more actions required when the interface goes down, add them here
echo "Attempting to start hostapd"
/etc/init.d/hostapd start
echo "Attempting to start dnsmasq"
/etc/init.d/dnsmasq start
echo "Setting IP Address for wlan0"
/sbin/ifconfig wlan0 $HostAPDIP netmask up
elif [[ $IP -eq 1 && $NetworkUp =~ $HostAPDIP ]]; then
echo "IP is $HostAPDIP - hostapd is running"
echo "Connection is up"
hostapd=`pidof hostapd`
if [[ ! -z $hostapd ]]; then
echo "Attempting to stop hostapd"
/etc/init.d/hostapd stop
echo "Attempting to stop dnsmasq"
/etc/init.d/dnsmasq stop
echo "Renewing IP Address for $Interface"
/sbin/dhclient wlan0
echo "-----------------------------------"
  • Thank you for your reply! Where can I find instructions on how to run a script with cron? – starbeamrainbowlabs Jul 8 '13 at 6:49
  • man crontab will help :) – Lawrence Jul 9 '13 at 1:15

You will need to take a look at some scripting tutorials for working with wireless. To answer the question left in the comment of Jim's answer: StackExchange Ubuntu had a solution.

Take this and modify it to copy the correct config file into the proper place based on if you are connected or not, then restart networking.

You will want to have your configs figured out, tested and working ahead of time, save them separately(One for adhoc, one for normal use), and then use the script to determine which one to use.

Bash would probably be my language of choice for this particular task.

Once you have your script working, you can run it on boot with a cron job, and you should be good to go.

Good luck! Share your code when you are done please!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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