Skip to content

Instantly share code, notes, and snippets.

@GitHeld
Forked from carry0987/RPi3-Auto-WiFi.md
Created July 7, 2024 19:40
Show Gist options
  • Select an option

  • Save GitHeld/1b00a8253490662882074779c6ff10d3 to your computer and use it in GitHub Desktop.

Select an option

Save GitHeld/1b00a8253490662882074779c6ff10d3 to your computer and use it in GitHub Desktop.
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
    systemctl restart wireless 
fi 

echo "WiFi check finished"

Make new file executable

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: /rcat /home/pi/wifi-log.txt

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