[GUIDE] Simple Shutdown/Turn on Button

Various user-contributed guides for hardware-related things
Post Reply
User avatar
Mtuckey
Posts: 20
Joined: Tue Nov 15, 2016 4:32 am
Location: Perth, Australia
Has thanked: 6 times
Been thanked: 6 times

[GUIDE] Simple Shutdown/Turn on Button

Post by Mtuckey » Fri Dec 23, 2016 8:23 am

Updated with new script thanks to HoolyHoo

Hi guys, this is the method I am going to employ with my GBZ. I cant be bothered dealing with the hassle of using a slide switch when this method is so much simpler.

Basic premise is using a momentary push button switch instead of a slide switch to turn the pi on and off. This switch will ground GPIO3 board pin 5 to its neighbor pin 6. There will be a python script running in the background that see's this even has taken place, and then issue a shutdown command. Once shut down, depressing this switch once again will boot the pi.

I'm planning on putting a switch elsewhere to mechanically disconnect the battery or power supply for periods of non use.

For the momentary button, I have found that the long stemmed tactile switchers Wermy uses as L and R buttons actually press nicely into the bottom of the standard gameboy slide switch cap, and there is enough slack in the area to allow it to act as a push button, assuming the base of the tactile switch is mounted appropriately. So my GBZ will simply require a press on the slide switch, rather than a slide.

Button has a 2 second hold delay for shutdown, to avoid accidental shutdowns.

apt-get with the following command (this is assuming you have your pi connected to the internet):

Code: Select all

	   
sudo apt-get install update
	
sudo apt-get install python3-gpiozero

call our script shutdown.py (it is written in python). Create and edit the script by doing:

Code: Select all

sudo nano /root/shutdown.py

The content of the script: Paste it in the blank area

Code: Select all

from gpiozero import Button
from subprocess import check_call
from signal import pause

def shutdown():
    check_call(['sudo', 'poweroff'])

shutdown_btn = Button(3, hold_time=2)
shutdown_btn.when_held = shutdown

pause()
To configure our script to run at startup, it should be added to /etc/rc.local

You can do this with the following:

Code: Select all

sudo nano /etc/rc.local
Add the following to the line

Code: Select all

python /root/shutdown.py &
Last edited by Mtuckey on Tue Dec 27, 2016 8:28 am, edited 3 times in total.

User avatar
HoolyHoo
Posts: 725
Joined: Sat Jul 09, 2016 9:34 pm
Has thanked: 229 times
Been thanked: 769 times

Re: [GUIDE] Simple Shutdown/Turn on Button

Post by HoolyHoo » Fri Dec 23, 2016 11:38 am

There is a simpler code that uses GPIO Zero instead of the GPIO package.

Intall GPIO Zero with:

sudo apt-get install python3-gpiozero python-gpiozero

Use this python script:

Code: Select all

from gpiozero import Button
from subprocess import check_call
from signal import pause

def shutdown():
    check_call(['sudo', 'poweroff'])

shutdown_btn = Button(3, hold_time=2)
shutdown_btn.when_held = shutdown

pause()
Should use less resources also. ;)
Last edited by HoolyHoo on Tue Dec 27, 2016 8:22 am, edited 1 time in total.

User avatar
Mtuckey
Posts: 20
Joined: Tue Nov 15, 2016 4:32 am
Location: Perth, Australia
Has thanked: 6 times
Been thanked: 6 times

Re: [GUIDE] Simple Shutdown/Turn on Button

Post by Mtuckey » Fri Dec 23, 2016 8:23 pm

Ive tried it out HoolyHoo, doesnt seem to do anything when pin 5 is grounded. When i tried running the script it gives some errors and says it needs to be run in root. I ran it in root buy when i try to execute it nothing happens, seems to just lock up the pi and i have to reset.

User avatar
Mtuckey
Posts: 20
Joined: Tue Nov 15, 2016 4:32 am
Location: Perth, Australia
Has thanked: 6 times
Been thanked: 6 times

Re: [GUIDE] Simple Shutdown/Turn on Button

Post by Mtuckey » Tue Dec 27, 2016 8:15 am

Mtuckey wrote:Ive tried it out HoolyHoo, doesnt seem to do anything when pin 5 is grounded. When i tried running the script it gives some errors and says it needs to be run in root. I ran it in root buy when i try to execute it nothing happens, seems to just lock up the pi and i have to reset.
Found the problem, GPIOZero talks about GPIO numbers exclusively, not board numbers. So for shutdown with board pin 5, the script needs to read the following:

Code: Select all

shutdown_btn = Button(3, hold_time=2)
As board pin 5 = GPIO3

User avatar
sixteenbit
Posts: 52
Joined: Mon Aug 21, 2017 8:42 pm
Has thanked: 22 times
Been thanked: 13 times
Contact:

Re: [GUIDE] Simple Shutdown/Turn on Button

Post by sixteenbit » Thu Aug 23, 2018 12:53 pm

HoolyHoo referred me to this post to help me setup a safe shutdown on my SNESPi. I ended up writing an installer to setup this script if anyone finds it useful.

https://gitlab.com/sixteenbitsystems/pi-safe-shutdown

User avatar
sixteenbit
Posts: 52
Joined: Mon Aug 21, 2017 8:42 pm
Has thanked: 22 times
Been thanked: 13 times
Contact:

Re: [GUIDE] Simple Shutdown/Turn on Button

Post by sixteenbit » Tue Sep 11, 2018 6:37 pm

So, there's a new - really awesome case out (https://www.amazon.com/dp/B07G4YTWQN) that comes with safe shutdown/reset - but you can use their script on any pi setup: https://github.com/RetroFlag/retroflag-picase

Just wire a shutdown button to GPIO 3 and a reset button to GPIO 2 and an LED to 14 and run the following:

Code: Select all

wget -O - "https://raw.githubusercontent.com/RetroFlag/retroflag-picase/master/install.sh" | sudo bash
This is much better than the script I wrote because it saves meta data and it's an actual reset/reboot instead of a hard reset.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests