from machine import Pin, I2C, ADC
from ssd1306 import SSD1306_I2C
import time
import network
import binascii
import random
# Setup OLED display
# define sda and scl pins for inter-path communication.
sda=machine.Pin(4)
scl=machine.Pin(5)
# determine the frequency values (need to learn more about this).
i2c=machine.I2C(0, sda=sda, scl=scl, freq=2000000)
# define and name the screen as oled.
oled=SSD1306_I2C(128, 64, i2c)
# Simulate a network connection.
print("Connecting to WiFi", end="")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Wokwi-GUEST", "")
while not wlan.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
print(wlan.ifconfig())
# Send test text to oled display.
oled.text("Connected",28,10)
oled.text("WiFi",50,30)
# show the screen.
oled.show() #show on OLED.
time.sleep(2)
# Scan for access points.
while True:
accessPoints = wlan.scan()
for ap in accessPoints:
print(f'\n{ap[0].decode()} {ap[3]}') # Filer for only ssid and strength.
# clear the display
oled.fill(0)
oled.show()
# Display ssid and strength on oled
oled.text("SSID:",1,1)
oled.text((ap[0].decode()),1,15)
oled.text("SPD:",1,30)
oled.text("{}".format(random.randint(-100, 0)),1,45)
# show the screen.
oled.show() #show on OLED.
time.sleep(2)
# simulate moving around with the scanner.
random.randint(-100, 0)
# Measure signal strength.
#wlan = network.WLAN() # network.WLAN(network.STA_IF)
#wlan.active(True)
#networks = wlan.scan() # list with tupples with 6 fields ssid, bssid, channel, RSSI, security, hidden
#i=0
#networks.sort(key=lambda x:x[3],reverse=True) # sorted on RSSI (3)
#for w in networks:
# i+=1
# print(i,w[0].decode(),binascii.hexlify(w[1]).decode(),w[2],w[3],w[4],w[5])