To implement 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
2.Shell script autowifi.sh , also placed in the /home/pi directory:
#!/bin/sh python /home/pi/autowifi.py &
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
[Related recommendations]
3. Python Meets Data Collection Video Tutorial
The above is the detailed content of Detailed explanation of how to implement automatic reconnection after WiFi disconnection in Python. For more information, please follow other related articles on the PHP Chinese website!