[FIX] Wifi Disconnect

Guides and tutorials for various stuff. Posted by DietPi users.
Post Reply
vinanrra
Posts: 13
Joined: Mon Oct 24, 2016 4:57 pm

[FIX] Wifi Disconnect

Post by vinanrra » Tue Sep 24, 2019 2:46 pm

  • Put the script into the home dir, like: /home/pi
  • Edit the parameter in the script

    vim NetReconnector.sh
    # NetCard and PingTarget should be modified.
  • Add x priority to the script

    chmod +x NetReconnector.sh
  • Add a task with crontab
    crontab -e

    Add this line which means run the script every 5 minutes between 8:00 and 22:00.

    */5 8-22 * * * ~/NetReconnector.sh
Log file generated in home dir, like: /home/pi/NetReconnector.log

Code: Select all

#!/bin/bash
# by nickwang [email protected]

NetCard=wlan0	 		# the netcard connected to the internet
PingTarget=www.baidu.com	# the ping target, router ip or website, etc: 192.168.1.1, www.baidu.com
LogFile=~/NetReconnector.log	# log file
LogAll=false			# log msg no matter whether the net is connected, used to be check whether the script is running.

test -e $LogFile || touch $LogFile

time=$(date "+%Y/%m/%d %H:%M:%S")

ret=$(ping -c 2 -W 1 -I $NetCard $PingTarget | grep 'received' | cut -d ',' -f 2 | cut -d ' ' -f 2)

if [ "$ret" == "0" ]; then
    sudo ifdown wlan0 && sudo ifup wlan0
    echo "Try Reconnect: $?, $time" | tee -a $LogFile
else
    if [ "$LogAll" == "true" ]; then
        echo "Network is ok, ret = $ret, $time" | tee -a $LogFile
    fi
fi

exit 0
Source: Github


PD: Could be good if you add this script to DietPi because Wifi sometimes drop connection and with this seems to be working 24/7.
I only tested this 2 days and I had no drops, I will write later if my connection drops.

User avatar
MichaIng
Site Admin
Posts: 1728
Joined: Sat Nov 18, 2017 5:21 pm

Re: [FIX] Wifi Disconnect

Post by MichaIng » Fri Sep 27, 2019 10:34 am

@vinanrra
Many thanks for sharing.

Did you try/compare with the WiFi monitor that you can enable within dietpi-config WiFi adapter settings?

vinanrra
Posts: 13
Joined: Mon Oct 24, 2016 4:57 pm

Re: [FIX] Wifi Disconnect

Post by vinanrra » Fri Sep 27, 2019 9:07 pm

Where it is?

It is the option to enable reconnect?

User avatar
MichaIng
Site Admin
Posts: 1728
Joined: Sat Nov 18, 2017 5:21 pm

Re: [FIX] Wifi Disconnect

Post by MichaIng » Fri Oct 11, 2019 5:06 pm

@vinanrra
Jep the "Auto Reconnect" option.

Post Reply