freezkyd
Posts: 8
Joined: Wed Aug 19, 2015 5:18 am

Trying to make Raspberry Pi's WiFi repairing itself

Wed Mar 22, 2017 9:08 am

For some unknown reason, my rPi loses wireless connection. It is located 10 meters from my WiFi AP but it has a very small Wifi module installed on it and this might actually be causing the issue. I decided to build a failover script in order to restart the wlan0 interface in case it's down.

I've been experimenting for some weeks with this unit and I am planning installing it in a farm 40+ miles from where I live, so I got to be sure that it's always up and running. So this is a challenge and a game for me, there should be no tolerance for wireless being down. The Wifi AP part is another case and I consider it 100% working (in theory). Or it might restart/fail/lose connection etc. and the rPi should be able to bring itself up again.

I've also used https://linux.die.net/man/8/ifenslave but decided to try a bash script better. Another idea was to use something like Monit for monitoring and let it also handle network's health. Let me know your opinions please :)

This is my network interfaces file:

Code: Select all

 auto lo
    iface lo inet loopback

    auto eth0
    allow-hotplug eth0
    iface eth0 inet static
        address 192.168.1.91
        netmask 255.255.255.0
        network 192.168.1.0
        broadcast 192.168.1.255

    auto wlan0
    allow-hotplug wlan0
    iface wlan0 inet static
        address 192.168.1.92
        netmask 255.255.255.0
        network 192.168.1.0
        broadcast 192.168.1.255
        wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
        wireless-power off
wpa_supplicant.conf

Code: Select all

#ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    #update_config=1

    network={
    ssid="mySSID"
    psk="myPassword!"
    proto=RSN
    key_mgmt=WPA-PSK
    pairwise=CCMP
    auth_alg=OPEN
    }
crontab

Code: Select all

 */5 * * * * /usr/bin/sudo -H /usr/local/bin/checkwifi.sh >> /dev/null 2>&1
checkwifi.sh

Code: Select all

ping -c4 192.168.1.254 > /dev/null
    if [ $? != 0 ]
    then
        echo "No network connection, restarting wlan0"
        /sbin/ifdown 'wlan0'
        sleep 5
        /sbin/ifup --force 'wlan0'
    else
        echo "Connection seems fine"
    fi

epoch1970
Posts: 5135
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Trying to make Raspberry Pi's WiFi repairing itself

Wed Mar 22, 2017 8:41 pm

Or you can use watchdog and check the network.
The watchdog on the Pi works at a higher frequency than others: 15 secs between checks at maximum. But you can test the network every X checks if you want. I have posted some time ago a recipe for a slow pinger running via watchdog.

The disadvantage of watchdog is that it reboots. So there is some chance you will shoot yourself in the foot during initial setup (errors in scripts etc.)
The advantage is that it reboots, always. So you're practically guaranteed the system will return to a sane state whatever has happened.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

Return to “Networking and servers”