Home > Backend Development > Python Tutorial > Using Python to implement an example of automatic reconnection after Raspberry Pi WiFi disconnection (with code)

Using Python to implement an example of automatic reconnection after Raspberry Pi WiFi disconnection (with code)

高洛峰
Release: 2017-03-17 09:24:52
Original
2170 people have browsed it

To achieve automatic reconnection when WiFi is disconnected, the principle is to use Python to monitor whether the network is disconnected, and restart the network service if it is disconnected. Next, I will share the implementation code with you. Friends who need it can refer to it

1.Python code autowifi.py, placed in the /home/pi directory:

#!/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
Copy after login

2.Shell script autowifi.sh , also placed in the /home/pi directory:

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

3. Automatically start the above script when booting: 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
Copy after login

The above is introduced by the editor Python implements automatic reconnection of Raspberry Pi WiFi disconnection. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for your support of the Script House website!

The above is the detailed content of Using Python to implement an example of automatic reconnection after Raspberry Pi WiFi disconnection (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template