# ML-C12: isolated BLE training lab

Keep every file in this folder together. Board: ESP32-S3 MicroPython v1.29.0 SPIRAM_OCT. Host: Python3.13, `bleak==3.0.2`, mpremote. Use an ordinary terminal and one training board. Supported desktop backends: Windows11 build22000+, macOS10.15+, Linux BlueZ5.55+. Bluetooth adapter and OS permission are required. A phone is not part of this workflow.

This course application defines its own service; these are not MicroPython built-in provisioning commands. The open BLE characteristics do **not** enforce authenticated encryption or ownership. Use only an isolated 2.4GHz WPA2-Personal training router with non-sensitive credentials and DHCP. Do not enter home/work/school credentials. Never enable BLE packet/debug logging during this lab. The file is plain text. This example leaves BLE setup active; it is not a production access-control design.

## Install and use

From this folder: `py -m pip install bleak==3.0.2 mpremote` (use python3 instead of py on macOS/Linux). Identify the runtime USB port with `py -m mpremote connect list`. Close other serial apps. Use the owner's front-view native USB socket at bottom LEFT, antenna up. COM is on the right. No external circuit or power supply.

Replace PORT with the actual port. First list board files with `py -m mpremote connect PORT fs ls`. Back up every existing file you will replace, e.g. `py -m mpremote connect PORT fs cp :main.py main-before-c12.py`. Copy existing lab_config.py, protocol.py and ble_setup.py to private uniquely named backups too. An absent file can be recorded; other backup errors must be solved. Never overwrite an existing local backup. Do not change boot.py. A config backup contains secrets.

Copy helpers: `py -m mpremote connect PORT fs cp lab_config.py protocol.py ble_setup.py :`. Copy startup last: `py -m mpremote connect PORT fs cp main.py :main.py`. Press upper RST, with lower BOOT released. Wait20seconds if a saved config exists. Normal hardware startup runs main.py; a tool's raw soft reset is not the persistence proof.

Run `py ble_client.py`. Confirm YES at the prompt after identifying your one training board. Enter SSID and hidden password interactively. No credential command-line arguments exist. getpass echo fallback is rejected. The client scans by service UUID, verifies service/characteristics/properties, subscribes to TX before hello/configure, and reassembles notifications. Expected events: accepted → saved → joining → connected. configured may already be true if replacing an old config. connected uses STA isconnected(), meaning association plus a valid IP; it does not mean Internet access. A BLE write response only acknowledges transport.

Let client exit. Press RST with BOOT released, wait20seconds. Run `py ble_client.py --status`. It sends no credentials and asks for one redacted snapshot delivered by a TX notification. connected + configured:true after this normal startup proves the application loaded a valid saved config and connected. If still joining, wait and reconnect for another snapshot. No repeated GATT-read polling is used. A missing/lost notification is incomplete evidence; reconnect for a snapshot, never blindly retry.

## Protocol v1

- Advertised name Molo-C12; name/service UUID are not identity authentication.
- Service b6201001-3e67-4d7b-a469-d9aa8c120001.
- RX b6201002-3e67-4d7b-a469-d9aa8c120001, WRITE with response.
- TX b6201003-3e67-4d7b-a469-d9aa8c120001, NOTIFY.
- Newline-terminated UTF8 JSON; maximum384bytes excluding newline. Both directions use pieces≤20bytes. Decode only a complete frame. Overflow ends the session. Single client, bounded RX buffer and TX queue; 30ms pacing.
- Commands: `{"op":"hello"}`, `{"op":"status"}`, or `{"op":"configure","config":{"schema":1,"ssid":"...","password":"..."}}`. Unknown fields rejected. Never paste real credentials into a shell/REPL.
- Config `/lab_wifi.json` exactly schema,ssid,password. schema exact integer1. SSID1–32UTF8bytes. WPA2 passphrase8–63printableASCII. Compatible with C09/C11.
- Every notification exactly `{"v":1,"state":"...","configured":true/false}`. No SSID/password/raw exception payload. accepted validates fields; saved writes and read-checks a temporary file then renames. No universal power-loss atomicity claim. joining is nonblocking; connected requires station isconnected; timeout stops/disconnects after15seconds. busy rejects replacement during a pending attempt. save_failed leaves the old configured flag/file meaning intact. Unexpected/stale status fields are rejected by the client.
- A disconnected central loses its partial incoming frame and queued output. The board does not leak one session's buffered data into another. Notifications are not acknowledged application delivery: lost data requires reconnect and a new snapshot.

## Failure and cache recovery

No board: check USB power, app installed/running, Bluetooth enabled, adapter and terminal permissions. Use one board; close other BLE apps. timeout/connect_failed: check training SSID/password, 2.4GHz WPA2, range, router power and DHCP. save_failed: check space/files through USB. busy: wait then reconnect. Invalid frames must be corrected, with fresh reconnect after overflow.

Missing/wrong UUID/properties after firmware changes: keep credentials unsent. Close clients, reset, verify intended files by USB and rescan. Refresh/remove a general app's stored service entry. If OS cache persists, remove only the identified board: BlueZ5.62+ `bluetoothctl -- remove BOARD_ADDRESS` then rescan; older versions may need administrator help with their cache. On Windows remove its saved Bluetooth device entry if one exists and rescan. Bleak documents no universal macOS cache-purge recipe; restart client/Bluetooth, verify app over USB and seek platform help if mismatch persists. Never guess numeric handles. Gate success on correct service/RX WRITE/TX NOTIFY + subscribed v1-ready handshake again.

Stop client. Connect native USB and `py -m mpremote connect PORT repl`, Ctrl-C to interrupt app. Type `import lab_config` then `print(lab_config.clear())`. Only /lab_wifi.json is removed; station is stopped. Deletion is not secure flash erasure, and independent backups remain. Exit Ctrl-], restore backed-up startup/helper files or remove only course files that did not exist before, then RST. USB firmware recovery is ML-C10 if REPL cannot be reached.

## Software checks and attribution

`py -m unittest -v test_lab` uses fake radio/client/BLE and isolated files; it does not establish physical flashing, enumeration, radio coexistence, notification delivery, association or DHCP on your device.

Original course implementation. Architecture informed by MicroPython's MIT-licensed example https://github.com/micropython/micropython/blob/v1.29.0/examples/bluetooth/ble_uart_peripheral.py ; no copied advertising helper or raw-receive echo. APIs: https://docs.micropython.org/en/v1.29.0/library/bluetooth.html and network.WLAN.html. Client: https://bleak.readthedocs.io/en/latest/api/client.html and troubleshooting.html. Course uses no bundled third-party library code; install Bleak and mpremote separately with their own licenses.
