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

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

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