0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ラスパイの無線アクセスポイント の接続が切れたとき pythonを使って自動で再接続する

Last updated at Posted at 2023-05-13

原因

raspberri pi を wifi にしたのはいいもののしばらく時間がたてば wifi の接続がきれてしまう。
いろいろ調べた結果、hostapd.service が止まっていたのでhostapd.service を再起動すればいいらしい。
コマンドは

sudo systemctl restart hostapd.service

これでつながった。

対策

ただ結構な頻度で接続が切れるのでpythonを使って自動で再接続することを試みる。
ロジックはこうなる。

1. syslog を読み込む
2. syslog を見て接続が切れていた場合再接続する
3. 60秒待機後、1.2 を繰り返す

syslog の確認

sudo gedit /var/log/syslog.1

接続が切れている場合

~ $ sudo cat /var/log/syslog.1 | grep 'hostapd' | grep 'disassociated'
May 12 08:39:08 raspberrypi hostapd: wlan0: *************: disassociated
May 12 08:39:08 raspberrypi hostapd: wlan0: *************: disassociated
May 12 18:25:32 raspberrypi hostapd: wlan0: *************: disassociated
May 12 18:25:32 raspberrypi hostapd: wlan0: *************: disassociated
May 12 18:26:07 raspberrypi hostapd: wlan0: *************: disassociated
May 12 18:26:07 raspberrypi hostapd: wlan0: *************: disassociated

切れてない場合 

~ $ sudo cat /var/log/syslog.1 | grep 'hostapd' | grep 'Succeeded'
May 12 19:36:13 raspberrypi systemd[1]: hostapd.service: Succeeded.
May 12 21:22:28 raspberrypi systemd[1]: hostapd.service: Succeeded.
May 12 22:43:42 raspberrypi systemd[1]: hostapd.service: Succeeded.
May 12 23:56:00 raspberrypi systemd[1]: hostapd.service: Succeeded. 

どうやら 「Succeeded」「disassociated」「hostapd」が重要っぽい。
これらを抽出してログの最終行の時刻どうしを比較し、「disassociated」のほうが最新だったら
接続がきれているってわかる

python を使ってプログラミング

RestartHostapd.py
import os
import time
import datetime

def writeLog(log):
    os.system('echo \'' + log + '\' | sudo tee -a ./log/RestartHostapdLog\n')

    
while True:
    
    dt_now = datetime.datetime.now()
    writeLog('['+str(dt_now)+'] Confirming the wifi conection --->')
    
    rowNum = sum([1 for _ in open('./log/RestartHostapdLog')])
    syslog = open('/var/log/syslog','r')    
    datalist = syslog.readlines()
    sucsess = str(datalist[0])[4:15].replace(':','').replace(' ','')
    fail = str(datalist[0])[4:15].replace(':','').replace(' ','')

    if rowNum > 1000:
        os.system('cp ./log/RestartHostapdLog ./log/RestartHostapdLog_'+str(dt_now.strftime('%Y.%m.%dT%H:%M:%S')))
        os.system('rm ./log/RestartHostapdLog')
    
    for data in datalist:
        if 'hostapd' in data and 'Succeeded' in data :
            sucsess = str(data)[4:15].replace(':','').replace(' ','')
        if 'hostapd' in data  and 'disassociated' in data:
            fail = str(data)[4:15].replace(':','').replace(' ','')
    syslog.close()
    
    if int(sucsess) < int(fail) :
        os.system('./restartHostapd.sh')
        writeLog('['+str(dt_now)+'] The hostapd is restarted by this program.')
    else :
        writeLog('['+str(dt_now)+'] Conection is right.')
    
    time.sleep(60)
  

python と同じ階層に以下のシェルを追加

restartHostapd.sh
sudo systemctl restart hostapd.service

コマンドで「RestartHostapd.py」を実行

 sudo python RestartHostapd.py 

動いていることを確認できた

[2023-05-13 16:18:50.778952] Conection is right.
[2023-05-13 16:19:50.858037] Confirming the wifi conection --->
[2023-05-13 16:19:50.858037] Conection is right.
[2023-05-13 16:20:51.104188] Confirming the wifi conection --->
[2023-05-13 16:20:51.104188] Conection is right.
[2023-05-13 16:21:51.796103] Confirming the wifi conection --->
[2023-05-13 16:21:51.796103] Conection is right.
0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?