Getting klipper to detect my CR-10 MAX reliably

I am on a journey to make my CR-10 MAX as reliable as possible. I have updated various things on it, but one thing I was struggling with was powering the printer on and having klipper work reliably with it once it was. The printer is powered down after completing jobs to save power.

The main thing with this, is not to rush all the commands one after another. Shutting down the printer causes klipper to think that some really bad has happened. This is to be expected, and means that the printer requires a FIRMWARE_RESTART to reset everything to a state of normalcy. NB, it is not generally a good thing to do a firmware reset, but in this is only being done in a specific instance.

The power of the printer, for me, is controlled via PSU Control with the home assistant plugin. Once the plug is switched on, either via home assistant or this plugin, the first thing that the happens is that the Raspberry PI running Octoprint and Klipper suddenly detects that it has a new serial port. Fortunately, we know some details about this serial port, so we can get udev to run a script on it's appearance.

My udev rule file, 98-klipper.rules (located in /etc/udev/rules.d/) contains the following:

# The following details are specific to the CR-10 Max. You may need to alter these values to suit your printer or main board.
# Using lsusb -vv is your friend for determining these values.
 
SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ACTION=="add", RUN+="/usr/bin/su pi -c '/usr/local/bin/restart.sh'"

Once edited, you need to restart the udev rules, either by restarting the PI or with the command:

sudo udevadm control --reload-rules

The file /usr/local/bin/restart.sh contains the foloowing:

#!/usr/bin/env bash

PRINTER_FILE="/tmp/printer"
CURRENT_USER=$(id -nu)
FILE_USER=$(stat -c '%U' $PRINTER_FILE)
RESTART_COMMAND="echo FIRMWARE_RESTART"

sleep 15

if [ "$CURRENT_USER" != "$FILE_USER" ]; then
    su -pc "$RESTART_COMMAND >$PRINTER_FILE"  $FILE_USER
else
    $RESTART_COMMAND  > $PRINTER_FILE
fi
sleep 5

if [ "$CURRENT_USER" != "$FILE_USER" ]; then
    su -pc "$RESTART_COMMAND >$PRINTER_FILE"  $FILE_USER
else
    $RESTART_COMMAND  > $PRINTER_FILE
fi

I initially set up the file, so that I could make changes without having to restart udev all the time. This allowed my to put in extra debugging and take it out when things didn't quite work properly. In case you are wondering, yes it does run the command twice, with a 5 second delay between them. Could I just run the command once with a 20 second delay? Maybe, but this is what I got working, and it works for me. YMMV.

Next, I needed to set up the PSU Control plugin to make sure that is handling everything properly. These settings are:

Post on delay                              : 10
Connect when powered on                    : Checked
Turn on prior to printing after API upload : Checked

Automatically turn PSU OFF when idle       : Checked
Idle Timeout                               : 10 min
Ignore Commands                            : M105
Wait For Temperature                       : 40 °C
Pre Off GCode Script                       : POWER_OFF_PRINTER
Disconnect on power off                    : Checked

Once configured, the printer should now be ready to do printing after a slight delay when it is switched on via the plugin and via the API (i.e. printing from Cura).

As an Amazon Associate I earn from qualifying purchases.

If you have found this post useful, please consider donating.