Skip to content

Instantly share code, notes, and snippets.

@Triodes
Last active April 11, 2022 13:21
Show Gist options
  • Save Triodes/b677ee413d17fe4c0b5f1879d1c5f7c4 to your computer and use it in GitHub Desktop.
Save Triodes/b677ee413d17fe4c0b5f1879d1c5f7c4 to your computer and use it in GitHub Desktop.
OctoPrint autoconnect

Udev rule and Systemd service for auto-connecting 3d printer to OctoPrint on raspberry pi

based on a tutorial by Bernd Zeimetz

Find vendor and product id

first run the following command to find the vendor and product id of your printer:

lsusb -v | grep -iE '(^bus|idvendor|idproduct)'

Example output:

Bus 001 Device 013: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
  idVendor           0x1a86 QinHeng Electronics
  idProduct          0x7523 HL-340 USB-Serial adapter

Note down the idVendor and idProduct without the 0x. In this case:

  • idVendor: 1a86
  • idProduct: 7523

If you don't know which device is your printer run the command once with the printer disconnected and again with the printer connected and turned on and compare the outputs of the command.

Create udev rule

create the file 90-3dprinter.rules in the folder /etc/udev/rules.d using the example content in this gist and replace the <idVendor> and <idProduct> placeholders with the values noted down above.

Create systemd service

create the file octoprint_connect@%k.service in the folder /etc/systemd/system using the example content in this gist. Replace <baud_rate> with the baud rate of your printer and <api_token> with a valid octoprint api token. (you can generate one in the octoprint settings)

run sudo systemctl daemon-reload

Your printer should now automatically connect to octoprint when physically connected to the Pi and turned on

KERNEL=="tty*", ATTRS{idVendor}=="<idVendor>", ATTRS{idProduct}=="<idProduct>", TAG+="systemd", ENV{SYSTEMD_WANTS}="octoprint_connect@%k.service"
[Unit]
Description=Connect printer to OctoPrint automatically
BindsTo=dev-%i.device
After=dev-%i.device
[Service]
Type=oneshot
User=pi
RemainAfterExit=yes
ExecStart=/usr/bin/curl -d '{"command": "connect", "port": "/dev/%i", "baudrate": <baud_rate>}' -H "Content-Type: application/json" -H "X-Api-Key: <api_token>" -X POST http://localhost/api/connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment