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.

Revisions

  1. @carry0987 carry0987 revised this gist Feb 27, 2024. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions RPi3-Auto-WiFi.md
    Original file line number Diff line number Diff line change
    @@ -28,11 +28,10 @@ fi
    echo 'WiFi check finished'
    ```

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

    #### Note:
    This script uses `/sbin/ip` command for disabling and enabling the wlan interface. This is a more modern approach compared to using `ifconfig` and is the preferred way in newer Linux distributions.
  2. @carry0987 carry0987 revised this gist Feb 27, 2024. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions RPi3-Auto-WiFi.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ Before you start, make sure `ip` command is available on your system. In modern
    ## Create script file
    Use ```touch /home/pi/wifi-reconnect.sh``` to create a shell script file,
    with the following content:
    ```
    ```bash
    #!/bin/bash

    SSID=$(/sbin/iwgetid --raw)
    @@ -29,7 +29,9 @@ 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```
    ```bash
    sudo wget https://raw.github.com/carry0987/Raspberry-Pi-Repo/master/Auto-WiFi-Reconnect/wifi-reconnect.sh
    ```
    to download the script.

    #### Note:
  3. @carry0987 carry0987 revised this gist Feb 27, 2024. 1 changed file with 31 additions and 19 deletions.
    50 changes: 31 additions & 19 deletions RPi3-Auto-WiFi.md
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,39 @@
    # Auto reconnect to wifi when lost connect
    # Auto reconnect to wifi when lost connection

    Before you start, make sure `ip` command is available on your system. In modern Linux distributions, `ip` replaces older `ifconfig` command. If `net-tools` package (that includes `ifconfig`) is not installed and you prefer using it, you can install it via `sudo apt-get install net-tools`.

    ## Create script file
    Use ```touch /home/pi/wifi-reconnect.sh``` to create shell script file,
    with following content:
    Use ```touch /home/pi/wifi-reconnect.sh``` to create a shell script file,
    with the following content:
    ```
    #!/bin/bash
    SSID=$(/sbin/iwgetid --raw)
    #!/bin/bash
    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
    SSID=$(/sbin/iwgetid --raw)
    echo "WiFi check finished"
    if [ -z "$SSID" ]; then
    echo "`date -Is` WiFi interface is down, trying to reconnect" >> /home/pi/wifi-log.txt
    if command -v /sbin/ip &> /dev/null; then
    /sbin/ip link set wlan0 down
    sleep 10
    /sbin/ip link set wlan0 up
    elif command -v sudo ifconfig &> /dev/null; then
    sudo ifconfig wlan0 down
    sleep 10
    sudo ifconfig wlan0 up
    else
    echo "`date -Is` Failed to reconnect: neither /sbin/ip nor ifconfig commands are available" >> /home/pi/wifi-log.txt
    fi
    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
    to download the script.

    #### Note:
    This script uses `/sbin/ip` command for disabling and enabling the wlan interface. This is a more modern approach compared to using `ifconfig` and is the preferred way in newer Linux distributions.

    ## Make new file executable
    ```sudo chmod +x /home/pi/wifi-reconnect.sh```
    @@ -32,17 +44,17 @@ to download the script
    ## Edit crontab
    Use ```sudo vim /etc/crontab``` to edit **crontab**

    By putting following content at end of file:
    By putting the following content at the end of the file:

    ```* * * * * root /home/pi/wifi-reconnect.sh```

    Test it by disconnecting from WiFi:

    ```sudo ifconfig wlan0 down```
    ```/sbin/ip link set wlan0 down```

    Script should reestablish connection within 1 minute.
    The script should reestablish the connection within 1 minute.

    ## Check log file
    After the RPi reestablish connection, reconnect RPi and check log file:<br>
    After the RPi reestablishes the connection, reconnect to the RPi and check the log file:<br>
    ```cat /home/pi/wifi-log.txt```
    ![](https://i.imgur.com/hGcfdLm.jpg)
  4. @carry0987 carry0987 revised this gist Jul 30, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RPi3-Auto-WiFi.md
    Original file line number Diff line number Diff line change
    @@ -45,4 +45,4 @@ Script should reestablish connection within 1 minute.
    ## Check log file
    After the RPi reestablish connection, reconnect RPi and check log file:<br>
    ```cat /home/pi/wifi-log.txt```
    ![](https://i.imgur.com/uCzUsfG.png)
    ![](https://i.imgur.com/hGcfdLm.jpg)
  5. @carry0987 carry0987 revised this gist Jul 24, 2018. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions RPi3-Auto-WiFi.md
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,10 @@ 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```

  6. @carry0987 carry0987 revised this gist Jul 22, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RPi3-Auto-WiFi.md
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ echo "WiFi check finished"
    ```

    ## Make new file executable
    ```chmod +x /home/pi/wifi-reconnect.sh```
    ```sudo chmod +x /home/pi/wifi-reconnect.sh```

    ## Install cron
    ```sudo apt-get install cron```
  7. @carry0987 carry0987 renamed this gist Jul 22, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. @carry0987 carry0987 revised this gist Jul 21, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion RPi3-auto-wifi.md
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,9 @@ 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
    sudo ifconfig wlan0 down
    sleep 30
    sudo ifconfig wlan0 up
    fi
    echo "WiFi check finished"
  9. @carry0987 carry0987 revised this gist Jul 21, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions RPi3-auto-wifi.md
    Original file line number Diff line number Diff line change
    @@ -37,6 +37,6 @@ Test it by disconnecting from WiFi:
    Script should reestablish connection within 1 minute.

    ## Check log file
    After the RPi reestablish connection, reconnect RPi and check log file:
    /r```cat /home/pi/wifi-log.txt```
    After the RPi reestablish connection, reconnect RPi and check log file:<br>
    ```cat /home/pi/wifi-log.txt```
    ![](https://i.imgur.com/uCzUsfG.png)
  10. @carry0987 carry0987 revised this gist Jul 21, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RPi3-auto-wifi.md
    Original file line number Diff line number Diff line change
    @@ -38,5 +38,5 @@ 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```
    /r```cat /home/pi/wifi-log.txt```
    ![](https://i.imgur.com/uCzUsfG.png)
  11. @carry0987 carry0987 revised this gist Jul 21, 2018. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions RPi3-auto-wifi.md
    Original file line number Diff line number Diff line change
    @@ -38,6 +38,5 @@ 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```
    ```cat /home/pi/wifi-log.txt```
    ![](https://i.imgur.com/uCzUsfG.png)
  12. @carry0987 carry0987 revised this gist Jul 21, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions RPi3-auto-wifi.md
    Original file line number Diff line number Diff line change
    @@ -38,5 +38,6 @@ 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```
    ![](https://i.imgur.com/uCzUsfG.png)
  13. @carry0987 carry0987 revised this gist Jul 21, 2018. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions RPi3-auto-wifi.md
    Original file line number Diff line number Diff line change
    @@ -24,11 +24,16 @@ echo "WiFi check finished"
    ```sudo apt-get install cron```

    ## Edit crontab
    ```sudo vim /etc/crontab```
    by putting following content at end of file:
    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
  14. @carry0987 carry0987 revised this gist Jul 21, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RPi3-auto-wifi.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Auto reconnect to wifi when lost connect

    ## Create script file
    Use```touch /home/pi/wifi-reconnect.sh```to create shell script file,
    Use ```touch /home/pi/wifi-reconnect.sh``` to create shell script file,
    with following content:
    ```
    #!/bin/bash
  15. @carry0987 carry0987 revised this gist Jul 21, 2018. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions RPi3-auto-wifi.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,7 @@
    # Auto reconnect to wifi when lost connect

    ## Create script file
    Shell script:
    ```touch /home/pi/wifi-reconnect.sh```
    Use```touch /home/pi/wifi-reconnect.sh```to create shell script file,
    with following content:
    ```
    #!/bin/bash
  16. @carry0987 carry0987 revised this gist Jul 21, 2018. 1 changed file with 28 additions and 23 deletions.
    51 changes: 28 additions & 23 deletions RPi3-auto-wifi.md
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,38 @@
    # Auto reconnect to wifi when lost connect

    ## 1. Python Script
    Named **autowifi.py** and then put it to **/home/pi** folder
    ## Create script file
    Shell script:
    ```touch /home/pi/wifi-reconnect.sh```
    with following content:
    ```
    #!/usr/bin/python
    import os, time
    #!/bin/bash
    SSID=$(/sbin/iwgetid --raw)
    while True:
    if '192' not in os.popen('sudo wpa_cli status').read():
    print '\n****** wifi is down, restart... ******\n'
    os.system('sudo ifup wlan0')
    time.sleep(300) #5 minutes
    ```
    if [ -z "$SSID" ]
    then
    echo "`date -Is` WiFi interface is down, trying to reconnect" >> /home/pi/wifi-log.txt
    systemctl restart wireless
    fi
    ## 2. Shell Script
    Named **autowifi.sh** and then put it to **/home/pi** folder
    ```
    #!/bin/sh
    python /home/pi/autowifi.py &
    echo "WiFi check finished"
    ```

    ## 3. Set Autorun
    ```sudo cp -f /home/pi/autowifi.sh /etc/init.d/```

    ```sudo chmod +x /etc/init.d/autowifi.sh```
    ## Make new file executable
    ```chmod +x /home/pi/wifi-reconnect.sh```

    ```sudo chown root:root /etc/init.d/autowifi.sh```
    ## Install cron
    ```sudo apt-get install cron```

    ```sudo update-rc.d autowifi.sh defaults```
    ## Edit crontab
    ```sudo vim /etc/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.

    ## 4. Demo Screenshot
    ![](https://i.imgur.com/cykJowB.png)
    ## Check log file
    After the RPi reestablish connection, reconnect RPi and check log file:
    ```cat /home/pi/wifi-log.txt```
    ![](https://i.imgur.com/uCzUsfG.png)
  17. @carry0987 carry0987 revised this gist Jul 18, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RPi3-auto-wifi.md
    Original file line number Diff line number Diff line change
    @@ -30,4 +30,4 @@ python /home/pi/autowifi.py &
    ```sudo update-rc.d autowifi.sh defaults```

    ## 4. Demo Screenshot
    ![](https://i.imgur.com/NoVaQcY.png)
    ![](https://i.imgur.com/cykJowB.png)
  18. @carry0987 carry0987 revised this gist Jul 18, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions RPi3-auto-wifi.md
    Original file line number Diff line number Diff line change
    @@ -22,8 +22,11 @@ python /home/pi/autowifi.py &

    ## 3. Set Autorun
    ```sudo cp -f /home/pi/autowifi.sh /etc/init.d/```

    ```sudo chmod +x /etc/init.d/autowifi.sh```

    ```sudo chown root:root /etc/init.d/autowifi.sh```

    ```sudo update-rc.d autowifi.sh defaults```

    ## 4. Demo Screenshot
  19. @carry0987 carry0987 revised this gist Jul 18, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RPi3-auto-wifi.md
    Original file line number Diff line number Diff line change
    @@ -27,4 +27,4 @@ python /home/pi/autowifi.py &
    ```sudo update-rc.d autowifi.sh defaults```

    ## 4. Demo Screenshot
    [](https://i.imgur.com/NoVaQcY.png)
    ![](https://i.imgur.com/NoVaQcY.png)
  20. @carry0987 carry0987 revised this gist Jul 18, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RPi3-auto-wifi.md
    Original file line number Diff line number Diff line change
    @@ -27,4 +27,4 @@ python /home/pi/autowifi.py &
    ```sudo update-rc.d autowifi.sh defaults```

    ## 4. Demo Screenshot
    [Imgur](https://i.imgur.com/NoVaQcY.png)
    [](https://i.imgur.com/NoVaQcY.png)
  21. @carry0987 carry0987 created this gist Jul 18, 2018.
    30 changes: 30 additions & 0 deletions RPi3-auto-wifi.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    # Auto reconnect to wifi when lost connect

    ## 1. Python Script
    Named **autowifi.py** and then put it to **/home/pi** folder
    ```
    #!/usr/bin/python
    import os, time
    while True:
    if '192' not in os.popen('sudo wpa_cli status').read():
    print '\n****** wifi is down, restart... ******\n'
    os.system('sudo ifup wlan0')
    time.sleep(300) #5 minutes
    ```

    ## 2. Shell Script
    Named **autowifi.sh** and then put it to **/home/pi** folder
    ```
    #!/bin/sh
    python /home/pi/autowifi.py &
    ```

    ## 3. Set Autorun
    ```sudo cp -f /home/pi/autowifi.sh /etc/init.d/```
    ```sudo chmod +x /etc/init.d/autowifi.sh```
    ```sudo chown root:root /etc/init.d/autowifi.sh```
    ```sudo update-rc.d autowifi.sh defaults```

    ## 4. Demo Screenshot
    [Imgur](https://i.imgur.com/NoVaQcY.png)