cubieplayer / Cubian

Debian for Cubieboard
http://cubian.org
271 stars 49 forks source link

WiFi doesn't reconnect on signal loss? #340

Open roger- opened 9 years ago

roger- commented 9 years ago

I've noticed that my WiFi goes down frequently so I have to physically log in and issue a sudo ifup wlan0 to get network access again.

Is there anyway to make it auto-reconnect?

mzapa commented 9 years ago

One approach is write a watchdog script and add an entry to the crontab that kicks off that script every 1 minute or whatever time period you desire.

When logged in to the cubieboard via the serial console and you discover the wifi is not functioning: Can you ping the wifi interface? Can you ping the gateway?

Let's say you get a proper ping response from the wifi interface but an improper ping response when trying the gateway.

Your script would start. Your script would send a single ping to the wifi interface. Your script parses the response and it's determined to be proper. Your script would send a single ping to the gateway. Your script parses the response and it's determined to be improper. Your script would then kick off the ifdown then the ifup on the wifi interface. Your script would record the event details in the custom log file for later analysis. Your script would end.

On the next polling period, if the ping response is proper, your script ends normally until the next polling period.

roger- commented 9 years ago

Thanks, I found a script that seems like it should do what you described (or close enough).

I'm surprised there isn't a standard way of handling this though, because it seems like it would be a common issue. Maybe use a daemon like wicd should be a default package?

michalliu commented 9 years ago

Network configuration, on a Debian System, is stored in /etc/network.

In this directory you’ll find :

interfaces : this file describe your NICs according to interfaces(5) (check this man page to see how to setup the interface, fixed IP or DHCP, gateway, netmask, and so on)

  1. if-pre-up.d : directory with scripts which will get run before bringing up an interface
  2. if-up.d : directory with scripts which will get run right after bringing up an interface
  3. if-down.d : directory with scripts which will get run before bringing down an interface
  4. if-post-down.d : directory with scripts which will get run after bringing down an interface
  5. run/ifstate : the current state of the NICs

Put a script in if-post-down.d should work, thought i did not test it.

michalliu commented 9 years ago

I'd like to attach the code here in case of the author deletes the code

#!/bin/bash
##################################################################
# A Project of TNET Services, Inc
#
# Title:     WiFi_Check
# Author:    Kevin Reed (Dweeber)
#            dweeber.dweebs@gmail.com
# Project:   Raspberry Pi Stuff
#
# Copyright: Copyright (c) 2012 Kevin Reed <kreed@tnet.com>
#            https://github.com/dweeber/WiFi_Check
#
# Purpose:
#
# Script checks to see if WiFi has a network IP and if not
# restart WiFi
#
# Uses a lock file which prevents the script from running more
# than one at a time.  If lockfile is old, it removes it
#
# Instructions:
#
# o Install where you want to run it from like /usr/local/bin
# o chmod 0755 /usr/local/bin/WiFi_Check
# o Add to crontab
#
# Run Every 5 mins - Seems like ever min is over kill unless
# this is a very common problem.  If once a min change */5 to *
# once every 2 mins */5 to */2 ... 
#
# */5 * * * * /usr/local/bin/WiFi_Check 
#
##################################################################
# Settings
# Where and what you want to call the Lockfile
lockfile='/var/run/WiFi_Check.pid'
# Which Interface do you want to check/fix
wlan='wlan0'
##################################################################
echo
echo "Starting WiFi check for $wlan"
date
echo 

# Check to see if there is a lock file
if [ -e $lockfile ]; then
    # A lockfile exists... Lets check to see if it is still valid
    pid=`cat $lockfile`
    if kill -0 &>1 > /dev/null $pid; then
        # Still Valid... lets let it be...
        #echo "Process still running, Lockfile valid"
        exit 1
    else
        # Old Lockfile, Remove it
        #echo "Old lockfile, Removing Lockfile"
        rm $lockfile
    fi
fi
# If we get here, set a lock file using our current PID#
#echo "Setting Lockfile"
echo $$ > $lockfile

# We can perform check
echo "Performing Network check for $wlan"
if ifconfig $wlan | grep -q "inet addr:" ; then
    echo "Network is Okay"
else
    echo "Network connection down! Attempting reconnection."
    ifdown $wlan
    sleep 5
    ifup --force $wlan
    ifconfig $wlan | grep "inet addr"
fi

echo 
echo "Current Setting:"
ifconfig $wlan | grep "inet addr:"
echo

# Check is complete, Remove Lock file and exit
#echo "process is complete, removing lockfile"
rm $lockfile
exit 0

##################################################################
# End of Script
##################################################################
roger- commented 9 years ago

So you think putting that in if-post-down.d should be enough? Thanks, I'll try that.

mzapa commented 9 years ago

That's a nice script worthy of preservation. I've experienced intermittent wifi connectivity on occasion and noticed the wifi interface is still active and can ping itself but not the gateway as if the routing table got buggered, but it's something deeper and may be specifically related to my wifi interface. Here's some historical test results:

cubieboard wifi ping cubieboard wifi -> OK (and still has active inet IP address) cubieboard wifi ping network computer -> BAD cubiboard wifi ping network gateway -> BAD network computer ping network computer -> OK network computer ping cubieboard wifi -> BAD network computer ping gateway -> OK gateway ping gateway -> OK gateway ping network computer -> OK gateway ping cubieboard wifi -> BAD

If this is the same symptom roger is experiencing and he reproduces a similar test result when the cubieboard wifi falls offline, the script as written will not function to provide the desired effect and would require customization.

I too haven't tested the script however, in the context of this post, the line containing ' if ifconfig $wlan | grep -q "inet addr:" ; then ' will always be "true" resulting in the wifi interface not getting re-initialized.

roger- commented 9 years ago

I'll have to do some more investigation when it happens again, but I do know it gets disassociated from my router.

BTW I'm actually using an MK805II clone similar to this, which works fine with Cubian (except for a small FEX fix to partially get the OTG USB port working).

michalliu commented 9 years ago

Some USB wifi is not stable, it's a common problem maybe there is a bug in the hardware/driver.