Run script on NodeMCU to scan for WiFi

I've looked a bit further into using micro python on NodeMCU. I've got a tiny monochromatic SSD1306 OLED display connected to one of my ESP8266 based NodeMCU boards. It's only 128x64 pixels in size, but well supported by micro python. So I thought I'll give it a try and implemented a continous WiFI scanner, that's displaying SSIDs directly on the micro controllers display.

First of all I needed some tooling for uploading my python scripts to my micro controller.

pip install --break-system-packages microdeploy

Here's the WiFi scanner script I came up with ('main.py'):

from time import sleep
from machine import I2C
from ssd1306 import SSD1306_I2C
from network import WLAN, STA_IF

d = SSD1306_I2C(128, 64, I2C(sda=14, scl=12))

sta_if = WLAN(STA_IF)
while True:
    d.text("scanning ...", 0, 0)

    scan_result = sta_if.scan()
    for i in range(len(scan_result)):
        d.text("found WiFi:", 0, 0)
        d.text(scan_result[i][0], 0, 16)
        d.text("({}/{})".format(i+1, len(scan_result)+1), 0, 48)

Micro Python will automatically start a script called 'main.py' on boot. So all that's left to do is uploading the script to my micro controller:

microdeploy --port /dev/ttyUSB0 --baud 115200 device put ~/main.py main.py

written by uniq on 2024-02-13