-
- Posts: 22
- Joined: Tue Jan 22, 2013 3:40 pm
Re: What's the simple and safe way to do a "shutdown" button
Pull the plug. it's just silly software.
- tonyhughes
- Posts: 951
- Joined: Wed Dec 26, 2012 3:46 am
Re: What's the simple and safe way to do a "shutdown" button
Totally unhelpful and uninformed comment.
Many people will be running processes that should not be interrupted like that, including database access, backups, and writing to critical system files.
This is not the case most of the time that a Pi is running, but these things are very common nonetheless.
Many people will be running processes that should not be interrupted like that, including database access, backups, and writing to critical system files.
This is not the case most of the time that a Pi is running, but these things are very common nonetheless.
Re: What's the simple and safe way to do a "shutdown" button
I'm sorry because I had no hard evidence to back up my statement. There is very little around these forums I see.
I'm not sorry for my suggestion that you don't need a switch, you merely need another card. That's a lot less fun though.
/Dextrus
I'm not sorry for my suggestion that you don't need a switch, you merely need another card. That's a lot less fun though.
/Dextrus
tonyhughes wrote:Why are you sorry? Do you think those of use who are doing this have wasted our time?
Our needs are different to yours.
I need to be able to move my Pi around at will - its great having a shutdown button, no SSH or commands needed to do it - I just flick the button, great.
Have more FUN with your Pi. Visit www.pi-fun.com
Re: What's the simple and safe way to do a "shutdown" button
I noticed nobody has asked, and the title says shutdown - so this is probably a stupid question. but, does this button also boot the device? or does it then have to be disconnected from power, and reconnecting it turns it on?
ideally, I'm looking for a power state toggle(or as close to it as possible, seeing as how the pi doesnt have power states)
ideally, I'm looking for a power state toggle(or as close to it as possible, seeing as how the pi doesnt have power states)
- tonyhughes
- Posts: 951
- Joined: Wed Dec 26, 2012 3:46 am
Re: What's the simple and safe way to do a "shutdown" button
No, using a simple button on GPIO is only good for monitoring for a button press from a running program.
Once the Pi is off, the button will do nothing.
Somewhere on the forums someone has released an on off switch that does exactly what you are thinking of.
Once the Pi is off, the button will do nothing.
Somewhere on the forums someone has released an on off switch that does exactly what you are thinking of.
Re: What's the simple and safe way to do a "shutdown" button
Hello! Here is a link to the power switch I've created.
http://www.raspberrypi.org/phpBB3/viewt ... 63&t=32435
I've got a new run of boards being printed now and they have shrunk the switch to almost half the size of the one in the demo video/photos.
http://www.raspberrypi.org/phpBB3/viewt ... 63&t=32435
I've got a new run of boards being printed now and they have shrunk the switch to almost half the size of the one in the demo video/photos.
Re: What's the simple and safe way to do a "shutdown" button
If you wanted to go with 2 buttons and you have a Rev 2 board (or a model A) you can use a button connected to short the two pins on P6 to bring the Pi back up as long as it's still connected to power (obviously LOL). The catch is that you'll have to solder on the P6 pins yourself. It's not hard. Someone made a video about it. (Can't remember whotonyhughes wrote:No, using a simple button on GPIO is only good for monitoring for a button press from a running program.
Once the Pi is off, the button will do nothing.
Somewhere on the forums someone has released an on off switch that does exactly what you are thinking of.


Alex Eames RasPi.TV, RasP.iO
Re: What's the simple and safe way to do a "shutdown" button
I've spent hours trying to figure out why the script wouldn't run on Raspbmc from either /etc/rc1.d/myscript or /etc/rc.local. Apparently Ubuntu is moving on from rc.local etc and are now using Upstart:
http://upstart.ubuntu.com/getting-started.html
/etc/init/button.conf
http://upstart.ubuntu.com/getting-started.html
/etc/init/button.conf
Code: Select all
start on startup
exec python /home/pi/projects/button.py
Re: What's the simple and safe way to do a "shutdown" button
I am desperately trying to get this button to work.
I just followed this post: http://www.raspberrypi.org/phpBB3/viewt ... 37#p186137
Only changes I made are:
- put the shutdown.py in /home/pi/scripts/button/
- I did change this in the /etc/rc.local
My shutdown.py looks like this:
I think something might be wrong with my breadboard-setup. I attached a photo, sorry for the bad quality..

Does anyone see any mistakes?
I just followed this post: http://www.raspberrypi.org/phpBB3/viewt ... 37#p186137
Only changes I made are:
- put the shutdown.py in /home/pi/scripts/button/
- I did change this in the /etc/rc.local
My shutdown.py looks like this:
Code: Select all
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
while True:
if(GPIO.input(17)):
os.system("sudo shutdown -h now")
break
time.sleep(1)

Does anyone see any mistakes?
Re: What's the simple and safe way to do a "shutdown" button
Ok, found the mistake myself. Connected it to pin 17 and not GPIO17 (pin 11)
Because on this site http://www.3cc.org/blog/2013/01/raspber ... ff-the-pi/ he says [...] and the 1K to pin 17 [...].
Unfortunately my RPi won't start anymore, so I have to re-flash my sd-card.
Hopefully it will work then...

Because on this site http://www.3cc.org/blog/2013/01/raspber ... ff-the-pi/ he says [...] and the 1K to pin 17 [...].
Unfortunately my RPi won't start anymore, so I have to re-flash my sd-card.
Hopefully it will work then...
-
- Posts: 96
- Joined: Fri Feb 08, 2013 7:22 pm
Re: What's the simple and safe way to do a "shutdown" button
I am having some problems getting this to work. What happens is that pin 17 always seems to be high, so the shutdown command is always executed. I have double checked my wiring and am sure it is correct. I have also tried ommiting the switch completely, so pin 17 is connected to ground via the 1k and 10K resistor and should be low. Can anyone suggest what might be wrong?
Re: What's the simple and safe way to do a "shutdown" button
@geekinthesticks, read the post immediately above yours. Does that explain it?
tl;dr: try connecting to pin 11 instead of pin 17, leave the software the same, retest.
tl;dr: try connecting to pin 11 instead of pin 17, leave the software the same, retest.
-
- Posts: 96
- Joined: Fri Feb 08, 2013 7:22 pm
Re: What's the simple and safe way to do a "shutdown" button
Thanks, I did have the correct pin. However problem now solved - dry joint on the 1k resistor, so the pin was never getting pulled low.
Re: What's the simple and safe way to do a "shutdown" button
I have found yet another method on the web to do a shutdown, but now by using the new(er) interrupt possibility for the GPIO pins, rather than polling. The new version uses no cpu time and is very responsive (no waiting for sleep cycles).
Here is the code I found, and I only added the shutdown statement.
This code should be easy to change to the so called "jumper solution" (shortening pin 2 and 3 together with a jumper) that is also used to halt the system. See this topic:
http://www.raspberrypi.org/phpBB3/viewt ... 37&t=42449
And you can use other GPIO pins. This program can be installed at boot time (eliminate the print statements or pipe them to a log file) or you can implement this in a larger Python program. I guess you have to use multi-threading or a sub-process, but I have no experience with that. If somebody knows how, I'd be interested.
Alex has another example that is interesting to experiment with too:
http://raspi.tv/2013/how-to-use-interru ... pio-part-2
Thanks Alex!
Here is the code I found, and I only added the shutdown statement.
Code: Select all
#!/usr/bin/env python2.7
# script by Alex Eames http://RasPi.tv/
# http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio
import subprocess
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
# GPIO 23 (pin 16) set up as input. It is pulled up to stop false signals
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
print "Make sure you have a button connected so that when pressed"
print "it will connect GPIO port 23 (pin 16) to GND (pin 6)\n"
raw_input("Press Enter when ready\n>")
print "Waiting for falling edge on port 23"
# now the program will do nothing until the signal on port 23
# starts to fall towards zero. This is why we used the pullup
# to keep the signal high and prevent a false interrupt
print "During this waiting time, your computer is not"
print "wasting resources by polling for a button press.\n"
print "Press your button when ready to initiate a falling edge interrupt."
try:
GPIO.wait_for_edge(23, GPIO.FALLING)
print "\nFalling edge detected."
subprocess.call(['shutdown -h now "System halted by GPIO action"'], shell=True)
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit
This code should be easy to change to the so called "jumper solution" (shortening pin 2 and 3 together with a jumper) that is also used to halt the system. See this topic:
http://www.raspberrypi.org/phpBB3/viewt ... 37&t=42449
And you can use other GPIO pins. This program can be installed at boot time (eliminate the print statements or pipe them to a log file) or you can implement this in a larger Python program. I guess you have to use multi-threading or a sub-process, but I have no experience with that. If somebody knows how, I'd be interested.
Alex has another example that is interesting to experiment with too:
http://raspi.tv/2013/how-to-use-interru ... pio-part-2
Thanks Alex!
-
- Posts: 96
- Joined: Fri Feb 08, 2013 7:22 pm
Re: What's the simple and safe way to do a "shutdown" button
That's very interesting. There are a number of Python frameworks that allow you to turn multi threaded apps into an event driven app. You don't have to worry about managing threads, but write an event handler, so when your button gets pressed it runs the code in your event handler. Possibly the best known is Twisted: http://www.python.org/workshops/2002-02/papers/09/ . However, there are several others around.
Re: What's the simple and safe way to do a "shutdown" button
In the meantime, I decided to figure out how to do a thread in Python. Needed to do that for a while anyway, so I had a good reason.
Here is my sample code:
I am a n00by with threading, so be warned, but this seems to work really well.
It took some time to figure out the t.daemon = True statement, because I could not terminate the program with Ctrl+C, only with Ctrl+Z and then sudo kill -9 process_id. (n00by, remember)
Notice that when you run the code, there is no debouncing of the button, so you may see two instances of the edge recognition. That is only because of the while loop for testing. If you take that out and un-comment the shutdown, you're good to go.
Below is a small sampleHave fun!
Here is my sample code:
Code: Select all
#!/usr/bin/env python2.7
import subprocess
import RPi.GPIO as GPIO
import time
from threading import Thread
GPIO.setmode(GPIO.BCM)
# GPIO 23 (pin 16) set up as input. It is pulled up to stop false signals
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def gpio_interrupt():
print "\tStarting thread"
while True: # so we can test multiple interrupts, otherwise take the while loop out
GPIO.wait_for_edge(23, GPIO.FALLING)
print "\n\tFalling edge detected."
# subprocess.call(['shutdown -h now "System halted by GPIO action"'], shell=True)
# start the tread
t = Thread(target=gpio_interrupt)
# if the thread is not declared as a daemon, you cannot terminate the program with Ctrl+C
# the daemon needs to terminate when the main program does
t.daemon = True
t.start()
# start the main program
print "Starting main program"
try:
while True:
print "Running in main"
time.sleep(2)
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on Ctrl+C exit
GPIO.cleanup() # clean up GPIO on normal exit (if there is one)
It took some time to figure out the t.daemon = True statement, because I could not terminate the program with Ctrl+C, only with Ctrl+Z and then sudo kill -9 process_id. (n00by, remember)
Notice that when you run the code, there is no debouncing of the button, so you may see two instances of the edge recognition. That is only because of the while loop for testing. If you take that out and un-comment the shutdown, you're good to go.
Below is a small sample
Code: Select all
Starting main program
Running in main
Starting thread
Running in main
Running in main
Running in main
Falling edge detected.
Falling edge detected.
Running in main
Re: What's the simple and safe way to do a "shutdown" button
Thank you for the hint. However, this stuff, like many other examples go way over my poor head, so I looked long and hard for a simple example that I could understand. I finally found something that I could follow and implement. See my post above:geekinthesticks wrote:That's very interesting. There are a number of Python frameworks that allow you to turn multi threaded apps into an event driven app. You don't have to worry about managing threads, but write an event handler, so when your button gets pressed it runs the code in your event handler. Possibly the best known is Twisted: http://www.python.org/workshops/2002-02/papers/09/ . However, there are several others around.
Re: What's the simple and safe way to do a "shutdown" button
Well, while was at it, I discovered yet another method to add a button press event to a python program.
Turns out that when you use the GPIO.add_event_detect function, you can not only add a separate thread, but also add a debounce value like so:
GPIO.add_event_detect(PORT, GPIO.FALLING, callback=my_callback, bouncetime=200) # in ms
The Python code is almost the same:
Turns out that when you use the GPIO.add_event_detect function, you can not only add a separate thread, but also add a debounce value like so:
GPIO.add_event_detect(PORT, GPIO.FALLING, callback=my_callback, bouncetime=200) # in ms
The Python code is almost the same:
Code: Select all
#!/usr/bin/env python2.7
# script by Alex Eames http://RasPi.tv/
# http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio
# http://code.google.com/p/raspberry-gpio-python/wiki/Inputs
from time import sleep
import subprocess
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
# GPIO 23 (pin 16) set up as input. It is pulled up to stop false signals
PORT = 23
GPIO.setup(PORT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def my_callback(PORT):
print('This is an edge event callback function!')
print('Edge detected on channel %s'%PORT)
print('This is run in a different thread to your main program')
# subprocess.call(['shutdown -h now "System halted by GPIO action"'], shell=True)
print "Make sure you have a button connected so that when pressed"
print "it will connect GPIO port 23 (pin 16) to GND (pin 6)\n"
raw_input("Press Enter when ready\n>")
print "Waiting for falling edge on port 23"
# now the program will do nothing until the signal on port 23
# starts to fall towards zero. This is why we used the pullup
# to keep the signal high and prevent a false interrupt
# add a falling edge detection on a channel
GPIO.add_event_detect(PORT, GPIO.FALLING, callback=my_callback, bouncetime=200)
print "During this waiting time, your computer is not"
print "wasting resources by polling for a button press.\n"
print "Press your button when ready to initiate a falling edge interrupt."
try:
while True:
print "in main"
sleep (2)
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit
Re: What's the simple and safe way to do a "shutdown" button
Great stuff.
Now if there's a way to navigate through XBMC this way , i would be very happy.
I've got my buttons all set-up, the python code works but:
I can't seem to import xbmc module into my python script so i'm pretty much stuck.
Also, i'm very noobish when it comes to code larger projects .
Maybe somebody here already figured it out?
Now if there's a way to navigate through XBMC this way , i would be very happy.
I've got my buttons all set-up, the python code works but:
I can't seem to import xbmc module into my python script so i'm pretty much stuck.
Also, i'm very noobish when it comes to code larger projects .
Maybe somebody here already figured it out?
Re: What's the simple and safe way to do a "shutdown" button
Take a look at gpio-keys, discussed here http://www.raspberrypi.org/phpBB3/viewt ... 2&p=147899, it may do what you want.troyk wrote:Great stuff.
Now if there's a way to navigate through XBMC this way , i would be very happy.
I've got my buttons all set-up, the python code works but:
I can't seem to import xbmc module into my python script so i'm pretty much stuck.
Also, i'm very noobish when it comes to code larger projects .
Maybe somebody here already figured it out?
Re: What's the simple and safe way to do a "shutdown" button
Has anyone found a way to use interrupts to perform a shutdown using Mike McCauley's "C" Library. I have been using polling, which works great for my headless pi, but not the most efficient method.
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/
https://www.flaminghellmet.com/launch/
Re: What's the simple and safe way to do a "shutdown" button
pjc123, look at post 40, just above your entry, where I explain how to use an interrupt.
Re: What's the simple and safe way to do a "shutdown" button
That is Python, not "C". If someone has actual working code for "C" and my library, I would be interested.paulv wrote:pjc123, look at post 40, just above your entry, where I explain how to use an interrupt.
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/
https://www.flaminghellmet.com/launch/
Re: What's the simple and safe way to do a "shutdown" button
If i were to remap his keys and do some work in the code , I think this could be what i'm looking for.PiGraham wrote:Take a look at gpio-keys, discussed here http://www.raspberrypi.org/phpBB3/viewt ... 2&p=147899, it may do what you want.troyk wrote:Great stuff.
Now if there's a way to navigate through XBMC this way , i would be very happy.
I've got my buttons all set-up, the python code works but:
I can't seem to import xbmc module into my python script so i'm pretty much stuck.
Also, i'm very noobish when it comes to code larger projects .
Maybe somebody here already figured it out?
Thanks for the find , i'll update if I got it to work.
-
- Posts: 12
- Joined: Fri Jul 26, 2013 12:01 am
Re: What's the simple and safe way to do a "shutdown" button
I think the solution you're looking for, kronflux, is a simple external circuit like this one that lets you start up the RPi with a momentary button press, do a soft shutdown with another momentary press, or force a power off with a longer press.kronflux wrote:I noticed nobody has asked, and the title says shutdown - so this is probably a stupid question. but, does this button also boot the device? or does it then have to be disconnected from power, and reconnecting it turns it on?
ideally, I'm looking for a power state toggle(or as close to it as possible, seeing as how the pi doesnt have power states)