Skip to content

Instantly share code, notes, and snippets.

@disconn3ct
Last active February 2, 2025 14:17
Show Gist options
  • Save disconn3ct/7257eb31fa278ae9f4ca0b3b0892cb7b to your computer and use it in GitHub Desktop.
Save disconn3ct/7257eb31fa278ae9f4ca0b3b0892cb7b to your computer and use it in GitHub Desktop.
Mosquitto relay for Bambu Labs printers - deprecated

Bambu Labs Proxy

DEPRECATED: Use this instead https://github.com/disconn3ct/bambu-proxy/

This is a proxy for the Bambu Labs printer MQTT broker. It uses Mosquitto to multiplex many clients to a single printer without hitting the connection limits. If you do not have a BBL printer, DO NOT BUY ONE. Get something else. Seriously. I'm not kidding. If you really want one still, I'll sell you mine.

If you are already unfortunately stuck with one, this is a pretty big quality of life improvement.

This configuration creates a dedicated relay that can multiplex hundreds of connections, not just 4. Printer clients (xtouch or octoprint or whatever) connect to the relay directly. Optionally, you can even bridge the dedicated relay to your main MQTT broker. (I did.)

(Optional: Existing MQTT broker)
        || - Optional
(THIS: Bambu Relay Mosquitto Broker)
        || - printer.conf
(Bambu Labs Printer)

It is presented as a Flux Helm installation but it shouldn't be any harder to decode than a docker-compose would be. Let me know if there are questions.

What it does not do

  • Anything non-MQTT. Tools that go beyond standard MQTT require a direct connection.
    • No chamber camera (I have a proxy for that, if there is interest.)
    • No file management (yet..)
    • Pinging or portscanning or otherwise doing something strange to determine if there is a printer

What Works

Everything that uses only MQTT should be fine. This configuration relays the requests to the printer and reports back, while ignoring everything else. I use this with the following projects:

  • WolfWithSword's Node Red flows
    • Requires hacking on the "printer alive" test. Author is unwilling to put an input hook in for environments where ping is inappropriate. but the change can be done by hand on every update. (Instead of ping, it should check $SYS/broker/connection/printer/state.)
  • xTouch esp32 control panel
  • Octoprint BBL sometimes works great
    • There is some dependency on ftp in there that messes with it. Might be an Octoprint architectural problem.
    • Lots of errors in the logs about the chamber images.
    • When it misbehaves, I point it straight to the printer and it works as well as it ever does. (Not great, but that is not the author's fault.) Since the relay only uses one connection, and the slicer uses one, there is usually one available for Octoprint.
  • Home Assistant via HACS.
    • Seems to be fine, even combined with the NR flows.
    • Had to rename a some devices to prevent conflicts.
    • Shows much more accurate information than Octoprint, such as ETA and current layer.
---
apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata:
name: &name mosquitto-printer
namespace: homeautomation
spec:
chart:
spec:
chart: mosquitto
version: 2.4.1
sourceRef:
kind: HelmRepository
name: t3n # https://storage.googleapis.com/t3n-helm-charts
valuesFrom:
- kind: Secret
name: mosquitto-printer-users
valuesKey: USERS
targetPath: authentication.passwordEntries
## use mosquitto_passwd to generate it. The result looks like an htpasswd file. (`username:$wdaeflkjasdfljsdaflkdfs`)
## The following users must exist:
## bblp: Password is the printer access code
## exporter: Random password, used by the Prometheus metrics exporter
## Optional users:
## mosquitto: If you want to bridge the printer-relay to the main broker. Random password.
## admin: For tools like MQTTX on the relay. Random password.
postRenderers:
- kustomize:
patches:
- target:
version: v1
kind: Deployment
name: *name
# Fix exporter config for auth
# Use mqtt instead of websocket for health. No idea why websocket failed. Don't care.
patch: |-
- op: add
path: /spec/template/spec/containers/0/env
value:
- name: BROKER_ENDPOINT
value: "tcp://127.0.0.1:1883"
- name: MQTT_USER
value: exporter
- name: MQTT_PASS
valueFrom:
secretKeyRef:
name: mosquitto-exporter
key: password
- op: replace
path: /spec/template/spec/containers/1/livenessProbe/tcpSocket/port
value: mqtt
- op: replace
path: /spec/template/spec/containers/1/readinessProbe/tcpSocket/port
value: mqtt
values:
image:
repository: eclipse-mosquitto
tag: 2.0.20
service:
type: LoadBalancer
externalTrafficPolicy: Cluster
ports:
mqtt:
port: 1883
protocol: TCP
websocket:
port: 9090
protocol: TCP
mqttssl:
port: 8883
protocol: TCP
persistence:
enabled: false
authorization:
acls: |-
# applies to everyone
pattern readwrite #
#
user exporter
topic #
topic $SYS/#
#
user admin
topic #
topic $SYS/#
#
user bblp
topic read device/+/report
topic readwrite device/+/request
#
user mosquitto
topic read device/+/report
topic readwrite device/+/request
topic read $SYS/broker/connection/printer/state
config: |-
persistence false
log_dest stdout
listener 1883
listener 8883
cafile /mosquitto/ssl/tls.crt
certfile /mosquitto/ssl/tls.crt
keyfile /mosquitto/ssl/tls.key
listener 9090
protocol websockets
#log_type all
log_type error
log_type warning
log_type notice
log_type information
# log_type subscribe
# log_type unsubscribe
sys_interval 10
#
include_dir /mosquitto/conf.d/
#
## Additional volumes.
extraVolumes:
- name: printer-ca
configMap:
name: printer-ca
- name: tls
secret:
secretName: mosquitto-printer-ssl
# Printer config:
- name: printer
secret:
secretName: mosquitto-printer
items:
- key: printer.conf
path: printer.conf
## Additional volumeMounts to the main container.
extraVolumeMounts:
- name: tls
mountPath: /mosquitto/ssl
- name: printer
mountPath: /mosquitto/conf.d/
readOnly: true
- name: printer-ca
mountPath: /mosquitto/printer
readOnly: true
monitoring:
podMonitor:
enabled: true
sidecar:
enabled: true
port: 9234
image:
repository: jryberg/mosquitto-exporter
tag: v0.7.4@sha256:09a5cc8518b61f5b83bc93f6e51ff5c0a331ffc258293f28384b26969f99c82a
# bridge for BBL Printer
# Update values marked with ##
connection printer
address 1.2.3.4:8883 ## Printer IP
bridge_cafile /mosquitto/panda/blcert.pem
bridge_insecure true
remote_username bblp
remote_password 12123 ## Access Code
notification_topic $SYS/broker/connection/printer/state
notifications_local_only true
bridge_protocol_version mqttv311
try_private false
# Set the amount of time a bridge using the automatic start type will wait until attempting to reconnect.
restart_timeout 10 30
start_type automatic
keepalive_interval 30
topic device/01P123123123/report in ## Printer Serial Number
topic device/01P123123123/request out ## Printer Serial Number
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: mosquitto-printer
namespace: homeautomation
spec:
secretName: mosquitto-printer-ssl
commonName: &hostname "printer-relay.EXAMPLE.COM" ## Hostname for the MQTT relay certificate
dnsNames:
- *hostname
issuerRef:
name: letsencrypt-prod
kind: ClusterIssuer
@keithwkx
Copy link

Is there a dummy guide on how do I apply this in docker on my pi? Currently I'm running xtouch, octoeverywhere, ams+(openspool), HA.
My slicer now keeps disconnecting so I suspected it might be mqtt.

@disconn3ct
Copy link
Author

I'll put something together but basically just start with a mosquitto compose file and give it the two config files. You can ignore the certificate stuff and most of the relay yaml.

@keithwkx
Copy link

keithwkx commented Feb 1, 2025

ok so ill just use your compose.yaml & printer config, what other config file is needed here?

@disconn3ct
Copy link
Author

@keithwkx
Copy link

keithwkx commented Feb 2, 2025

ok tqvm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment