Skip to content

Instantly share code, notes, and snippets.

Embed
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/wifi-reconnect.sh to create shell script file, with following content:

#!/bin/bash 
 
SSID=$(/sbin/iwgetid --raw) 

if [ -z "$SSID" ] 
then 
    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 
fi 

echo "WiFi check finished"

Or you can also use sudo wget https://raw.github.com/carry0987/Raspberry-Pi-Repo/master/Auto-WiFi-Reconnect/wifi-reconnect.sh to download the script

Make new file executable

sudo chmod +x /home/pi/wifi-reconnect.sh

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/wifi-reconnect.sh

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

@junkycoder
Copy link

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 👍

@carry0987
Copy link
Author

carry0987 commented Apr 11, 2021

@junkycoder Great 👍

@Ekibunnel
Copy link

Ekibunnel commented Sep 13, 2021

didn't work for me.
sudo ifconfig wlan0 down was working but sudo ifconfig wlan0 up wasn't, no matter what i try.

So I had to set it up the old way by editing:

sudo nano /etc/network/interfaces

to add it :

auto wlan0
iface wlan0 inet dhcp
    wpa-ssid CHANGE_THIS_TO_YOUR_SSID
    wpa-psk CHANGE_THIS_TO_YOUR_PASSWORD

and then just change sudo ifconfig wlan0 down to sudo ifdown wlan0 and sudo ifconfig wlan0 up to sudo ifup wlan0
like this :

then 
    echo "`date -Is` WiFi interface is down, trying to reconnect" >> /home/pi/wifi-log.txt
    sudo ifdown wlan0
    sleep 30
    sudo ifup wlan0
fi 

@carry0987
Copy link
Author

carry0987 commented Sep 13, 2021

@Ekibunnel What version of Raspberry Pi OS did you use ?

@Ekibunnel
Copy link

Ekibunnel commented Sep 13, 2021

@carry0987

pi@raspberrypi3:~ $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

pi@raspberrypi3:~ $ cat /etc/debian_version
10.10
pi@raspberrypi3:~ $ uname -a
Linux raspberrypi3 5.10.59-v7+ #1447 SMP Thu Aug 19 12:24:58 BST 2021 armv7l GNU/Linux

@carry0987
Copy link
Author

carry0987 commented Sep 14, 2021

@Ekibunnel That's really weird, in my case it work just right, I'll test it on other raspberry pi next month.

@Ekibunnel
Copy link

Ekibunnel commented Sep 14, 2021

@carry0987 could totally be my fault, it make quite some time since I did a proper fresh OS install on that pi.
I just wanted to share how I did solve it, in case someone one day as the same issue.

@carry0987
Copy link
Author

carry0987 commented Sep 14, 2021

@Ekibunnel
I know, I just want to reproduce this kind of bug in other model of raspberry pi, which can point out the problem is a single case or not.
Since the code is really old (three years ago), so I must figure it out.

@JazzBrown1
Copy link

JazzBrown1 commented Sep 19, 2021

Hey works perfect. Thanks for sharing!

@carry0987
Copy link
Author

carry0987 commented Sep 20, 2021

@jebeaudet
Copy link

jebeaudet commented Apr 21, 2022

Worked like a charm on OG pi Model B Revision 1.0 with Raspbian 9 Stretch and a usb wifi adapter, thanks!

@carry0987
Copy link
Author

carry0987 commented Apr 22, 2022

@jebeaudet Glad it helps, thanks for your reply !

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