我尝试使用此iwlist扫描解析脚本打开GPIO(带有连接的LED),表示无线连接:
#!/usr/bin/env python
import sys
import subprocess
import RPi.GPIO as GPIO
import time
interface = "wlan0"
ledPin = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(ledPin, GPIO.OUT)
def get_quality(cell):
quality = matching_line(cell,"Quality=").split()[0].split('/')
# This now returns a number
return int(round(float(quality[0]) / float(quality[1]) * 100))
def matching_line(lines, keyword):
"""Returns the first matching line in a list of lines. See match()"""
for line in lines:
matching=match(line,keyword)
if matching!=None:
return matching
return None
def match(line,keyword):
"""If the first part of line (modulo blanks) matches keyword,
returns the end of that line. Otherwise returns None"""
line=line.lstrip()
length=len(keyword)
if line[:length] == keyword:
return line[length:]
else:
return None
def main():
cells=[[]]
parsed_cells=[]
proc = subprocess.Popen(["iwlist", interface, "scan"],stdout=subprocess.PIPE, universal_newlines=True)
out, err = proc.communicate()
for line in out.split("\n"):
cell_line = match(line,"Cell ")
if cell_line != None:
cells.append([])
line = cell_line[-27:]
cells[-1].append(line.rstrip())
cells=cells[1:]
for cell in cells:
# get_quality now returns an integer
qual = get_quality(cell)
if qual > 65:
GPIO.output(ledPin, GPIO.HIGH)
else:
GPIO.output(ledPin, GPIO.LOW)
main()
我知道它遗漏了一些东西,比如干净的逃生和GPIO.cleanup(),但我遇到的主要问题,即使是经过数小时和谷歌数小时的试验,也是我能得到的LED点亮一次,但我想在后台运行这个脚本,每20-30秒检查一次连接。我是以完全错误的方式来做这件事的吗?有没有更简单的方法来检索wifi信号质量并返回可用于操作GPIO的INT?
答案 0 :(得分:0)
看起来你没有打电话给main()。
if __name__ == '__main__':
main()