Skip to content

Add a Bambu Lab Printer widget that reads printers over MQTT - #23

Open
masonjmiller wants to merge 5 commits into
kemalandic:mainfrom
masonjmiller:bambu-widget
Open

masonjmiller wants to merge 5 commits into
kemalandic:mainfrom
masonjmiller:bambu-widget

Conversation

@masonjmiller

@masonjmiller masonjmiller commented Oct 7, 2026 •

Copy link
Copy Markdown

Another native widget, built with Claude like the Cameras one (#22): a Bambu Lab Printer widget (Network) for keeping an eye on a print from the dashboard. It talks to the printer directly on the local network. There's no Home Assistant, no Bambu account and no cloud.

The Bambu Lab Printer widget in a tall tile on a XENEON EDGE: the job name, the slicer's picture of the plate filling in from the bottom, the progress bar at 29% with time left and finish time, layer 6 of 122, nozzle and bed temperatures, and four AMS spools with the active one ringed

What it does

  • The print: the job, a progress bar, the time left and the clock time it will finish, and the layer count.

    • The state: Idle, Preparing, Printing, Paused, Finished or Failed.
    • What it's busy with before the first layer, such as "Heating bed" or "Auto bed levelling".
    • Nozzle and bed temperatures.
  • Spools: each AMS slot and the external spool in its filament's colour, with a ring on the one printing.

  • Alerts: HMS alerts show in the same code the printer's own screen uses.

  • Picture, once the widget is five columns wide:

    • Camera: the live view of P1 and A1 printers.
    • Model: the slicer's picture of the plate being printed, faint at first and filling in from the bottom with the progress bar's percentage.
    • None.

    Tall tiles stack: the job on top, the picture filling the middle, the numbers below. Wide tiles put the picture beside the details.

  • Setup: Find Printers in the widget's settings lists the Bambu printers on the network. Pick one, then type the access code from the printer's network settings screen. A line under the fields says "Connected to P1S" or what went wrong, such as "Wrong access code".

  • Follows the printer: the widget keeps the printer's serial number. If the router gives the printer a new address, the widget finds it again and saves the new address.

  • Dropping off the network: a printer that stops answering keeps its last status, marked OFFLINE, and is tried again with backoff. A wrong access code waits for a new one instead of retrying.

  • Camera only while visible: the camera streams only while the widget is on screen and the displays are awake.

How

  • Status over MQTT. Every Bambu printer runs an MQTT broker on port 8883 for its slicer. BambuMQTTSession connects over TLS (Network.framework) as bblp with the printer's LAN access code, and subscribes to device/{serial}/report.
    • It asks for the full status once on connecting, and again every ten minutes; the community protocol notes (OpenBambuAPI) warn that asking more often slows a P1P down.
    • P1 and A1 printers then send only what changed. BambuJSON.merging lays each report over the last: objects merge key by key, and lists of objects with an id (AMS units, trays) merge entry by entry.
  • MQTT.swift is just enough MQTT 3.1.1 for this: CONNECT, SUBSCRIBE, PUBLISH, PINGREQ and DISCONNECT, all at QoS 0. CONTRIBUTING rules out third-party code, and this much of the protocol is small.
  • Read-only. The widget never sends anything that would move or heat the printer. Bambu's Authorization Control gates control commands, not status, so this works in the printer's standard LAN mode without Developer Mode.
  • The serial number comes from the printer's TLS certificate, whose common name it is. So the address and the access code are all anyone enters. The serial's first three characters give the model.
  • Camera. P1 and A1 printers stream 1280×720 JPEGs over TLS on port 6000: an 80-byte login, then a 16-byte header before each picture. A P1S sends about one a second.
  • Model picture. The printer serves its files over FTPS (port 990, TLS from the first byte), and a job's .3mf carries the slicer's PNG of each plate.
    • The printer refuses REST (502), so a file can't be read from the middle. Bambu Studio and OrcaSlicer put the pictures first and the G-code last, though.
    • ThreeMFPreviewReader reads the ZIP entries as they stream in. It hangs up once it has the picture of the plate being printed, which is named by that plate's own plate_N.json or plate_N.gcode entry. That's about 100 KB, two seconds, for a project of any size, once per job.
    • It handles entries whose sizes come after the data (flag 0x08), and both stored and deflated pictures (Compression).
    • BambuPreviewSession is the FTP client: USER, PASS, PBSZ, PROT P, TYPE I, PASV and RETR.
  • Discovery. Bambu printers don't use Bonjour.
    • Their own announcements go to UDP 2021, which Bambu Studio binds exclusively. Taking it would break Bambu Studio's printer discovery if it were open on the same Mac.
    • BambuDiscovery instead tries port 8883 on every address of the Mac's IPv4 networks (each narrowed to 256 addresses). It then finishes a TLS handshake only on the few that answered, and keeps those whose certificate comes from Bambu's "BBL CA" with a serial-shaped name. Nothing is sent to the printer, and a home network takes two to four seconds.
  • The access code goes to the Keychain through the existing KeychainSecretStore (service ai.pakslab.edgecontrol.bambu), keyed by address. It stays out of layout.json.
  • Certificates. Bambu printers present certificates from Bambu's own authority, which macOS doesn't know, so they're accepted without checking the authority. The access code only goes to the address that was typed or picked. Pinning Bambu's CA certificate would be stricter; I left it out to avoid bundling a vendor certificate, but I'm happy to add it.
  • Wiring:
    • ServiceKey.bambu and BambuService;
    • ConfigFieldType.bambuPrinter, rendered by BambuPrinterEditor;
    • NSLocalNetworkUsageDescription in Info.plist.

Choices worth a look

  • Searching only when asked. The background search runs only after a printer that answered before has failed twice, and at most every five minutes per printer. Sweeping the network on a timer would be rude on shared networks.
  • The access code is keyed by address. The serial number isn't known until the first connection, so the address is the key. When a printer moves, its code is copied to the new address.
  • Dark filament in the model picture is lifted to grey on a faint plate, or a black print would vanish on a dark dashboard.
  • The fill-in follows the percentage, not layers. Layers would be truer to height, but the picture is drawn at an angle, so a flat part fills most of its height. A wide base also takes most of a print's time in its first layers: a shelf at 63% on layer 14 of 122 looked barely begun when the fill followed layers.
  • Padding. The widget pads 6 points more than Theme.widgetPadding, because its text runs the width and height of the tile. With a widget gap of 0, that padding is all that keeps it off the edge.

Testing

  • xcodebuild test passes in full: 442 Swift Testing tests in 47 suites, plus the XCTest suite. 55 of the tests are new, in ten suites:
    • MQTT encoding and decoding;
    • report merging;
    • status;
    • the service, with stand-in connections;
    • camera framing;
    • the .3mf picture reader, against synthetic ZIPs laid out like Bambu Studio's;
    • FTP replies;
    • picture fetching;
    • discovery;
    • whether the widget is on screen, so its camera keeps streaming on the dashboard's right half.
  • The fixtures are a recorded P1S full report and update, with the serials, IDs, addresses and job name replaced.
  • Against a P1S mid-print in standard LAN mode, without Developer Mode, with Bambu Studio open on the same Mac (macOS 27.2):
    • On a XENEON EDGE: the status and its updates, and the model picture (the right plate of a two-plate project) in stacked and side-by-side layouts.
    • Run directly against the printer:
      • the camera stream: 1280×720, about a frame a second;
      • the network search, which found the P1S in five runs out of five, in 2–4 s;
      • a wrong access code, refused by both MQTT and FTPS.
  • Not tried on an X1, A1 or H2. The camera is P1/A1 only; on the others the model picture still works.

Overlaps with #22

#22 (Cameras) and the Printers PR touch the same places this one does:

  • ConfigFieldType, ServiceKey, AppModel and WidgetRegistry;
  • NSLocalNetworkUsageDescription;
  • the README's widget count;
  • the guide.

Whichever lands later, I'll rebase. The conflicts are a line or two each.

Not in this PR

  • The X1 and H2 cameras, which stream RTSPS that AVFoundation can't play.
  • Controls like pause, stop or the light, which need Developer Mode or Bambu's authorization.
  • Several printers in one tile.

🤖 Generated with Claude Code

masonjmiller and others added 5 commits October 7, 2026 16:01
Bambu Lab printers are the 3D printers most likely to sit on a desk next to
an Edge, and a print runs for hours: what's left, what's printing, which
spool is feeding and how the first layer looks are things worth a glance.

Every Bambu printer runs an MQTT broker on port 8883 for its slicer and
publishes its status there about once a second. The widget connects with
the user "bblp" and the printer's LAN access code, subscribes to its report
topic and asks once for the full status; P1 and A1 printers then send only
what changed, which BambuJSON merges in, so the picture stays whole. The
serial number the topics are named after comes from the printer's TLS
certificate, so the address and access code from the printer's network
screen are all anyone types in. The code goes to the Keychain, not the
layout file.

The widget only reads. Nothing is sent that would move or heat the printer,
so it works with Bambu's standard LAN access and needs neither Developer
Mode nor a Bambu account.

MQTT.swift is just enough of MQTT 3.1.1 for this: CONNECT, SUBSCRIBE, one
PUBLISH, keep-alive pings. EdgeControl takes no third-party dependencies,
and the protocol is small. P1 and A1 cameras stream JPEGs over TLS on port
6000, about one a second; the widget shows them beside the print once it is
five columns wide, only while it is on screen and the displays are awake.
X1 and H2 cameras stream RTSP, which AVFoundation can't play, so they show
no camera.

The tile shows the job, progress, time left and the clock time it will
finish, layers, nozzle and bed temperatures, each AMS spool in its color
with a ring on the one printing, and any HMS alert in the code the
printer's own screen uses. A printer that drops off keeps its last status,
marked offline, and is tried again with backoff; a wrong access code waits
for a new one.

The tests read a recorded P1S report, with identifiers replaced.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The camera shows what's on the bed; the slicer's drawing of the plate
shows what it will be. A Picture setting picks one: Camera, Model or None.

Bambu printers serve their files over FTPS on port 990 with the same user
and access code as MQTT, and the job's .3mf carries the slicer's picture of
each plate. The printers don't support starting a download partway, but
Bambu Studio and OrcaSlicer write the pictures first and the G-code last, so
ThreeMFPreviewReader reads the ZIP's entries as they arrive and hangs up as
soon as it has the picture of the plate being printed, named by the plate's
own JSON or G-code entry. That is about 100 KB, a couple of seconds, for a
project of any size, once per job.

The model is drawn faint where it hasn't printed and solid up to the current
layer. Dark filament is lifted to grey on a faint plate, or a black print
would disappear into a dark dashboard.

A tile at least 0.7 times as tall as it is wide stacks: the job across the
top, the picture filling the middle, progress, temperatures and spools
across the bottom. Wider tiles keep the picture beside the details. The
widget's padding is a little larger than usual, since its text runs the
width and height of the tile; with a widget gap of 0 that padding is all
that keeps it off the tile's edge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Bambu printers don't use Bonjour, and their own announcements go to a UDP
port Bambu Studio keeps for itself; taking it would break Bambu Studio's
discovery. But every Bambu printer answers TLS on its MQTT port with a
certificate from Bambu's authority whose common name is its serial number,
and the serial names the model. BambuDiscovery tries the port on every
address of this Mac's networks, narrowed to 256 addresses each, then
finishes the handshake only on the few that answered, since a P1 busy
printing can take a couple of seconds over it. No access code is sent and
nothing is asked of the printer. A home network takes two to four seconds.

Find Printers in the widget's settings lists what it finds, and choosing
one fills in the address, leaving only the access code to type. The search
runs only when asked, with one exception: the widget now keeps the
printer's serial number, and a printer that stops answering at its address
is looked for once, at most every five minutes, in case the router gave it
a new one. Found elsewhere, its access code moves with it and the widget
saves the new address.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On a monitor shelf at 63%, layer 14 of 122, the model looked barely begun:
a wide base takes most of a print's time in its first layers, and the
picture is drawn at an angle, so a flat part fills most of its height
anyway. The fill now follows the same percentage as the progress bar under
it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The widget decided it was off screen by comparing its frame in the
dashboard's coordinates with the dashboard's bounds from
GeometryProxy.bounds(of:), which are in the widget's own coordinates. A
widget whose left edge was right of about the middle counted as off screen,
and its camera stopped three seconds after it appeared. Both rectangles
are now in the widget's coordinates, as in the Cameras widget's fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant