Connecting Automatically to my Printer


#1

Greetings everyone,

I had to join the community because I've been struggling with this small project to allow my octopi to connect automatically to my printer without having to access to the web portal and manually press connect. There's not a lot of people who have thought about this so it was hard for me to find something, however, after looking carefully I found this website. The problem is that I don't understand most of it to be honest.

I have already created a new file in my /home/pi directory and implemented the two tweaks found on this website but the problem is that I don't understand how to find the port, since I don't even know if my port is actually 5000 and I don't know if I should delete everything on the API in connect_octoprint.py or keep the
AAABBB000_____000BBBAAA.

I have tried pretty much everything but I think all the other scripts are good to go. The problem must be with the connect_octoprint.py script.

This is the information of my printer which is an Monoprice Maker Select Plus and I have only used the 1a86 and 7523 for the scripts.

idVendor 0x1a86 QinHeng Electronics
idProduct 0x7523 HL-340 USB-Serial adapter

Thanks for your help!

P.S. Found this too but I don't understand it either. Looks like another way to achieve this.


#2

There are some checkboxes in the Connection side panel of OctoPrint. Set things up, click the Save connection settings and Auto-connect on server startup, finally click Connect.

55%20PM


#3

Thank you but this doesn't help, I always keep my OctoPrint server running but I turn off the 3D printer. I'm trying to avoid doing that by using a script that will connect the printer automatically without having to use that web interface.


#4

Sorry, that wasn't clear from your first post.

  1. You leave your Raspberry Pi on at all times
  2. When you're finished printing, you turn off the printer itself; OctoPrint loses the connection as a result
  3. You turn on the printer the next day and OctoPrint doesn't automatically reconnect
  4. You don't want to visit the web interface in order to push the Connect button
  5. Correct me if I'm being thick but what good is reconnecting the printer if you don't visit the web interface to select a file and to start a job? Do you control it via a phone app instead or something? I'm not understanding your need here.

It goes without saying that there's nothing easy that the printer can do to poke the Raspberry over the serial connection to say "hey, wake up" since the serial connection is of course down.

You could of course power both Raspberry and printer from the same TP-Link outlet as I do. It's convenient.

Assuming that you'd still like to create something, it would be nice to know what would be running this script. Is this you pushing a button on your workstation Desktop... or is this a script which is supposed to fire off automatically when something magically knows that the printer is on again?

Reading through that first link you provided, it looks like the following:

  • As the pi user, you create a Python script called /home/pi/connect_octoprint.py which must be modified to include your own printer's API key.
  • As the root user, you create a text file called /etc/systemd/system/octoprint_connect@.service
  • You run sudo systemctl daemon-reload
  • As the root user, you create a text file called /etc/udev/rules.d/3dprinter.rules, modifying it with your own printer's ID information
  • Reboot with sudo reboot

You'd then try to cycle the printer OFF, wait, then back ON. Presumably the rule as created can detect the state change on the collection of serial devices in such a way that the script is run. But in my mind, this is like Schrödinger's cat. You have to go into OctoPrint's web interface to confirm that it's connected but now you've "opened the box". (Technically, you could use a mobile phone app to query the REST API.)


#5

Well I'm not using OctoPrint as my slicer, instead I'm using the beta of Cura 4.0 which I modified in order for the OctoPrint plugin to work, since it isn't currently supported. I really don't have to open the web interface and I can even look at the webcam footage from the Cura app.

I know at first it might sound weird and yes I have to open the web interface in order to make sure that the printer auto connects, but that should only be done for the initial setup, once I know that the script is up and running I would be comfortable enough to not have to open the web interface as much as I currently do.

And yes it looks like you understand as much as I do. That's exactly what I've done, however I suck at understanding ports, so I don't even know if my port is 5000 or something else and when it comes to the API key I don't know if I should delete everything or the part where it says YOURAPIKEYHERE only.


#6
  • In the string after the equals sign to the right of API_KEY, delete AAABBB000YOURAPIKEYHERE000BBBAAA and put your own key.
  • The part port = sys.argv[1] means "accept the port as a command line argument to the script". So later, when you call this script, it gets something as an argument. This then will be used for telling OctoPrint what to do. Note that the author has also hard-coded a different port as information into that URL in the script.
  • Note that in a later script, you see this line ExecStart=/home/pi/connect_octoprint.py /dev/%I. As it turns out, that entire /dev/something is the command line argument to that script earlier. Maybe it's /dev/ttyAMA0 if it's like my own printer. So that then becomes the port variable seen in the earlier script. In other words, one of these port variables is really a device and one of them is a tcp port. The author probably should have made this known to you.
#!/home/pi/OctoPrint/venv/bin/python

OCTOPRINT_URL = 'http://localhost:5000/api/connection'
API_KEY = 'AAABBB000YOURAPIKEYHERE000BBBAAA'
BAUDRATE = 115200


import requests
import sys

port = sys.argv[1]
headers = {'X-Api-Key': API_KEY}
json = {
  "command": "connect",
  "port": port,
  "baudrate": BAUDRATE,
}

r = requests.post(
        OCTOPRINT_URL,
        json=json,
        headers=headers
)

if (r.status_code == 204):
    sys.exit(0)
else:
    print(r)
    sys.exit(1)

#7

Check out the PortLister plugin. It will allow you to automatically reconnect once a serial port becomes available. May take a couple of extra seconds.


#8

Maybe I'm just horrible at this, but this was my attempt:

#!/home/pi/oprint/bin/python

OCTOPRINT_URL = 'http://192.168.0.179/api/connection'
API_KEY = 'REDACTED'
BAUDRATE = 115200

import requests
import sys

port = sys.argv[1]
headers = {'X-Api-Key': API_KEY}
json = {
"command": "connect",
"port": port,
"baudrate": BAUDRATE,
}

r = requests.post(
OCTOPRINT_URL,
json=json,
headers=headers
)

if (r.status_code == 204):
sys.exit(0)
else:
print(r)
sys.exit(1)


[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/python /home/pi/connect_octoprint.py /dev/%I


KERNEL=="tty*", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523",
TAG+="systemd", ENV{SYSTEMD_WANTS}="octoprint_connect@%k.service"

Just so you know the 5V cable was cut because my printer's LCD was always on! Also I tried changing the exec command you said and replacing it by ttyUSB0 but didn't work. And I used the localhost:5000 but then tried the IP to see what would happen, could it be the port or something? I feel like I'm trying to achieve something way out of my league right now.


#9

Fyi, the second beta of Cura 4 is available, and there’s now a version of the OctoPrint Connection plugin available from the Marketplace. No modification necessary.


#10

I just reinstalled Cura and deleted all my modified files for the OctoPrint connection. Are you the author of the plugin? What about the script I uploaded?


#11

I am indeed the author of that plugin. To keep this thread more on-topic, if you have questions about that plugin you could use this thread: Cura OctoPrint Connection plugin

I did not really look in to your script.

Have you seen the PortLister plugin? It doesn't just refresh the list of serial ports, it also lets you (re)connect to a port when it becomes available.


#12

I'm not sure what you're trying to say here. Did you open up the serial cable for your printer and cut the 5V wire?

It's entirely possible that none of this will work unless that 5V line is still there.