Fixing WIFI dropout on Raspberry Pi

I have noticed that my wifi gets degraded, and becomes useless, over time on the Raspberry Pi. I use a tiny wifi dongle without any extra power connected to it. It may run for weeks without problems, but suddenly I'm not able to connect to it, nor is the Raspberry able to connect to the outside world. When this happens, I have no choice but to unplug the power and reboot it.

I have written a few posts about this problem earlier, but this is now how I manage the problem. You may want to read part 1 and part 2 of my old solution.

This week the wifi was terrible and I had to unplug it several times every day. I needed a way to make the Raspberry check the signal level automatically and do a reboot if it was too low. My solution is to check the wifi signal every 5 minutes using this command:


$ iwconfig wlan0

This will typically print out something like this:

wlan0     IEEE 802.11bgn  ESSID:"MySecretNetwork"  Nickname:"<WIFI@REALTEK>"
          Mode:Managed  Frequency:2.412 GHz  Access Point: 00:22:07:38:A7:DC
          Bit Rate:72.2 Mb/s   Sensitivity:0/0
          Retry:off   RTS thr:off   Fragment thr:off
          Power Management:off
          Link Quality=35/100  Signal level=29/100  Noise level=0/100
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0

As you can see, one of the lines contains information about the quality and the level of the signal. Looks like this finally solved my problem. 

Make sure you are in /home/pi and create the script with:

$ nano wificheck

Type/copy in the following:


#Get the signal level
iwconfig wlan0 | grep -i  quality | cut -d '=' -f 3 | cut -d '/' -f 1


if [ $RESULT > 10 ]; then
  # The signal is above 10 - Do nothing
  echo success
  # The signal is too low - Reboot
  sudo /sbin/shutdown -r now
  echo failed

Then make the script executable with:

$ chmod +x wificheck

Finally we need make this run every 5 minutes:

$ sudo crontab -e

Add the following line at the bottom of the file:

*/5 * * * * /usr/bin/sudo -H /home/pi/checkwifi >> /dev/null 2>&1

Now, restart the cron daemon:

$ sudo service cron restart