これが原稿です。
この章ではmicropythonでWifiスキャナーを作ります。周りの2.4GHz帯のWifiリストを出しつつ、電波強度を音の高さで表すことができます。
network.scan()で取れる値は
ssid
,bssid
,channel
,RSSI
,auth_method
,hidden
の6要素がtupleで入ってます。
from m5stack import * | |
import network | |
import time | |
import _thread | |
class WifiList: | |
def __init__(self, fontHeight): | |
self.access_points = [] | |
self.cursor = 0 | |
self.fontHeight = fontHeight | |
def scan(self, wifi): | |
self.access_points = wifi.scan() | |
if self.cursor >= len(self.access_points): | |
self.cursor = len(self.access_points) | |
def move_cursor(self): | |
color = lcd.WHITE | |
if hasattr(self, 'target') and wifi_list.target is not None and self.target[0] == self.access_points[self.cursor][0]: | |
color = lcd.RED | |
lcd.text(0, self.cursor * self.fontHeight, self._access_point_format(self.access_points[self.cursor]), color) | |
self.cursor = self.cursor + 1 | |
if self.cursor >= len(self.access_points): | |
self.cursor = 0 | |
lcd.text(0, self.cursor * self.fontHeight, self._access_point_format(self.access_points[self.cursor]), lcd.YELLOW) | |
def subscribe(self): | |
self.target = self.access_points[self.cursor] | |
return self.target | |
def cancel(self): | |
self.target = None | |
def show_list(self): | |
lcd.setCursor(0, 0) | |
lcd.clear(0xEEEEEE) | |
for index, sta in enumerate(self.access_points): | |
color = lcd.WHITE | |
if hasattr(self, 'target') and wifi_list.target is not None and self.target[0] == sta[0]: | |
color = lcd.RED | |
elif self.cursor == index: | |
color = lcd.YELLOW | |
lcd.text(lcd.LASTX,lcd.LASTY, self._access_point_format(sta), color) | |
def _access_point_format(self, sta): | |
return "{0}: {1}:{2}-{3}\r\n".format(sta[0].decode('UTF-8'), sta[3],sta[4],sta[5]) | |
def on_wasAPressed(): | |
wifi_list.move_cursor() | |
def on_wasBPressed(): | |
wifi_list.subscribe() | |
def scan_wifi(): | |
while True: | |
wifi_list.scan(wifi) | |
wifi_list.show_list() | |
time.sleep_ms(30) | |
buttonA.wasPressed(on_wasAPressed) | |
buttonB.wasPressed(on_wasBPressed) | |
lcd.setCursor(0, 0) | |
lcd.setColor(lcd.BLACK) | |
lcd.clear(lcd.BLACK) | |
fw, fh = lcd.fontSize() | |
speaker.volume(1) | |
lcd.text(0,0,'initialize wifi..') | |
wifi = network.WLAN(network.STA_IF) | |
wifi.active(True) | |
wifi_list = WifiList(fh) | |
wifi_list.scan(wifi) | |
wifi_list.show_list() | |
def main(): | |
while True: | |
if hasattr(wifi_list, 'target') and wifi_list.target is not None: | |
target_access_point = wifi_list.target | |
if buttonC.isPressed(): | |
wifi_list.cancel() | |
rssi = list(filter(lambda s: s[0] == target_access_point[0], wifi_list.access_points))[0][3] | |
if rssi in range(0,-30,-1): | |
freq = 6600 | |
elif rssi in range(-30,-40,-1): | |
freq = 5500 | |
elif rssi in range(-40,-50,-1): | |
freq = 4400 | |
elif rssi in range(-50,-67,-1): | |
freq = 3300 | |
elif rssi in range(-68,-70,-1): | |
freq = 2200 | |
elif rssi in range (-71,-80,-1): | |
freq = 1100 | |
elif rssi in range (-81,-90,-1): | |
freq = 800 | |
else: | |
freq = 100 | |
# speaker.tone(freq,20) | |
time.sleep_ms(100) | |
main_id = _thread.start_new_thread('main', main,()) | |
_thread.start_new_thread('scan_wifi', scan_wifi, ()) |