# Video URL example # ffplay -rtsp_transport tcp -tls_verify 0 "rtsps://bblp:@:322/streaming/live/1" import ssl import paho.mqtt.client as mqtt import os from dotenv import load_dotenv load_dotenv() # --- CONFIGURATION from .env --- PRINTER_IP = os.getenv("PRINTER_IP") PRINTER_ACCESS_CODE = os.getenv("PRINTER_ACCESS_CODE") PRINTER_SERIAL = os.getenv("PRINTER_SERIAL") TARGET_BROKER_IP = os.getenv("TARGET_BROKER_IP") TARGET_PORT = int(os.getenv("TARGET_PORT", 8883)) TARGET_USERNAME = os.getenv("TARGET_USERNAME") TARGET_PASSWORD = os.getenv("TARGET_PASSWORD") TARGET_PREFIX = os.getenv("TARGET_PREFIX", "bambulab/") # --- TARGET CLIENT (SINK) --- def setup_target(): client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) # TLS configuration for target broker (certificates are verified) client.tls_set(cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2) # Authentication client.username_pw_set(TARGET_USERNAME, TARGET_PASSWORD) try: client.connect(TARGET_BROKER_IP, TARGET_PORT, 60) client.loop_start() # Runs in background thread print(f"[Target] Connected to {TARGET_BROKER_IP}") return client except Exception as e: print(f"[Target] Error: {e}") exit(1) target_client = setup_target() # --- SOURCE CLIENT (BAMBULAB) --- def on_connect_source(client, userdata, flags, rc, properties=None): if rc == 0: # Subscribe to main report topic topic = f"device/{PRINTER_SERIAL}/report" client.subscribe(topic) else: print(f"[Source] Connection failed. Code: {rc}") def on_message_source(client, userdata, msg): import datetime # Add prefix, e.g. bambu/device/SERIAL/report mirror_topic = f"{TARGET_PREFIX}{msg.topic}" # Check if the data has changed if not hasattr(on_message_source, "last_payloads"): on_message_source.last_payloads = {} last_payload = on_message_source.last_payloads.get(mirror_topic) if last_payload != msg.payload: target_client.publish(mirror_topic, msg.payload, qos=0, retain=False) on_message_source.last_payloads[mirror_topic] = msg.payload now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') print(f"[{now}] Received & forwarded: Topic={mirror_topic}, Bytes={len(msg.payload)}") else: now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') print(f"[{now}] Received, but not forwarded (no change): Topic={mirror_topic}") def main(): # BambuLab requires SSL/TLS, but often only accepts insecure certificates source_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) # TLS configuration for self-signed certs source_client.tls_set(cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2) source_client.tls_insecure_set(True) # Auth: User is ALWAYS 'bblp', password is the access code source_client.username_pw_set("bblp", PRINTER_ACCESS_CODE) source_client.on_connect = on_connect_source source_client.on_message = on_message_source try: # Port 8883 is default for BambuLab MQTTS source_client.connect(PRINTER_IP, 8883, 60) source_client.loop_forever() # Blocks here and keeps the script alive except KeyboardInterrupt: print("\nExiting...") target_client.loop_stop() source_client.disconnect() if __name__ == "__main__": main()