Arduino UNO Q with a camera monitoring a 3D printer, next to it a laptop showing the AI print watchdog's status page with a red alarm

AI Print Watchdog with the Arduino UNO Q (Part 4): Building the Watchdog App with App Lab

📚 Series: AI Print Watchdog with the Arduino UNO Q

Part 1: Idea, Case and Camera Mount
Part 2: Camera Setup and Training Images
Part 3: Training the Anomaly Model with Edge Impulse
Part 4: Building the Watchdog App with App Lab (this article)
Part 5: Optimisation and Finalisation

Status of the experiment: the FOMO-AD model is trained and cleanly separates normal prints from spaghetti failures (part 3). Today it becomes an actual watchdog: an app on the Arduino UNO Q that scores a camera frame every 15 seconds while printing, raises an alarm on anomalies — in Home Assistant, on the LED matrix, and with photo evidence — and can optionally pause the printer. And since no first day in production goes without surprises: there were three solid lessons, and several real catches to make up for them. Let’s take it in order.

The hardware for this series – an Arduino UNO Q*, its Media Carrier* and an IMX219 camera* – is covered in part 1.

Links marked with * are affiliate links, which makes them advertising. If you buy through one, I earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases. Learn more

The plan: what the app should do

Before writing code, the ground rules. Rule one was fixed back in the concept phase: a single false alarm that pauses a nine-hour print is worse than having no watchdog at all. So the app doesn’t judge each frame on its own — the alarm only fires when three of the last four checks were above the threshold; a single outlier fizzles out. Rule two: the watchdog is only active while an actual print is running; the printer’s Moonraker API tells us whether that’s the case. And rule three: the automatic print pause is opt-in and stays off until the threshold has proven itself in everyday use. That results in a simple cycle: check print status → grab a frame → ask the model → apply the time filter → raise the alarm if needed.

App anatomy: how an App Lab app is structured

An app in Arduino App Lab is a folder under ~/ArduinoApps/ with a clear structure: app.yaml describes the app and lists the “bricks” — ready-made building blocks that App Lab provides as Docker containers. The Linux part (our watchdog logic) lives in python/, the microcontroller part in sketch/ — it runs on the UNO Q’s STM32 and drives the LED matrix as our alarm light. The two worlds talk via the “Bridge” — a function call from Python arrives as a callback in the sketch. Our app.yaml is pleasantly short (trimmed to the essentials here):

name: Spaghetti-Watchdog
icon: 🍝
bricks:
  - arduino:web_ui:      # status web page on port 7000
  - arduino:visual_anomaly_detection:
      model: spaghetti-fomo-ad-v1

Two bricks are all it takes: visual_anomaly_detection runs our Edge Impulse model (more on that in a moment), web_ui serves the status page later on. You’ll find the complete app code in the project’s GitHub repository.

Getting the app onto the board

Before we go into detail: this is how the app gets onto your UNO Q. Via SSH (familiar from part 2), clone the repository and copy the app folder to the place where App Lab looks for apps:

git clone --depth 1 https://github.com/raspberry-tips/uno-q-3d-print-watchdog.git
cp -r uno-q-3d-print-watchdog/app ~/ArduinoApps/spaghetti-waechter

Then reload App Lab once — the app shows up among your own apps with the 🍝 icon. In part 5 I will additionally package the app as a ready-made zip for App Lab’s “Import App” button; for today the clone route is the most direct one, and you get the code right there to read along.

Registering your own model — the undocumented part

The anomaly brick ships with a sample model (a concrete crack detector). How to feed it your own Edge Impulse model was documented nowhere at the time of writing — so consider this the first documentation. You need two things: the .eim file from part 3 (the “Linux aarch64” export) and a small registration file. After the export, the .eim initially sits as a download on your computer — it gets onto the board via SCP. On Windows the graphical way is WinSCP (same credentials as SSH; just drag the file into the home directory and rename it to spaghetti-fomo-ad-v1.eim while you are at it), on macOS/Linux it is a one-liner in the terminal:

scp raspberry-tips-*.eim arduino@<board-ip>:~/spaghetti-fomo-ad-v1.eim

On the board, the file then moves to its place in ~/.arduino-bricks/ei-models/ and needs execute permissions:

mkdir -p ~/.arduino-bricks/ei-models ~/.arduino-bricks/models/custom-ei/spaghetti-fomo-ad-v1
cp spaghetti-fomo-ad-v1.eim ~/.arduino-bricks/ei-models/
chmod +x ~/.arduino-bricks/ei-models/spaghetti-fomo-ad-v1.eim

Next to it, create the registration as ~/.arduino-bricks/models/custom-ei/spaghetti-fomo-ad-v1/model.yaml — it tells App Lab that this model exists and which brick should load it via which environment variable:

id: "spaghetti-fomo-ad-v1"
name: "Spaghetti-Waechter FOMO-AD v1"
runner: "brick"
description: "Anomaly detection for 3D print failures"
bricks:
- id: "arduino:visual_anomaly_detection"
  model_configuration:
    EI_V_ANOMALY_DETECTION_MODEL: "/home/arduino/.arduino-bricks/ei-models/spaghetti-fomo-ad-v1.eim"

Whether the registration worked is revealed by arduino-app-cli model list — your model has to show up there next to the bundled ones. From then on you can reference it in any app with a model: line.

Important for the cloned app: its app.yaml still has the bundled placeholder concrete-crack-anomaly-detection on that line, so the app starts even without a model of your own. Open ~/ArduinoApps/spaghetti-waechter/app.yaml in nano and replace the placeholder with spaghetti-fomo-ad-v1. Otherwise the watchdog scores your prints with the concrete-crack model and the numbers mean nothing.

Important: retire the data collector from part 2

Only one process can use the camera. The live view service from part 2 starts automatically on every boot — if it’s still running, the app gets no frames and all you’ll see is “No camera frame received” in the log. So, once, before the first app start:

sudo systemctl disable --now liveview capture

The disable is the important part — a plain stop only lasts until the next reboot. If you need the smooth live view again for aiming the camera: stop the app, sudo systemctl start liveview, then reverse the steps. In everyday use you’ll hardly miss it, because the watchdog’s web page shows the camera image anyway.

Home Assistant: MQTT discovery and a little user trap

The app announces itself to Home Assistant via MQTT discovery — a device “Spaghetti Watchdog (UNO Q)” appears automatically with six entities: an alarm sensor (device_class: problem, ready to use in automations and notifications), the current anomaly score as a measurement, the watchdog status (idle / warmup / watching), and — my favourite — a camera entity “Alarm-Bild” that puts the photo evidence on your dashboard when the alarm fires. On top of that come two buttons, “Alarm zurücksetzen” (reset the alarm) and “Fehlalarm bis Druckende” (mute a false alarm until the print ends), so you can acknowledge an alarm straight from your phone. The screenshot below comes from an earlier version: the device is still called “Spaghetti-Waechter (UNO Q)” there, and the two buttons are missing. What you have to configure in Home Assistant itself: nothing.

The spaghetti watchdog device in Home Assistant with the four entities created via MQTT discovery
There automatically: the watchdog device with alarm, score, status and alarm image — without a single line of HA configuration

There’s still a trap hiding in the details: the Mosquitto add-on has no user accounts of its own by default — it authenticates against Home Assistant users. And in Home Assistant, “people” and “users” are two different things: a person without a user account cannot log in to the broker. The clean way: under Settings → People → “Users” tab, create a dedicated, local-only user just for MQTT (mine is simply called mqtt-test) and enter those credentials in the app.

Home Assistant: creating a new local-only user for MQTT under Settings, People, Users tab
The user trap resolved: the MQTT login is a Home Assistant user — “local access only” is all it needs

Moonraker: the printer tells us when it’s printing

How does the app actually know a print is running? It talks to the Moonraker API — Moonraker is Klipper’s HTTP API server, which ships enabled on Klipper printers like my Elegoo Neptune 4 Plus (here on port 80, without authentication on the LAN; Mainsail and Fluidd talk to the printer through this very API too). The question “are you printing right now?” is answered by a simple HTTP GET to /printer/objects/query?print_stats. The app polls this every cycle and uses two fields from the response: state (controls whether frames get scored at all) and print_duration (pure print time — important for a lesson further down). For the later auto-pause, a POST to /printer/print/pause is all it takes — a clean pause with a parked head, deliberately not an emergency stop. You enter your printer’s IP later in the settings panel of the watchdog web page (the “Moonraker host” field).

Moonraker API response to printer objects query print_stats in the browser, state printing
Testable right in the browser: the Neptune 4 Plus print_stats response during a print

No Klipper printer? No problem — the Moonraker link is optional. Simply turn off the “only check while printing” switch in the settings panel and the watchdog evaluates continuously, without any printer API at all. You only lose two conveniences: the automatic start/stop of monitoring and the auto-pause. And if your printer speaks a different API (OctoPrint & co.): moonraker.py is, at roughly 50 lines, the only place in the code you would have to swap.

Settings panel of the watchdog web page with the only check while printing switch
For setups without Klipper: turn off “only check while printing” and the watchdog runs continuously

First start: Docker pulls, the sketch gets flashed

Starting is a single click: open the “Spaghetti-Watchdog” app in App Lab and press the Run button (▶) at the top. Quite a lot happens on the very first start: App Lab compiles the sketch and flashes it to the STM32 via the internal debug adapter, pulls the Docker images for the bricks (the Edge Impulse model runner is a few hundred megabytes, that takes a few minutes) and then starts the containers. You can follow along live in App Lab’s console window; alternatively via SSH with arduino-app-cli app logs user:spaghetti-waechter --follow. If everything works out, you’ll see the three most beautiful lines of the day: model runner “Healthy”, “App started”, “MQTT connected”.

Starting and stopping the app in App Lab
Starting the app in App Lab

Lesson 1: training pipeline and inference pipeline must be identical

Then the first test print — and disillusionment: every single frame scored above the alarm threshold, on a perfectly normal print. A look at the debug image immediately showed why: the image was deep blue. Initially the app grabbed its frames through App Lab’s camera peripheral — and that renders with a different white balance than the GStreamer pipeline that produced all the training images in part 2. Red and green channels matched the training images almost exactly, only blue was doubled. To the model, every frame looked different from anything it had ever learned.

The solution is a principle worth remembering for any camera AI project: inference must run through exactly the same image pipeline as training. Same resolution, same rotation, same white balance, ideally the same code. So the app now internally starts the exact GStreamer pipeline from part 2 as a continuous stream (the white balance needs a few seconds to settle, which rules out a single-shot approach) and reads its frames from there. After that: colours correct, problem gone.

The first real catch

And then things moved faster than planned: while I was still rebuilding the pipeline, my printer — presumably due to filament that had absorbed moisture — started producing spaghetti in series. Five failed prints in one morning, and the watchdog reported every single one: score above 50, alarm in Home Assistant, blinking LED matrix, photo evidence saved. What was planned as a calibration session involuntarily became the first stress test — passed.

Camera frame of the first real spaghetti alarm: a blob of filament on the print bed, anomaly score 52
The first real catch: spaghetti on the print bed, score 52 — saved as photo evidence

Especially helpful: the app draws into the alarm image which regions bothered the model. FOMO-AD doesn’t just score the image as a whole but on a grid — the brick returns the suspicious cells as bounding boxes, and a bundled helper function (draw_anomaly_markers) paints them into the image. So you can see at a glance whether the alarm points at the spaghetti blob or at something else entirely.

Alarm image with anomaly regions drawn in: red grid cells mark the spaghetti blob
The same frame with markers: the red grid cells show which regions bothered the model

Lesson 2: the start phase needs a warm-up gate

The second lesson came with the next print start: alarm — although nothing had been printed yet. The reason, honestly, is a hole in my training data: during homing and the purge line, the bed moves all the way to the front and the camera suddenly sees the printer frame, the rails and my wooden desk. Images like that barely existed in training — the data collector was always started only once the print was already running. To the model, the start phase is maximally “anomalous”.

The app now has a warm-up gate for this: during the first 60 seconds of pure print time (that’s what Moonraker’s print_duration delivers — heating doesn’t count), scores are still computed and displayed, but not counted towards the alarm. 60 seconds cover homing and purge, yet leave the critical first layer monitored — which is exactly where most spaghetti is born.

The status web page: log, live image and every setting in the browser

Reading logs over SSH is fine for setup, not for everyday use. That’s why the app got its second brick: web_ui serves a small status page on port 7000 — deliberately kept simple, the page polls a single JSON endpoint every five seconds, no WebSocket tinkering. You reach it in the browser at http://<your-board-ip>:7000 — the app also prints the exact address to the App Lab console on startup (the “Network URL” line). It shows a status light (idle / warmup / watching / ALARM), the current score with its threshold, the current camera image, the last alarm image with markers, and a running event log.

Status web page of the spaghetti watchdog: watching state, score, live view of the print bed and event log
The status page in action: status light, score, live view and event log — here during a clean print

And since thresholds get touched more often than planned, all the settings moved onto the page as well: threshold, check interval, alarm window, warm-up gate, the switches for auto-pause and training frame collection, plus the connection details for Moonraker and MQTT — each with a little info icon explaining what it does. Everything is saved to a JSON file in the app folder that survives restarts; changes apply immediately (only MQTT needs an app restart). One honest note belongs here: the page has no login — anyone on your network can open it and change the settings, MQTT credentials included. The page doesn’t display the stored password, but it sits in plain text in the JSON file on the board. Fine for a tinkering LAN, but use a dedicated MQTT user just for the watchdog and nothing you use anywhere else.

The threshold ships at 100. That’s an observer mode: the app shows the score and the live image and collects training frames, but never raises an alarm. Only once you know which scores your model produces on normal prints do you set a real threshold just above them here. Part 5 shows how to derive it from your own numbers.

Settings panel of the watchdog web page: threshold, interval, alarm window, auto-pause and MQTT access in the browser
Every setting in the browser — each with an info icon, saved to a JSON file that survives restarts

Bonus: the app collects its own training data

One feature practically suggested itself during testing: if the app scores an image every 15 seconds anyway, it might as well keep it. The training buffer stores every inconspicuous frame in rotation — 2,500 at most, the oldest ones get dropped. If you need a re-training later (new lighting, new camera position, different filament), the material is already waiting and goes to Edge Impulse with the upload script from part 3. An important detail: only frames below the alarm threshold are collected — otherwise spaghetti images would sneak into the training data as “normal” during a failed print and poison the model on re-training. The circle back to part 2’s data collector closes: the app is now its own data collector.

Lesson 3 and an honest verdict

Finally the third lesson, and it’s the most important one: while working on the printer I nudged the camera ever so slightly — and the scores of normal prints climbed from below 33 to 45–50, permanently. FOMO-AD doesn’t learn “what spaghetti looks like”, it learns “what my scene looks like when everything is normal” — camera position and lighting are part of the model. A few degrees of offset, and the calibration is off. That’s not a weakness of the concept, but it is a clear message to the hardware: the camera mount must sit absolutely repeatably, or the lighting must become constant — ideally both.

Still, the verdict after day one in production: concept proven. The watchdog detected and reported five real failed prints, every false alarm had a traceable cause, and the complete chain from camera to photo evidence on the Home Assistant dashboard works. What’s missing now is fine-tuning — and that gets its own part.

Coming up in part 5

Part 5 is about optimising and finalising: a stiffer camera mount that makes the position repeatable, a re-training with images from different lighting situations (the training buffer is already collecting), re-calibrating the threshold, arming the automatic print pause — and the app as a finished package you can import straight into App Lab. Until then you’ll find the complete source code in the GitHub repository. Have fun building along!

Update: Part 5 is now online — there the watchdog gets a sturdier mount, a new model and an honestly derived alarm threshold, plus the app as a ready-made zip to import.

Frequently asked questions

Does the watchdog also work with OctoPrint or other printers?

Print detection uses the Moonraker API, so it works with any Klipper printer. For OctoPrint you would only have to swap the small Moonraker class for the OctoPrint REST API — the rest of the app is printer-agnostic. It even works without any printer API: turn off the “only check while printing” switch and the watchdog evaluates continuously.

Do I absolutely need Home Assistant?

No. Without an MQTT broker the app runs just the same — status web page, LED matrix alarm, photo evidence and the optional print pause work independently of it. Home Assistant just makes the watchdog more comfortable: push notifications, a dashboard tile and the alarm image as a camera entity.

Can I use your finished model instead of training my own?

Unfortunately not — and that’s the essence of FOMO-AD: the model learns what your specific scene looks like, including printer, camera position and lighting. My model would be worthless on your setup. But training your own is the easiest part of the series: collect images on the side (part 2), train in about an hour (part 3).

Will the watchdog pause my printer on its own?

Only if you want it to. The auto-pause is deliberately an opt-in switch and off by default — only once the threshold has survived several prints without a false alarm should you enable it. And even then the app only sends a regular pause command to Moonraker that parks the head — not an emergency stop.

Similar Posts