webMCU-AI Motion Anomaly Trainer v002

Matched with firmware-anomaly-v002.ino: train on the machine's two states (off, normal), score every window as an anomaly percent with a per-axis X/Y/Z readout, and keep the page and the device in step over WebBLE, Web Serial or the SD card. Single file, no network.

1. Data source and device link

SD card in this computer
Device over the air
not connected

The SD card is the source of truth. iPhone Safari has no Web Bluetooth: use a Web Bluetooth browser (for example Bluefy), or work from a zip. Serial: close the Arduino IDE monitor first, opening the port can reboot the board. Chrome or Edge on a desktop has the folder picker and Web Serial.

no device info yet
Nothing loaded yet. Pick the SD card root (the folder that contains motion/ and header/), load a zip of it, or pull from a connected device.

2. Classes and data

The firmware has exactly two trained classes: (machine mounted, motor OFF) and (motor running NORMALLY). The anomaly percent is 100% minus the model's confidence in normal. The optional folder holds recordings of a FAULT: it is never trained on, it only lets the page measure how well faults are detected (the device can record into it too).

Get-ready delay before a capture (ms):

Phone motion (DeviceMotion)

off

Phone readings are converted from m/s² to g, mapped with these selectors, and resampled to the device's interval. The XIAO reads about +1 g on Z when flat; iPhone and Android report the opposite sign, so use the check or flip the signs by hand. Live (mapped, g): -

last capture (ax red, ay green, az blue)
Step through one class with a large viewer (arrows, X = mark bad). Clicking a thumbnail opens the same viewer.

3. Train

Model layout (compile-time #defines in the sketch: they must match; changing them discards the model in memory, never the data)
Anomaly settings (runtime on the device through config.json, no recompile)
Calibration
Calibration is the mean and std of a still window (std floored at 0.01), exactly like the sketch. It must be the same on the page and the device, so the model package carries it.
Training settings
Per-axis baseline
no baseline yet
Idle.
Lines to paste into the sketch

Hold-out is the last N samples per class in capture order (the sketch holds out N random ones). Near-duplicate samples taken one after another make validation look better than it is. Training needs samples in BOTH classes, and the baseline is built from the normal class only. Stop keeps the model in memory and does not save.

4. Analyze

No model.
Bars: ML anomaly percent per window (0 = looks normal, 100 = looks nothing like normal): normal (trained on), normal (held out), off, test anomaly. The line is the flag threshold. A window is flagged when its ML percent OR any axis percent reaches it.

Most suspicious (normals that look the most anomalous = possible bad data; test anomalies that look the most normal = missed faults)

5. Infer (live)

phone window
device window (the one the device just used)

6. Transfer and save

Device
SD folder
Files
Saving to the SD folder writes new samples (the next free sN.csv), the weights, calibration, feature baseline, header .h and config.json; existing model files are copied to .bak first. Nothing is saved automatically. A weights .bin alone has no calibration or baseline: save the .zip or push the model over the link.

7. Console

8. Serial monitor (Web Serial)

Device text (frame lines are hidden). Needs Connect Serial.