Automatic Shutdown - shutdown command help

Hi, I'm using 1.3.7 (stable) with the Automatic Shutdown plugin installed (amongst others) on OctoPi. I have my Pi set up with a GPIO relay, so that the Automatic Shutdown actually powers down the printer itself, rather than just the Pi, when the print finishes.

At the moment, I have changed the OctoPrint Settings -> Server -> Shutdown System command to sudo printer_off.sh and created a bash script that does it for me, which works. However, instead, I would like to enter a native OctoPrint command in the Shutdown System field to first disconnect the printer, then trigger the two GPIO ports - which flip the relays cutting the live & neutral connections. I can actually get this to work from a menu item in config.yaml - so I know that the command works, but I can't get it to work here, in the Shutdown System field

disconnect; gpio -g write 17 1; gpio -g write 18 1

What do I need to do to get it to run from the Shutdown System field - or is it impossible & I should just stick with my working bash script? Thanks.

1 Like

Stick with your working bash script. Those commands there are (at least currently) supposed to be one command to call, not multiple chained ones, so a bash script doing everything you want is the right approach.

1 Like

Thanks, will do, much appreciated.

I would like to understand this better, since I want to do the same thing. I am a newbe to Octoprint and Raspbian, but am very familiar with hardware and other OSs.

Several questions:

  1. I understand that you have created a script (let's call it pwrdn.sh)
    it contains the following commands: disconnect;gpio -g x write 1; shutdown -h now where x is the Pi port you want to set high.
  2. You put the command pwrdn.sh in the Shutdown system field (in config.yaml)
    When Octopi shuts down, it executes the bash script pwrdn.sh, which toggles the gpio pin x high, and shuts down the Pi.
  3. The bash script pwrdn.sh is located in the same directory as Octoprint

Is this correct? If not, how do I make this work? I will be using a timer with a self-latching relay on the printer power supply (that also powers the Pi) to shut everything down.

Thanks in advance.

Steve

Sounds to me like you've got everything pretty much correct, if you haven't already I would recommend reading the documentation about system commands.

Doesn't have to be, I would recommend passing the full path anyway to minimise problems say if you started OctoPrint from a different place, it would not break.

Steve,

my way of doing things has moved on quite a bit since I posted this. However, my first bit of advice is to only power down the 3D Printer, not both the Raspberry Pi & Printer together. The Pi will happily run forever & uses virtually no power, & it is far more useful when it is running. I just use my relays to cut the mains power to my 3D Printer PSU (2 relays, set to trigger simultaneously, one for the Live & the other for the Neutral). My Pi stays turned on, & I have some additional menu items in Octoprint so I can turn the printer on or off, and the Shutdown System field in my settings just points at my shitdown bash script. I'll give a quite detailed answer, as there's bound to be loads of people reading this who are only just starting to play around with relays, & who need friendly explanations.

I also had some interesting times playing with cheap relays bought online. I discovered that if you buy ones with two different sets of jumpers, it made life much easier.

Block of three jumpers
JD-VCC | VCC | GND

Block of three (or more) Interface Jumpers (depending on how many relays the board has)

GND | IN1 | VCC  (if the board only has one relay)
  or   
GND | IN1 | IN2 | INx... | VCC (if the board has 2 or more relays)

The relay comes with a Jumper fitted, jumping JD-VCC to VCC on the Block of three jumpers. UNJUMP this to separate the power supply for the trigger circuits and the relay circuits. The relays are driven through the opto-isolators, so that the actual current required to trigger each relay is <2mA - which means that they are safe to connect to any GPIO pins on the Pi. We are connecting VCC on the trigger circuits to the +3V3 GPIO pin on the Pi, as this is enough to power the opto-isolators. Meanwhile, the relays will only work on +5V, so they are powered from the +5V GPIO pin on the Pi instead. This has worked flawlessly for a couple of years for me, so it's well tested.

Each relay has an associated LED indicator which shows its state:

OFF = relay coil unenergised
ON = relay coil energised

The layout for the connectors to each relay is a common terminal in the middle, with a normally open terminal and a normally closed terminal on either side, all with Optocoupler isolation. I have my mains cables wired in so everything defaults to disconnected, on safety grounds.

It has been said that GPIO 8 is very noisy on boot, which can flip the relays, so avoid using it. I havn't verified this as true, but I haven't disproved it either, so YMMV - but that's why I don't use GPIO 8.

Wiring

Block of three pins

JD-VCC	Raspberry Pi +5V		Provides 5v power to drive the relays
VCC	 Leave disconnected		
GND	 Raspberry Pi 0V or GND		Provides the ground connection

Block of Interface pins

GND	Raspberry Pi 0V or GND		  Provides the ground connection
IN1	A suitable GPIO output pin	  Switches relay 1 when GPIO goes Low
INx	A suitable GPIO output pin	  Switches relay 2, or 3, or 4 etc, when GPIO goes Low
VCC	Raspberry Pi +3V3		      Provides +3V3 power for the opto-isolators

################################

 /usr/local/bin/printer_on.sh

#!/bin/bash
gpio export 17 out
gpio export 18 out
gpio -g write 17 0
gpio -g write 18 0

################################

 /usr/local/bin/printer_off.sh

#!/bin/bash
gpio export 17 out
gpio export 18 out
gpio -g write 17 1
gpio -g write 18 1

################################

/home/pi/.octoprint/config.yaml

excerpt which adds my menu items.

I haven't used full paths because my scripts are in /usr/local/bin, & thus on the path, but YMMV.
Interestingly, GPIO commands don't require SUDO, which makes things simpler.

system:
  actions:
  - action: pon
    command: printer_on.sh
    name: PrinterOn
  - action: poff
    command: printer_off.sh
    confirm: Are you sure you want to turn off the printer?
    name: PrinterOff

(edited to fix my markdown)

1 Like

You might want to block quote your config.yaml in the above post as the markdown formatting messed it up.

I want to shutdown both the printer and the PI. I run the PI off the 24V printer supply (with a buck converter) because I don't like to leave anything running on the printer. I am using gpio pin 27 to trigger a timer to shut down the power supply 15 seconds after the script runs. I am using a ULN2003 off the gpio pins to close relays to do things. I can trigger gpio 17 and after 15 seconds, the power supply shuts down. The 15 seconds should be enough time to allow raspbian to fully shut down before I pull power. Everything works fine, but I having trouble with the shutdown script. Here is the script
Script: sdown.sh
disconnect
gpio -g mode 27 out
gpio -h write 27 1
sudo shutdown -h now

I tried putting the sdown.sh command in the server config, and it didn't work. I tried putting it in u/sr/bin/ and manually executing it, but I got the message "command not found".

I am a newbe to Linux and have very little clue as to where things should go. I am using samba to read and write files to the Pi, but samba won't let me write into the /usr/bin directory so I used nano. Any help with this would be very much appreciated.

Thanks in advance.

I just want to add that my RPi is on separate power because I DON'T want to shut it down with the printer. The RPi draws so little power that leaving it on doesn't cost very much and having it around even when I'm not printing has proven to be very useful.

The Pi I am using for Octoprint is a old Pi 2B+, not a Pi 3B+, therefore, it is preferable to shut it down with the printer. I'm not a fan of leaving electronics running except where they are really needed. I have had wall wart power supplies catch fire and burn, I have a Pi 3B+ that I use for miscellaneous things that is NOT on a wall wart power supply. The Pi that I am using for Octoprint has a prototype hat on it that contains a buck voltage converter, a ULN2003, a couple of tiny 24V relays, and a timer module. It works great, and it is connected to the printer power supply. It has two big LED panels on the front to illuminate the printer when using the camera. It also has a camera and a custom 3D printed case for the whole thing. The printer is a Prusa I3MK3/S with a really large Meanwell 25A power supply.

In any case, a Pi 3B+ or Pi 4 draws in excess of 20W, if you include the inefficiencies of the power supply, it's more.

If someone is using a Pi for other things beside Octoprint, great. I would rather have dedicated processors then putting a lot of stuff on a single processor. Especially running Python, which is a CPU hog. I prefer C or C++, but Python has a lot of good libraries.

Of course, I am an old guy who has been in the IT and electronics business (MSEE) nearly since the beginning, and have some definite experience in these matters.

All that aside, I would appreciate it if someone could answer my question. I am really a Linux newbe, but I am very experienced with other OSs, including Windows.

I can see the point of shutting the Pi down, and understand your reasons, but for the sake of correctness I have to point out this is false - the max a Pi 4 is rated for is 5V 3A, which makes max 15W. Add in to this that when it is sitting idle, it consumes <1A, so <5W. Nowhere near 20W, let alone above it. Pi 3B+ is even less, @ 5V 2.4A, so max 12W.

In answer to your question, you need to pass the full path to where the script is.
If it is in your home directory, the shutdown system should then be:
/home/pi/sdown.sh. You also need to make sure it is executable and has a shebang at the top, to tell the system what to run it using. So something like this:

#!/bin/bash
disconnect
gpio -g mode 27 out
gpio -h write 27 1
sudo shutdown -h now

Then to make it executable, run chmod +x /home/pi/sdown.sh. Check all of these things, and let me know if you have more problems with it.

Still problems. The script is:
#! /bin/bash
disconnect
gpio -g mode 27 out
gpio -g write 27 1
sudo shutdown -h now

I put is in the /home/pi directory
When I try to execute it using ssh, I get the following:

pi@octopi:~ ls mjpg-streamer OctoPrint oprint oprint.bak scripts sdown.sh upgrade.py pi@octopi:~ /home/pi/sdown.sh
-bash: /home/pi/sdown.sh: /bin/bash^M: bad interpreter: No such file or directory

Help!

BTW, my Pi 3 was drawing 20 W with a keyboard, mouse, and network plugged in, and with a monitor hooked up.

Remove the space, it should all be one thing:
#!/bin/bash

My comment was originally like this, but corrected just after. Maybe you got the unedited version

Changed script to:
#!/bin/bash
disconnect
gpio -g mode 27 out
gpio -g write 27 1
sudo shutdown -h now

Same error:
pi@octopi:~ /home/pi/sdown.sh -bash: /home/pi/sdown.sh: /bin/bash^M: bad interpreter: No such file or directory ls But it's there: pi@octopi:~ ls
mjpg-streamer OctoPrint oprint oprint.bak scripts sdown.sh upgrade.py

I noticed the line was cut off
No such file or directory

If you enclose the command text and response using the </> menu item (or ```), it would be a lot easier to read and understand.

Please be more specific.

  1. Highlight/mark the text from a log or code
  2. Click </> in the menu bar of the editor

I hate to be such a doofus, but what editor are you referring to? I am doing editing of the script on Windows (using notepad++) and loading to the PI using Samba. For doing the batch testing, I am using PuTTy and using Windows colors (they're easier to read).

I thought I it was fairly clear what I was doing, but I guess not. How do I make it clearer?

The text box in the forum. If you look back at the text from your post, you'll notice the formatting is messed up due to the way markdown formatting works.