Package: prusa.connect.sdk.printer
Requirements:basic knowledge ofPrusaConnect API docs.Printer instanceYou can create a Printer instance using the constructor and passingserverandtokento it. These you can find inprusa_printer_settings.ini.fromprusa.connect.printerimportPrinter,constSERVER="https://connect.prusa3d.com"SN='SERIAL_NUMBER_FROM_PRINTER'FINGERPRINT='Printer fingerprint'TOKEN='secret token from prusa_printer_settings.ini'printer=Printer(const.PrinterType.I3MK3,SN,SERVER,TOKEN)printer.loop()# communication loopOr you can usePrinter.from_config()method which reads these values from the ini file.fromprusa.connect.printerimportPrinter,constSERVER="https://connect.prusa3d.com"SN='SERIAL_NUMBER_FROM_PRINTER'printer=Printer.from_config("./prusa_printer_settings.ini",const.PrinterType.I3MK3,SN)printer.loop()# communication loopRegistrationIf the printer has not been registered yet, you need to usePrinter.register()to get a temporary code. This code is then used in theAdd Printerform in Connect Web. After the printer
has been added to Connect,Printer.get_token()will return printer’s persistent token.fromtimeimportsleepfromprusa.connect.printerimportPrinter,constSERVER="https://connect.prusa3d.com"SN='SERIAL_NUMBER_FROM_PRINTER'printer=Printer(const.PrinterType.I3MK3,SN,SERVER)tmp_code=printer.register()print(f"Use this code `{tmp_code}` in add printer form "f"{SERVER}/printers/overview?code={tmp_code}.")token=NonewhiletokenisNone:token=printer.get_token(tmp_code)sleep(1)print("Printer is registered with token%s"%token)TelemetryPrinter must send telemetry to connect at least each second. Because obtaining telemetry values might not be atomic, this must be done in a different thread thanPrinter.loop.fromthreadingimportThreadfromtimeimportsleep...# start communication loopthread=Thread(target=printer.loop)thread.start()# each second send telemetry to internal queue in the main-threadwhileTrue:printer.telemetry(const.State.READY,temp_nozzle=24.1,temp_bed=23.2)sleep(1)EventsEvents are a way to send information about the printer to Connect. They can be split into a few groups:Command answers- Response for Connect if the command was be ACCEPTED,
REJECTED, etc. These are handled by the SDK inPrinter.loopmethod or inCommand.__call__method.State change- indicating that the printer state has changed. This are sent
byPrinter.set_statemethod.FILE INFOevents which are created byFileSystemobject.Alternatively you can inform Connect about other events like attaching/detaching of storage.
You can do this by callingPrinter.event_cb.Examples for these groups follow below.Event callbackYou can inform Connect about some specific situation using events.fromthreadingimportThread...# start communication loopthread=Thread(target=printer.loop)thread.start()try:...exceptExceptionaserr:# send event to internal queueprinter.event_cb(const.Event.ATTENTION,const.Source.WUI,reason=str(err))Printer statefromthreadingimportThreadfromtimeimportsleep...# start communication loopthread=Thread(target=printer.loop)thread.start()# toggle the state each secondwhileTrue:ifprinter.state==const.State.READY:printer.set_state(const.State.BUSY,const.Source.MARLIN)elifprinter.state==const.State.BUSY:printer.set_state(const.State.READY,const.Source.MARLIN)sleep(1)FilesFiles are sent to Connect in a dictionary using theSEND_INFOcommand.
Within theSEND_INFOcommmand response, there’s afilesdictionary with all
files and folders within the Filesystem. Here you can find info about file (or
folder). Available info is type, name, ro (read only), m_timestamp (when the
file was last modified), size and in case of folder, info about its children.
Also you can find here information about free_space and total_space of the each
storage, if available.CommandsWhen Connect sends a command in the answer to telemetry,Printer.commandobject will be created. Please note that thePrinter.looponly creates and parametrizes this command instance. It never
calls this command’s handler. It must happen in another (e.g. main) thread.Each command handler must return a dictionary with at least thesourcekey.Normally each command is marked as finished by the FINISHED event. You
might want to override it by some other event, e.g. INFO. In that case,
also theeventkey must be set in the returned dictionary.Additional data for this event is passed using thedatakey with
a dictionary as a value.For further detail seehttps://connect.prusa3d.com/docs/printer_communicationor have a look at the implementation details in the SDK (INFO event
handled by thePrinter.get_info()method).fromthreadingimportThreadfromtimeimportsleep...@printer.handler(const.Command.START_PRINT)defstart_print(args:List[str]):"""This handler will be called when START_PRINT command was sent to
       the printer."""printer.set_state(const.State.PRINTING,const.Source.CONNECT)print("Printing file:{args[0]}")...@printer.handler(const.Command.STOP_PRINT)defstart_print(args:List[str]):"""This handler will be called when STOP_PRINT command was sent to
       the printer."""printer.set_state(const.State.READY,const.Source.CONNECT)print("Printing stopped")...# communication loopthread=Thread(target=printer.loop)thread.start()# try run command handler each 100 mswhileTrue:printer.command()sleep(0.1)