Skip to content

Unofficial Golang bindings for communication with BambuLabs 3d printers.

License

Notifications You must be signed in to change notification settings

torbenconto/bambulabs_api

Repository files navigation

Logo

Bambulabs API Golang Library

Important

This app is still in development and no public release is available. Consider starring the repository to show your support.

This repository provides a Golang library to interface with Bambulabs 3D printers via network protocols. It allows easy integration of Bambulabs printers into your Go applications, providing access to printer data, control over printer features, and more.

This project does not support the bambulabs cloud api, but it's sister project bambulabs-cloud-api does.

Table of Contents

Installation

To install the Bambulabs API Golang library, use the go get command:

go get -u github.com/torbenconto/bambulabs_api

Connecting to a Printer

To interact with a Bambulabs printer, you need the following details:

  • IP Address: The local IP address of the printer.
  • Serial Number: The unique serial number of the printer.
  • Access Code: A local access code for authentication.

You can find the IP Address and Access Code in the printer’s network settings. Please refer to the guides below for more detailed instructions:

Once you have the necessary details, you can create and connect to the printer with the following code:

// Replace the values below with the ones for your printer
config := &bambulabs_api.PrinterConfig{
    IP:           net.IPv4(192, 168, 1, 200),
    AccessCode:   ACCESS_CODE,
    SerialNumber: SERIAL_NUMBER,
}
printer := bambulabs_api.NewPrinter(config)
err := printer.Connect()
if err != nil {
    panic(err)
}

Managing Multiple Printers

The PrinterPool is a concurrent, thread-safe structure designed to manage multiple printers in a pool. It allows you to interact with the printers by serial number, retrieve their status or data, and perform operations on them, all while handling multiple printers concurrently. To begin using the PrinterPool, first create an instance of it:

pool := bambulabs_api.NewPrinterPool()

Next, add printers to the pool using the AddPrinter method and passing in a config:

configs := []*PrinterConfig{
    {IP: net.IPv4(192, 168, 2, 5), SerialNumber: "M123ALE29D", AccessCode: "ODJ2j3"},
    {IP: net.IPv4(192, 168, 2, 6), SerialNumber: "M123ALE29E", AccessCode: "LDAdj3"},
}

for _, config := range configs {
    pool.AddPrinter(config)
}

Next, connect to the printers using the built-in ConnectAll method:

err := pool.ConnectAll()
if err != nil {
    panic(err)
}

Once connected, you can interact with the printers in the pool using the various methods provided by the PrinterPool struct. For example, you can toggle the light on all printers in the pool:

err := pool.ExecuteAll(func(printer *Printer) error {
    return printer.Light(light.ChamberLight, true)
})

if err != nil {
    panic(err)
}

You can also retrieve the data of all printers in the pool:

data, err := pool.DataAll()
if err != nil {
    panic(err)
}

for _, printerData := range data {
    fmt.Println(printerData)
}

For operations on one printer in a pool, you can retrieve a printer by serial number using the At method:

printer, err := pool.At("M123ALE29D")
if err != nil {
    panic(err)
}

Finally, you can disconnect from all printers in the pool using the DisconnectAll method:

err := pool.DisconnectAll()
if err != nil {
    panic(err)
}

Basic Examples

Here is a basic example of how to create a connection to a printer and interact with it. This example toggles the printer's light and retrieves some printer data:

package main

import (
	"fmt"
	"github.com/torbenconto/bambulabs_api"
	"github.com/torbenconto/bambulabs_api/light"
	"time"
	"net"
)

func main() {
	config := &bambulabs_api.PrinterConfig{
		IP:           net.IPv4(192, 168, 1, 200),
		AccessCode:   "00293KD0",
		SerialNumber: "AC1029391BH109",
	}

	// Create printer object
	printer := bambulabs_api.NewPrinter(config)

	// Connect to printer via MQTT
	err := printer.Connect()
	if err != nil {
		panic(err)
    }

	// Attempt to toggle light
	err = printer.Light(light.ChamberLight, true)
	if err != nil {
		panic(err)
	}
	
	for {
		time.Sleep(1 * time.Second)

        data, err := printer.Data()
		if err != nil {
			panic(err)
        }
		if !data.IsEmpty() {
			fmt.Println(data)
        }
    }
}

This example establishes a printer pool with two printers, connects to them, toggles the light on both printers, and retrieves their data:

package main

import (
    "fmt"
    "github.com/torbenconto/bambulabs_api"
    "github.com/torbenconto/bambulabs_api/light"
    "net"
    "time"
)

func main() {
    pool := bambulabs_api.NewPrinterPool()

    configs := []*bambulabs_api.PrinterConfig{
        {IP: net.IPv4(192, 168, 2, 5), SerialNumber: "M123ALE29D", AccessCode: "ODJ2j3"},
        {IP: net.IPv4(192, 168, 2, 6), SerialNumber: "M123ALE29E", AccessCode: "LDAdj3"},
    }

    for _, config := range configs {
        pool.AddPrinter(config)
    }

    err := pool.ConnectAll()
    if err != nil {
        panic(err)
    }

    err = pool.ExecuteAll(func(printer *bambulabs_api.Printer) error {
        return printer.Light(light.ChamberLight, true)
    })

    if err != nil {
        panic(err)
    }

    for {
        time.Sleep(1 * time.Second)

        data, err := pool.DataAll()
        if err != nil {
            panic(err)
        }

        for _, printerData := range data {
            fmt.Println(printerData)
        }
    }
}

Development

Current Status: IN RELEASING PROCESS

This library is in active development. While many features have been implemented, certain functions are not fully tested across all supported devices. Contributions are welcome to improve functionality and expand coverage.

Contributing

We welcome contributions to improve this project! If you’d like to contribute, please follow these steps:

  1. Fork the repository
  2. Clone your fork
  3. Create a new branch for your feature or bug fix
  4. Write tests if applicable
  5. Submit a pull request with a detailed description of your changes

Please refer to the CONTRIBUTING.md file for more details on how to contribute.

Links & Resources

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Feel free to join the community and connect with us for help, suggestions, or collaborations:
Join the Discord!

About

Unofficial Golang bindings for communication with BambuLabs 3d printers.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages