A Raspberry Pi that stops a failed 3D print in the night
A print started at 23:40 can be a pile of filament by morning. A board, a camera and a relay: one question every two minutes, and the power is cut after two yeses in a row.
At 23:40 a ten-hour print is started. Some time after three, the part comes loose from the bed. The printer does not know. It goes on laying filament in the air for seven hours, and in the morning there is a nest of plastic where the part should be, and a good part of a spool gone.
Anyone who watched the printer would have seen it within a minute. The trouble is that nobody watches a printer at three in the morning.
This post builds the watcher from our short film “Give your board eyes”: a Raspberry Pi with a camera pointed at the bed, a relay on the printer’s power, and one question asked again and again.1
The parts
- A Raspberry Pi with a camera module, looking at the print bed.
- A relay on GPIO 17 that carries the printer’s power.
- A Camly API key.
A word on the relay before anything else: it switches mains power. Use a relay module or a controllable socket rated for the printer’s load, in a proper enclosure. If mains wiring is not something you do, have the board drive a ready-made switched socket instead of building your own.
The whole program
import os, time, requests
from picamera2 import Picamera2
from gpiozero import OutputDevice
KEY = os.environ["CAMLY_API_KEY"]
cam = Picamera2(); cam.start()
relay = OutputDevice(17, initial_value=True) # the printer's power
yes = 0
while relay.value:
cam.capture_file("now.jpg")
r = requests.post("https://api.camly.ai/v1/runs",
headers={"Authorization": f"Bearer {KEY}"},
data={"config": '{"type":"event","question":"Has the 3D print failed?"}'},
files={"image": open("now.jpg", "rb")}, timeout=60).json()
yes = yes + 1 if r["result"]["flagged"] else 0
if yes == 2: relay.off() # two in a row: cut the power
time.sleep(120)
Sixteen lines of code. The picture, the question, the answer in result.flagged, and your decision: after two yeses in a row the relay opens, and the loop ends because the relay is off.
The request is the API’s real one. The camera and relay lines are the usual ones of picamera2 and gpiozero; we have not run this exact program on a board ourselves, so treat it as a starting point.
Why two yeses
One picture can catch the print head in front of the part, a strand of support material that looks like a tangle, or the moment between two layers. Cutting the power on one doubtful picture ruins a good print, and a watcher that ruins good prints gets unplugged.
Two yeses in a row, two minutes apart, is a failure that lasted. A single yes followed by a no resets the count. Four minutes of wasted filament is a fair price for not killing prints that were fine.
It is the same idea the API applies on the server when it turns checks into incidents, written here in two lines because the board keeps its own state. The server-side version is in from checks to incidents.
Getting the question right
“Has the 3D print failed?” is the question from the film, and it is a broad one. Failures look different: a part knocked off the bed, a tangle of loose strands, a blob around the nozzle. Before trusting the relay to it, test it on your printer.
- Keep a few pictures of failures you have had, and a few of good prints at different stages, including the first layers, which look like very little.
- Send them with a live key and read
result.descriptionfor each. The sentence says what the model saw. - If a good print is flagged, say what counts in the question itself: “a failed print is a tangle of loose filament or a part detached from the bed; a part that is simply unfinished is not a failure”.
Ten trial pictures cost five cents. We have not measured how reliably this question separates failed prints from good ones, so this step is not optional.
Light matters more than the wording. A printer in a dark room gives the camera nothing; a small lamp on the bed, left on for the night, does more than any rewording.
What cutting the power does
The relay removes power from the whole printer: heaters, motors and fans together. That stops the waste at once. It also stops the fan that cools the hot end, so the heat left in it soaks upwards for a while, and on some machines that can leave a clog to clear the next day.
If your printer can be paused through its own interface, by a command over USB or a network call, sending a pause on the second yes is the gentler action, and the relay can stay as a last resort. The program does not care: the last line of the loop is yours.
Either way, keep the picture of the second yes in a dated file. In the morning it shows why the board acted.
What it costs
A check costs €0.005 at the time of writing.2
| Print length | Checks, one every two minutes | Cost |
|---|---|---|
| 2 hours | 60 | €0.30 |
| 10 hours | 300 | €1.50 |
| 24 hours | 720 | €3.60 |
The program only runs while a print does, so there is no cost between prints. Set a budget on the key in the console all the same: it caps the month if the board ever gets stuck in a loop. The reasons, and the other protections worth having on a board (a key that can only run checks, retries that do not pay twice, stopping on errors that will not fix themselves), are in give a Raspberry Pi eyes and what it costs, and how to keep it that way.
One addition is worth making before the first night: when the API cannot be reached, the reply has no result and the program stops with an exception. Catch it and keep the count as it was. A board that cannot ask should neither cut the power nor give up watching.
Same code, another question
The program is about a printer in one sentence only. With another question and a buzzer in place of the relay, the same lines tell you that a long job on another machine has finished, or that its output tray is full. A greenhouse version, with a pump in place of the relay, is in a Raspberry Pi that waters the seedlings when they wilt.
Sources
- Camly AI, “Give your board eyes: the 3D printer”, short film on the Camly API for makers, November 2026. The program above is the one shown in the film, with a check every two minutes.
- Camly API, all guides as plain text: Quick start, Send from a device, Keys, Limits and prices. platform.camly.ai
- Camly API contract (OpenAPI). api.camly.ai