haochuang

实现 WiFi 断线自动重连。原理是用 Python 监测网络是否断线,如果断线则重启网络服务。

1.Python 代码 autowifi.py,放在 /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脚本autowifi.sh,也放在 /home/pi 目录下:

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

 

3.开机自动启动以上脚本:在终端窗口执行以下命令即可

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

 

每5分钟检测一次,若 WiFi 断线,则自动重新连接。

如果觉得5mins时间太短或者太长,可以自行修改参数即可。

本文来自:树莓派实验室
 
 

分类:

技术点:

相关文章:

  • 2022-01-08
  • 2021-12-03
  • 2021-11-04
  • 2021-09-02
  • 2021-08-29
  • 2022-01-06
猜你喜欢
  • 2021-11-04
  • 2021-09-28
  • 2022-12-23
  • 2021-09-02
  • 2021-08-29
相关资源
相似解决方案