From 0e8d7fa7d9fe5aba170cce71696d0f01b02e231f Mon Sep 17 00:00:00 2001 From: jimmy Date: Sun, 23 Aug 2026 23:07:59 +1200 Subject: [PATCH] Add a Microdot AP example and mpremote instructions for device tests. Co-authored-by: Cursor --- test/README.md | 59 +++++++++++++++++++++++++++++++++++++++++++++ test/ap_microdot.py | 46 +++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 test/README.md create mode 100644 test/ap_microdot.py diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..640e955 --- /dev/null +++ b/test/README.md @@ -0,0 +1,59 @@ +# Device tests + +These scripts run **on the board** with MicroPython, not on the host. Use [mpremote](https://docs.micropython.org/en/latest/reference/mpremote.html) from the repo root (`pipenv sync` first). + +Replace `PORT` with your serial device, e.g. `/dev/ttyACM0` or `/dev/ttyUSB0`. List ports with `ls /dev/ttyACM* /dev/ttyUSB*` or `pipenv run mpremote connect list`. + +## Stop the main app first + +If `src/main.py` is already on the board it starts on boot and will fight the tests (Wi‑Fi, I2C, LEDs). Interrupt it, then run the test: + +``` +pipenv run mpremote connect PORT +``` + +In the REPL press `Ctrl-C` until you get `>>>`. Leave that session, or use a second command as below. + +## Copy libraries (needed for Microdot / LSM6DS3) + +`mpremote run` only uploads the one test file. Anything it `import`s must already live on the board. + +``` +pipenv run mpremote connect PORT fs cp -r lib : +``` + +Same as `pipenv run ./dev.py PORT lib`. + +## Run a test + +From the **repo root**: + +``` +pipenv run mpremote connect PORT run test/leds.py +pipenv run mpremote connect PORT run test/lsm6ds3_test.py +pipenv run mpremote connect PORT run test/ap_microdot.py +``` + +`run` copies the file to the device, executes it, and streams stdout. `Ctrl-C` stops it. + +## Access-point + Microdot (`ap_microdot.py`) + +Starts a soft-AP (`led-test`, open by default) and a small HTTP app. + +1. Upload `lib` (so `microdot` imports). +2. `pipenv run mpremote connect PORT run test/ap_microdot.py` +3. Join the `led-test` Wi‑Fi network from a phone or laptop. +4. Open `http://192.168.4.1/` (plain text hello) or `http://192.168.4.1/ping` (JSON). + +SSID/password are at the top of `ap_microdot.py`. Default AP address on ESP32 is `192.168.4.1`. + +## Other useful mpremote commands + +``` +pipenv run mpremote connect PORT # REPL +pipenv run mpremote connect PORT fs ls : # list board files +pipenv run mpremote connect PORT fs cp test/leds.py : # copy a file onto the board +pipenv run mpremote connect PORT exec "import leds" # run a file already on the board +``` + +If the port is already in use (another `mpremote`, serial monitor, or `dev.py follow`), disconnect that first. diff --git a/test/ap_microdot.py b/test/ap_microdot.py new file mode 100644 index 0000000..9a0c710 --- /dev/null +++ b/test/ap_microdot.py @@ -0,0 +1,46 @@ +"""Soft-AP + a tiny Microdot app. + +Requires lib/microdot on the board (see test/README.md). +Connect to SSID AP_SSID, then open http://192.168.4.1/ +""" +import network +from microdot import Microdot + +AP_SSID = "led-test" +AP_PASSWORD = "" # empty = open network +AP_HOST = "0.0.0.0" +AP_PORT = 80 + + +def start_ap(ssid, password): + ap_if = network.WLAN(network.AP_IF) + ap_if.active(True) + if password: + ap_if.config(essid=ssid, password=password) + else: + ap_if.config(essid=ssid, authmode=network.AUTH_OPEN) + # Toggle like src/wifi.py so the AP actually comes up on ESP32. + ap_if.active(False) + ap_if.active(True) + ip, netmask, gateway, dns = ap_if.ifconfig() + print("AP ssid=%s ip=%s" % (ssid, ip)) + return ap_if, ip + + +app = Microdot() + + +@app.route("/") +async def index(request): + return "hello from microdot\n", 200, {"Content-Type": "text/plain"} + + +@app.route("/ping") +async def ping(request): + return {"ok": True} + + +if __name__ == "__main__": + _, ip = start_ap(AP_SSID, AP_PASSWORD) + print("listening on http://%s:%d/" % (ip, AP_PORT)) + app.run(host=AP_HOST, port=AP_PORT, debug=True)