Python implementation of raspberry PI WiFi automatically reconnect the instance code

  • 2020-05-26 09:36:14
  • OfStack

Realize WiFi wire breaking and automatic reconnection. The principle is to use Python to monitor whether the network is disconnected, and if so, restart the network service.

1.Python code autowifi.py, under /home/pi:


#!/usr/bin/python
import
os, time
 while
True:
  if
'192' not in os.popen('ifconfig | grep 192').read():
    print
'\n****** wifi is down, restart... ******\n'
    os.system('sudo /etc/init.d/networking restart')
  time.sleep(5*60) #5 minutes

2.Shell script autowifi. sh, also under /home/pi:


#!/bin/sh
python /home/pi/autowifi.py &

3. Startup the above script automatically: execute the following command in the terminal window


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 default

Related articles: