美文网首页树莓派
树莓派Wi-Fi断线重连

树莓派Wi-Fi断线重连

作者: 飞鱼ll | 来源:发表于2018-03-09 20:49 被阅读7次

实现 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 目前在用

相关文章

  • 树莓派Wi-Fi断线重连

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

  • 状态同步的断线重连

    最近陆陆续续在给游戏做断线重连相关的工作,大厅服的断线重连问题不是很大,难点主要在于战斗中的断线重连。断线重连要解...

  • 断线重连问题综述

    断线重连主要涉及到几个问题: 判定断线 怎么重连 重连后如何还原游戏环境 判定断线有两种方式,(1)是超时判定断线...

  • 断线重连

    今天看了下之前游戏登录相关逻辑,重新整理下断线重连的一些做法。 首先,介绍下断线的几种常见情况:情况一:客户端网络...

  • 断线重连

    using System; using System.Collections; using System.Coll...

  • Webscoket 断线重连

  • 定时断线重连

    客户端断线重连机制。客户端数量多,且需要传递的数据量级较大。可以周期性的发送数据的时候,使用。要求对数据的即时性不...

  • 蓝牙断线重连

    http://www.cocoachina.com/bbs/read.php?tid-1722597.html

  • .NET CORE SignalR Flutter 客户端

    依赖 例子 断线重连 automatic-reconnects-signalr

  • 让树莓派自动连接到一个预设好的 Wi-Fi 热点

    如何让树莓派自动连接到一个预设好的 Wi-Fi 热点? 如过没乱动过 /etc/network/interface...

网友评论

    本文标题:树莓派Wi-Fi断线重连

    本文链接:https://www.haomeiwen.com/subject/uupmfftx.html