Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Raspberry Pi 3B+ Auto reconnect to wifi when lost connect

Auto reconnect to wifi when lost connect

Create script file

Use touch /home/pi/ to create shell script file, with following content:

SSID=$(/sbin/iwgetid --raw) 

if [ -z "$SSID" ] 
    echo "`date -Is` WiFi interface is down, trying to reconnect" >> /home/pi/wifi-log.txt
    sudo ifconfig wlan0 down
    sleep 30
    sudo ifconfig wlan0 up 

echo "WiFi check finished"

Or you can also use sudo wget to download the script

Make new file executable

sudo chmod +x /home/pi/

Install cron

sudo apt-get install cron

Edit crontab

Use sudo vim /etc/crontab to edit crontab

By putting following content at end of file:

* * * * * root /home/pi/

Test it by disconnecting from WiFi:

sudo ifconfig wlan0 down

Script should reestablish connection within 1 minute.

Check log file

After the RPi reestablish connection, reconnect RPi and check log file:
cat /home/pi/wifi-log.txt


This comment has been minimized.

Copy link

@junkycoder junkycoder commented Apr 11, 2021

Looks working when I need to have RasPi connected to my hotspot on iPhone. Tried to disable / enable hotspot many times, still connection is renewed every time in few seconds 👍


This comment has been minimized.

Copy link
Owner Author

@carry0987 carry0987 commented Apr 11, 2021

@junkycoder Great 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment