Best practice for disconnecting the printer before using GPIO power relay


#1

I have a standard GPIO triggered relay which switches the mains power to my printer on or off. I have two bash scripts in /usr/local/bin that I call to run things. I've set it up so that it gracefully disconnects from the printer before it pulls the power, as that keeps OctoPrint happy.

printer_on.sh

#!/bin/bash

gpio export 17 out
gpio export 18 out
gpio -g write 17 0
gpio -g write 18 0

printer_off.sh

#!/bin/bash

wget -q --header='Content-Type: application/json' --header='X-Api-Key: ABC123ABC123ABC123ABC123ABC123AB' --post-data='{ "command":"disconnect" }' --timeout=10 --tries=1 "http://127.0.0.1/api/connection"

sleep 3

gpio export 17 out
gpio export 18 out
gpio -g write 17 1
gpio -g write 18 1

& my config.yaml has this section added

system:
  actions:
  - action: pon
    command: sudo printer_on.sh
    name: Printer Power On
  - action: poff
    command: sudo printer_off.sh
    confirm: Are you sure you want to turn off the printer?
    name: Printer Power Off

I use it either via menu items under the main Power menu, or via the Automatic Shutdown plugin, as I have changed OctoPrint Settings -> OctoPrint / Server -> Commands -> Shutdown System to sudo printer_off.sh as that means that my printer automatically powers down after it finishes the print.

This all works well on my OctoPi, in both 1.3.6 (stable) & 1.3.7 (stable). However, calling wget in a bash script from OctoPrint to disconnect the printer from OctoPrint, before the GPIO triggers the relay & cuts the power, seems like a really, really, dirty hack.

I know that I can easily make both the disconnect and the GPIO calls from my config.yaml, by changing it to look like this:

system:
  actions:
  - action: pon
    command: gpio -g write 17 0; gpio -g write 18 0
    name: Printer Power On
  - action: poff
    command: disconnect; gpio -g write 17 1; gpio -g write 18 1
    confirm: Are you sure you want to turn off the printer?
    name: Printer Power Off

which works beautifully, but I just can't find out how to disconnect the printer in the OctoPrint Settings -> OctoPrint / Server -> Commands -> Shutdown System settings field.

If the Shutdown System settings field points it to my hacky old bash script with sudo printer_off.sh it works exactly as it's supposed to, but if I change it to disconnect; gpio -g write 17 1; gpio -g write 18 1 it does nothing at all.

What am I missing, & what is the best practice way of doing what I'm trying to do. Thanks.


#2

OK, it turns out that the best solution is to stick with my bash script - as advised by @foosel on a different topic in another thread.


#3

Hi,
can you help me?
I also have a shutdown script (shut.sh) under /usr/local/bin/
If I run the script manually, everything works.

I changed OctoPrint Settings: Server / Commands / Shutdown System to "sudo shut.sh"

The script contains:
#! / Bin / bash
~ / raspberry-remote / send 10100 2 0 && ~ / raspberry-remote / send 10100 3 0

In my log I find the following:

2018-11-20 20:43:01,428 - octoprint.util.comm - INFO - Finished in 46.934 s.
2018-11-20 20:43:01,430 - octoprint.util.comm - INFO - Changing monitoring state from "Printing" to "Finishing"
2018-11-20 20:43:01,450 - octoprint.plugins.automaticshutdown - INFO - Starting abort shutdown timer.
2018-11-20 20:43:01,471 - octoprint.util.comm - INFO - Changing monitoring state from "Finishing" to "Operational"
2018-11-20 20:43:06,944 - octoprint.plugin - ERROR - Error while calling plugin tracking
Traceback (most recent call last):
File "/home/pi/oprint/local/lib/python2.7/site-packages/octoprint/plugin/init.py", line 226, in call_plugin
result = getattr(plugin, method)(*args, **kwargs)
File "/home/pi/oprint/lib/python2.7/site-packages/octoprint/plugins/tracking/init.py", line 119, in on_event
self.track_printjob_event(event, payload)
File "/home/pi/oprint/lib/python2.7/site-packages/octoprint/plugins/tracking/init.py", line 237, in track_printjob_event
sha.update(self.settings.get([b"unique_id"]))
TypeError: must be string or buffer, not None
2018-11-20 20:43:11,473 - octoprint.plugins.automaticshutdown - INFO - Shutting down system with command: sudo shut.sh
2018-11-20 20:43:11,479 - octoprint.plugins.automaticshutdown - ERROR - Error when shutting down: init() got an unexpected keyword argument 'async'
Traceback (most recent call last):
File "/home/pi/oprint/local/lib/python2.7/site-packages/octoprint_automaticshutdown/init.py", line 160, in shutdown_system
p = sarge.run(shutdown_command, async=True)
File "/home/pi/oprint/local/lib/python2.7/site-packages/sarge/init.py", line 1462, in run
p.run(input=input, async
=async
)
File "/home/pi/oprint/local/lib/python2.7/site-packages/sarge/init.py", line 1071, in run
self.run_node(node, input=input, async
=False)
File "/home/pi/oprint/local/lib/python2.7/site-packages/sarge/init.py", line 1187, in run_node
result = getattr(self, method)(node, input, async
)
File "/home/pi/oprint/local/lib/python2.7/site-packages/sarge/init.py", line 1333, in run_command_node
node.cmd.run(input=input, async_=async_)
File "/home/pi/oprint/local/lib/python2.7/site-packages/sarge/init.py", line 655, in run
self.process = p = Popen(self.args, **self.kwargs)
TypeError: init() got an unexpected keyword argument 'async'


#4

Use a full path, not ~/ and see if that helps.