NewAtlantis
Posts: 22
Joined: Tue Jan 22, 2013 3:40 pm

Re: What's the simple and safe way to do a "shutdown" button

Sun Feb 03, 2013 2:18 am

Pull the plug. it's just silly software.

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: What's the simple and safe way to do a "shutdown" button

Sun Feb 03, 2013 2:21 am

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.

dextrus
Posts: 119
Joined: Tue Jan 24, 2012 10:10 pm
Location: Eastleigh, Hampshire
Contact: Website

Re: What's the simple and safe way to do a "shutdown" button

Tue Feb 05, 2013 12:18 am

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
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

kronflux
Posts: 6
Joined: Wed Dec 26, 2012 9:16 am

Re: What's the simple and safe way to do a "shutdown" button

Fri Feb 08, 2013 11:31 pm

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)

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: What's the simple and safe way to do a "shutdown" button

Sat Feb 09, 2013 12:04 am

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.

mausberry
Posts: 13
Joined: Mon Feb 04, 2013 11:30 pm

Re: What's the simple and safe way to do a "shutdown" button

Sat Feb 09, 2013 3:35 pm

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.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2808
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: What's the simple and safe way to do a "shutdown" button

Sat Feb 09, 2013 5:39 pm

tonyhughes 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.
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 who ;) :roll: )
Alex Eames RasPi.TV, RasP.iO

nalbion
Posts: 3
Joined: Sat Feb 09, 2013 6:43 am

Re: What's the simple and safe way to do a "shutdown" button

Sat Apr 06, 2013 8:21 am

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

Code: Select all

start on startup
exec python /home/pi/projects/button.py

pifab
Posts: 56
Joined: Wed Mar 27, 2013 7:44 pm

Re: What's the simple and safe way to do a "shutdown" button

Wed May 15, 2013 5:28 pm

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:

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)
I think something might be wrong with my breadboard-setup. I attached a photo, sorry for the bad quality..

Image

Does anyone see any mistakes?

pifab
Posts: 56
Joined: Wed Mar 27, 2013 7:44 pm

Re: What's the simple and safe way to do a "shutdown" button

Wed May 15, 2013 5:56 pm

Ok, found the mistake myself. Connected it to pin 17 and not GPIO17 (pin 11) :roll:
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...

geekinthesticks
Posts: 93
Joined: Fri Feb 08, 2013 7:22 pm

Re: What's the simple and safe way to do a "shutdown" button

Fri May 17, 2013 9:14 am

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?

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4205
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: What's the simple and safe way to do a "shutdown" button

Fri May 17, 2013 9:35 am

@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.

geekinthesticks
Posts: 93
Joined: Fri Feb 08, 2013 7:22 pm

Re: What's the simple and safe way to do a "shutdown" button

Fri May 17, 2013 11:02 am

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.

paulv
Posts: 523
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: What's the simple and safe way to do a "shutdown" button

Fri May 24, 2013 9:52 am

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.

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!

geekinthesticks
Posts: 93
Joined: Fri Feb 08, 2013 7:22 pm

Re: What's the simple and safe way to do a "shutdown" button

Fri May 24, 2013 10:08 am

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.

paulv
Posts: 523
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: What's the simple and safe way to do a "shutdown" button

Fri May 24, 2013 1:01 pm

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:

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)

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 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
Have fun!

paulv
Posts: 523
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: What's the simple and safe way to do a "shutdown" button

Fri May 24, 2013 1:05 pm

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.
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:

paulv
Posts: 523
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: What's the simple and safe way to do a "shutdown" button

Sun May 26, 2013 2:04 pm

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:

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

troyk
Posts: 13
Joined: Thu May 02, 2013 8:42 am

Re: What's the simple and safe way to do a "shutdown" button

Mon Aug 05, 2013 9:29 am

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?

PiGraham
Posts: 2667
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: What's the simple and safe way to do a "shutdown" button

Mon Aug 05, 2013 10:22 am

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?
Take a look at gpio-keys, discussed here http://www.raspberrypi.org/phpBB3/viewt ... 2&p=147899, it may do what you want.

pjc123
Posts: 911
Joined: Thu Mar 29, 2012 3:37 pm

Re: What's the simple and safe way to do a "shutdown" button

Mon Aug 05, 2013 10:58 am

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:
http://www.flaminghellmet.com/launch/

paulv
Posts: 523
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: What's the simple and safe way to do a "shutdown" button

Mon Aug 05, 2013 11:05 am

pjc123, look at post 40, just above your entry, where I explain how to use an interrupt.

pjc123
Posts: 911
Joined: Thu Mar 29, 2012 3:37 pm

Re: What's the simple and safe way to do a "shutdown" button

Mon Aug 05, 2013 11:25 am

paulv wrote:pjc123, look at post 40, just above your entry, where I explain how to use an interrupt.
That is Python, not "C". If someone has actual working code for "C" and my library, I would be interested.
My Raspberry Pi Project Page:
http://www.flaminghellmet.com/launch/

troyk
Posts: 13
Joined: Thu May 02, 2013 8:42 am

Re: What's the simple and safe way to do a "shutdown" button

Mon Aug 05, 2013 12:10 pm

PiGraham wrote:
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?
Take a look at gpio-keys, discussed here http://www.raspberrypi.org/phpBB3/viewt ... 2&p=147899, it may do what you want.
If i were to remap his keys and do some work in the code , I think this could be what i'm looking for.
Thanks for the find , i'll update if I got it to work.

RaspberryPaul
Posts: 12
Joined: Fri Jul 26, 2013 12:01 am

Re: What's the simple and safe way to do a "shutdown" button

Tue Aug 06, 2013 1:35 am

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)
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.

Return to “General discussion”

Who is online

Users browsing this forum: No registered users and 43 guests