Jan 222013
 

Hey Everyone

I’ve had issues with my WiFi in the past, and with my Pi running headless it’s sometimes a pain to get it connected back up to WiFi, so I’ve created this little script to start hostapd and dnsmasq whenever the WiFi connection went down, which allowed me to SSH into the Pi even though it wasn’t connected to the network, because it was broadcasting it’s own network !

This can be customised to do different things if either WiFi or ethernet go down.


#!/bin/bash
#
# Interface checker
# Checks to see whether interface has an IP address, if it doesn't assume it's down and start hostapd
# Author : SirLagz
#
Interface='wlan0'
HostAPDIP='10.0.0.1'
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 255.255.255.0 up
fi
elif [[ $IP -eq 1 && $NetworkUp =~ $HostAPDIP ]]; then
echo "IP is $HostAPDIP - hostapd is running"
else
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
fi
fi
echo "-----------------------------------"

Share

  27 Responses to “Script : Starting hostapd when WiFi goes down”

  1. […] This is an interesting solution to wifi on a headless computer – when no wifi access point is found, automagically flip over to becoming an access point so it’s still accessible locally! From http://sirlagz.net/: […]

  2. Do you need to run this script as a cron job or simply call it through .bashrc?
    Thanks for sharing.

    • Set it up as a cronjob. That way it checks at regular intervals and brings up hostapd when it picks up that the wifi connection has gone down

  3. Hi SirLagz,
    You must be using your wifi in AP mode. I wonder if you have experienced the problem I am having. My adapter uses the rt3072 chip and the rt2800usb /cfg80211 drivers. It works well as a wifi client when power save mode is switched off. It works in AP mode but I’m experiencing a situation where something appears to go to sleep. I’m running 3.6.11+ #371, with nothing extra except hostapd, dnsmasq. I’m not running a bridge at this stage. The way I’m testing this problem is to have the pi headless with no ethernet and just one client. From the client, I run a ssh session running something like top at 0.01 seconds to generate some traffic and cpu load. I also have the client ping the pi every second. The ping delays are generally pretty good 1-3ms with occasional bursts of around 100-300ms. But then I get these timeouts in the ping for 5 seconds at time every 2 or 3 minutes or worse if HT/WMM/n mode are active where the timeouts are more frequent and much longer – typically 15 – 20 seconds. I’ve tried changing niceness of hostapd with no effect. I’m wondering what pings are like on your setup and whether you or anyone has any suggestions. I would reallly love to solve this.
    cheers Ivo

    • Have you tried turning power management off when it’s in AP mode ?

      • Thanks SirLagz, yes I have tried that. It is not available in AP mode. I think it lets the adapter processor go to sleep. You can see the effect in in client mode as ping latency is totally random – 2ms to 400ms (plus occasional timeouts) – I guess the time to wake up the adapter processor. When you switch off power saving the ping times are in general very steady ~ 2-3ms, with occasional delays maybe up to 200ms (no noticeable timeouts). However in AP mode, although it’s status reads as power save on, the ping times are generally stable 3-4ms. When you load things up though, with say top running 0.01 sec, the ping latency drops to around 1-2ms, apart from the timeouts, which are really bad with n-mode active. I wonder why other people using cfg80211 aren’t experiencing this? I have the simplest of setups!

        • What happens when you try to turn power management off ?
          Have you tried with G-Mode ?

          Cant say I’ve done a huge ping test or anything but performance is generally good.
          I’ll do some testing tonight for you though :)

          • That would be awesome thanks.
            I can’t recall whether I tested power save mode in both g and n modes. In ‘n’, I get in response to:
            sudo iw wlan0 set power_save off
            command failed: Operation not supported (-95)
            ****
            Just tried in g mode and I get the same response.
            Thanks

          • Interesting.
            What’s the exact device that you’re using ?
            Might see if I can obtain one myself and do some testing :) If I can afford it that is haha.

            Also what kernel are you using ?

          • My device is a
            Digitech computer high power wireless-n usb adapter.
            I got in in New Zealand:
            http://www.jaycar.co.nz/productView.asp?ID=YN8306

          • Good Ol’ jaycar :D
            Unfortunately out of my budget for now but I’ll try a few things when I get home anyway :)

          • oops kernel is: Linux raspberrypi 3.6.11+ #371 PREEMPT Thu Feb 7 16:31:35 GMT 2013

          • Running of the Pi’s AP now, and did a 100 packet ping, all came back within a second and a half with a few outliers.
            You were right about the powersaving also, I can’t turn power saving off in AP mode. I must have remembered wrong :)

            At the moment, My AP is running in G mode as most devices I have are G only devices. Not sure if that makes any difference though.

          • Thanks for doing that test. 1.5 seconds is a very long time if all you have is the AP and a client.. That would be consistent with my experience in g mode. Did you find your typical time was around the 1-3 ms range? Are you able to test in n mode with top running at 0.01 secs? I would expects your ping times to have much greater extremes.

        • Actually mine is worse, I can get 5 second delays maybe 10 to 15 minutes apart. But I suspect the same thing may be going on. Whether 5 seconds or 1.5 seconds, its still an extremely long time. During that time, my pi is about 95 % idle!

          • I did a test with another computer that is connected via wifi to an actual Wifi AP – a dlink of some sort – and the ping times were even worse haha

            I’ll see if I can do a test with n mode tonight

          • I just tried my home router/AP which is a dlink. In 20 minutes, the worst I got was 110ms. That was rare. Typical was around 3ms. I will try my work router tomorrow.

          • Work AP/router much the same – typical 3-5ms occasional 250ms, one 314ms in 40 minutes ~ 2400 pings.

          • Sounds like WiFi won’t get below 1ms in pingtimes then. I doubt the Pi is the problem with the Ping times there, though I don’t seem to be getting the drop out issues that you are having.
            Perhaps try and ping [i]out[/i] from the Pi, say setup a cronjob to run every minute, and just send out one ping to another device and see if that keeps it going ?
            At least that will tell us whether it’s power management playing up or something else.

          • I’m wondering if this has something to do with un-handled interrupts – just a guess!
            :-) . With my pi in N mode and HT on, and no ssh data traffic to speak of, performance for the most part was excellent until I got a 5 second timeout after nearly 20 minutes. However as soon as I started increasing ssh traffic – I use top as it is easy to change the data rate on the fly, I got long delays ~ 15 to 20 seconds more frequently. If I had 20kB/s, I get a 15 second timeout after 4 minutes. If I increase the data rate to about 75-80kB/s (d=0.01 with largish shell window), 15-20 second delays were very frequent. In this latter case, interrupts are up around 3,500/s.
            There are some very strange things going on though. For example, if after the above I:
            stop hostapd
            modify hostapd.conf to disable HT mode
            start hostapd
            run a shell session
            disconnect shell session
            stop hostapd

            The interrupt rate jumps to 8100/s !
            The same thing happens if I just unplug the adapter from the usb socket. There is nothing I can do to stop the interrupts without rebooting the pi.
            After removing the dongle, lsusb still shows all the driver stuff present – it has not been removed.

            Don’t know where to from here :-(

          • Sorry, may have confused you. My comment about the work AP router and home router had nothing to do with pi. I was just testing to see what the response was from general purpose equipment.
            What kind of adapter are you using? I will try and get one if I can. Does it use rt2800usb.ko driver?

          • I think I confused myself haha.
            I’m using a RT5370, $5 off ebay haha.
            Yep, uses the RT2800usb driver.

            I have no idea why your dongle seems to be doing that.
            I’ve been using my RaspAP and it has been working fine for me, no noticeable delays.

          • Thanks for your help buddy…really appreciate it :-). One good thing about problems is that you learn something. I got the pi to learn about linux, so I guess I’m getting what I wanted!!

          • No probs, been fun trying to figure this stuff out haha

  4. Just been checking out your script, like most people that I’ve been reading about on here and various other pages my wifi just drops for no reason, it normally lasts anywhere between 5 and 30 mins and then I have to reboot the Pi sometimes twice until it starts working again. Your script works fine and I put it into crontab but it didn’t fix my problem.

    I’ve been reading everything I could find on what may be the problem and have tried many different things. I thought may be there was a suspend or hibernate mode on the Pi or wifi but there isn’t. Then I started reading

    http://forum.doozan.com/read.php?2,6300

    By the way my wifi is a Alfa AWUSO36NH

    In the above link quite a way down it was suggested that these kind of wifi devices aren’t really ment to be used as routers and that it might be over heating.

    Anyway I used this cmd to reduce the txpower “iwconfig wlan0 txpower 14″ magic number 14.
    Since I’ve used this the whole thing has become much much more stable, I ran it all last night and today, randomly connecting to it and browsing and copying files across. It hasn’t gone down yet and is now something I can use :) Just thought I would add this in case any one else was as stuck in what to do as me.

  5. […] : WiFi Checker Script Script : Starting hostapd when WiFi goes down How To : Turn the Raspberry Pi Into a Shaping WiFi Router Script : Web Configuration Page for […]

  6. […] about Conroy’s setup, so I used this guy’s setup instead. He also provide a neat little launch AP failover […]

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>