实现 WiFi 断线自动重连。原理是用 Python 监测网络是否断线,如果断线则重启网络服务。
配置无线网连接
pi@raspi3:~ $ sudo iw wlan0 scan
BSS d4:ee:07:34:88:5c(on wlan0)
TSF: 0 usec (0d, 00:00:00)
freq: 2427
beacon interval: 100 TUs
capability: ESS Privacy ShortPreamble ShortSlotTime (0x0431)
signal: -90.00 dBm
last seen: 0 ms ago
SSID: "WIFINAME"
Supported rates: 1.0* 2.0* 5.5* 11.0* 9.0 18.0 36.0 54.0
DS Parameter set: channel 4
Extended supported rates: 6.0 12.0 24.0 48.0
Country: CN Environment: Indoor/Outdoor
Channels [1 - 13] @ 20 dBm
TIM: DTIM Count 0 DTIM Period 1 Bitmap Control 0x0 Bitmap[0] 0x4
ERP: <no flags>
HT capabilities:
Capabilities: 0x1ec
HT20
SM Power Save disabled
RX HT20 SGI
RX HT40 SGI
TX STBC
RX STBC 1-stream
Max AMSDU length: 3839 bytes
No DSSS/CCK HT40
Maximum RX AMPDU length 65535 bytes (exponent: 0x003)
Minimum RX AMPDU time spacing: 4 usec (0x05)
HT RX MCS rate indexes supported: 0-15
HT TX MCS rate indexes are undefined
HT operation:
* primary channel: 4
* secondary channel offset: no secondary
* STA channel width: 20 MHz
* RIFS: 0
* HT protection: no
* non-GF present: 0
* OBSS non-GF present: 0
* dual beacon: 0
* dual CTS protection: 0
* STBC beacon: 0
* L-SIG TXOP Prot: 0
* PCO active: 0
* PCO phase: 0
WPA: * Version: 1
* Group cipher: CCMP
* Pairwise ciphers: CCMP
* Authentication suites: PSK
RSN: * Version: 1
* Group cipher: CCMP
* Pairwise ciphers: CCMP
* Authentication suites: PSK
* Capabilities: 1-PTKSA-RC 1-GTKSA-RC (0x0000)
WMM: * Parameter version 1
* BE: CW 15-1023, AIFSN 3
* BK: CW 15-1023, AIFSN 7
* VI: CW 7-15, AIFSN 2, TXOP 3008 usec
* VO: CW 3-7, AIFSN 2, TXOP 1504 usec
BSS Load:
* station count: 0
* channel utilisation: 17/255
* available admission capacity: 31250 [*32us]
# 编辑wifi文件
sudo vim /etc/wpa_supplicant/wpa_supplicant.conf
# 在该文件最后添加下面的话
network={
ssid="WIFINAME"
psk="password"
}
# 引号部分分别为wifi的名字和密码
# 保存文件后几秒钟应该就会自动连接到该wifi
# 查看是否连接成功
ifconfig wlan0
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 断线,则自动重新连接。
本文来自:树莓派实验室
断线重连2 目前在用
网友评论