Page 1 of 3

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

Posted: Fri Sep 28, 2012 9:53 am
by piglet
Our school has a large screen in the reception area which has a rolling display of nice photos. It's currently driven by a laptop.

Getting inspiration from this thread http://www.raspberrypi.org/phpBB3/viewt ... 63&t=17797 I'm going to try replacing this with a "Poor Man's Photo Kiosk".

Set up the way described there I've got a Pi that you can plug in, switch on and will autologin, auto start x, and then run the rolling display of pictures.

Here comes the issue. This pi will be running "head-only" as opposed to "headless". ie. it will have a screen but no network, mouse or keyboard. How do I safely shut it down?

I assume there must be some way to have:

1) a push-button linked to +5v pin and a gpio pin (and a maybe pull-down resistor on the gpio to ground *says he hoping that people don't laugh at how little he knows*)
2) a background task/interrupt to then initiate "sudo shutdown now" when that pin goes to +5v


How to do 1, and 2 and have 2 running all the time is beyond me. Is anyone willing to help?

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

Posted: Fri Sep 28, 2012 10:20 am
by RaTTuS
no need to have x running to show images
why shut it down - leave it running all the time?
or
have it powered from the TV and boot the FS as readonly so it never matters it it gets switched off ?

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

Posted: Fri Sep 28, 2012 10:36 am
by brainsys
Ahem ... simply pull the plug out?

Might upset the purists but I've not had any subsequent SD corruption so far. And I do take the precaution of always backing up the image to PC so I can instantly re-image another SD card should the worst happen (or having a spare in the pocket).

KISS is the acronym so many IT people forget :roll:

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

Posted: Fri Sep 28, 2012 1:43 pm
by piglet
RaTTuS wrote:no need to have x running to show images
What's the best (light) pi-compatible cli slideshow application? qiv seems to work very well but wasn't happy without x.
brainsys wrote:Ahem ... simply pull the plug out?
I have to say I've had no problems doing that at home...
RaTTuS wrote:boot the FS as readonly so it never matters it it gets switched off ?
Not a bad idea...if I knew how to do it!



Then again, I'd quite like to know how to progress my original idea - as much for the information value as the end product.

edit: this might be what I need: http://www.cl.cam.ac.uk/~db434/raspi/bu ... _switches/

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

Posted: Fri Sep 28, 2012 3:20 pm
by tedhale
Yes, that is just what you need to do a real "shutdown button"

It is actually so simple that you don't need a breadboard, just a way to connect to two GPIO pins. Just connect a pushbutton to a GPIO pin and to ground.
Confugure the GPIO pin as input and with pull-up resistor enabled. Then it will read 1 normally and 0 when the button is pressed.

Now all you need is software to monitor that pin and do a shutdown when it sees a 0.
This could be anything from a full blown daemon written in C to a Python program or even just a bash script.

Whatever method you choose, run in the background, started from /etc/local.rc, and you should be good to go.

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

Posted: Fri Sep 28, 2012 9:41 pm
by Maarten Baert
a push-button linked to +5v pin and a gpio pin
GPIO pins are 3.3V IIRC, so whatever you decide to do, use the 3.3V rail, not the 5V one :).

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

Posted: Fri Sep 28, 2012 10:50 pm
by mahjongg
Yes putting 5V on a GPIO pin will actually put 5V on the 3V3 power supply blowing everything 3V3 powered up, not a good idea! :twisted:

In any case just put a small (220 Ohm or so) resistor in series with the switch, and things will no blow up.

My preference would be to use a 10K pullup to 3K3, on the GPIO pin, and from that same pin a switch with 220 Ohm in series to GND. The pullup will pull the pin up to 3V3, and when pressing the button it will go to near zero volts. If you accidentally program the GPIO to output a high, pressing the button doesn't damage the GPIO by shorting the high signal to ground, but will just draw a little current, not enough to do any damage.

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

Posted: Fri Sep 28, 2012 11:11 pm
by Dweeber
Note: I have little electronics background.. It is one of the reasons why I got a Raspberry Pi so I could learn.

I've been looking into doing this. this is what I was told could work:

Image

Raw code which I have not yet tried yet...

https://raw.github.com/gist/3610105/3ce ... ensor.bash

Was planning on giving this a shot when I got my bread board setup. Still waiting for part.

Script look for the button press, then issues a shutdown. Of course, the power light is still on after the shutdown.

I've noted that when the RPi shuts down in flashes what looks to be 8 blinks on the ACK led just before it is done.

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

Posted: Sat Sep 29, 2012 10:55 pm
by trazer
Great thread, I have the exact same needs with a Pi running a video display. Still waiting on my Pis to come in and I'll get started on my version of this. I'm still figuring out how to display the photos lol, but I'm sure as I read more I'll get it figured out.

I may get the GPIO cable so I can just short the correct pins together with the resistor itself as needed. Should be simple enough.

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

Posted: Mon Oct 01, 2012 9:27 pm
by piglet
Enough stuff arrived in the post today to test this out:

1) Electronic starter kit - including a breadboard and resistors needed: http://www.ebay.co.uk/itm/110887787457 (£9.99) It doesn't include any switches.
2) Female/Male Jumper leads: http://www.ebay.co.uk/itm/130761796863 (£4.40 for 40)

3) I also scrounged a momentary switch from the inside of a broken toy one of my children bought in a jumble-sale...

Jumpers attached to +3.3v, ground, and GPIO17 (as detailed on the wiki. Circuit with switch, 10K pull-down resistor and 1K current limiting resistor exactly as on the link posted previously

Image

Python script placed in /home/pi/bin/button

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)
Launch of code added to /etc/rc.local on the line before "exit 0" :

Code: Select all

python /home/pi/bin/button
Press button for 1 second to ensure it's polled, and the pi shuts down. Perfect.

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

Posted: Tue Oct 09, 2012 10:22 pm
by piglet
Hurrah!

Image

Image

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

Posted: Wed Oct 10, 2012 7:36 am
by alexeames
Well done. Looks like a tight squeeze to get it all in the case.

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

Posted: Wed Oct 10, 2012 9:01 am
by Dweeber
piglet wrote:Hurrah!
Trying to figure out what you have connected.

1 wire to the 3.3v line
1 wire with a resistor to Ground and a resistor to GPIO17.

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

Posted: Wed Oct 10, 2012 10:34 am
by alexeames
He shows it here...
http://www.raspberrypi.org/phpBB3/viewt ... 37#p186137

and gives a link to where he got the info. :D

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

Posted: Sat Jan 12, 2013 3:18 pm
by Tenocticatl
I've been looking to do something similar; I want to use gpio buttons to navigate through raspbmc. Any ideas on how to do that?

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

Posted: Sat Feb 02, 2013 2:31 am
by tonyhughes
Great thread, and good info from all, especially Piglet - thankyou.

I have bought and installed a momentary switch in my case, and purchased the 1K and 10K resistors.

Im comfortable soldering (and adding header plugs to slide onto the GPIO pins), but Im not quite 100% on what to connect to what.

Can someone spell it out a little simpler for me please, so I don't muck it up please?

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

Posted: Sat Feb 02, 2013 2:42 am
by tonyhughes
Here's a photo of my switch.

I just drilled a hole with the correct size bit in my ModMyPi case. I chose the end, so it will not accidentally have anything placed on top of it, and I can stack Pi's in cases if need be.

Image

I am not 100% sure if I have the right resistors or not - I'd appreciate if someone could let me know? (Until then, don't take this as a gospel parts list).

Switch $4.90 for one: http://jaycar.co.nz/productView.asp?ID= ... rm=KEYWORD
1K resistor 53cents for 2: http://jaycar.co.nz/productView.asp?ID= ... rm=KEYWORD
10K resistor 55 cents for 8: http://jaycar.co.nz/productView.asp?ID= ... rm=KEYWORD
(The Jaycar site is just showing stock pics, the actual items are below...)

Image

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

Posted: Sat Feb 02, 2013 2:55 pm
by piglet
Looks good to me. No just follow this: http://www.cl.cam.ac.uk/~db434/raspi/bu ... _switches/

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

Posted: Sun Feb 03, 2013 12:44 am
by tonyhughes
Thanks.

For others: my parts list above works out fine, and as a n00b, I followed instructions here:

http://www.3cc.org/blog/2013/01/raspber ... ff-the-pi/

Helped with a diagram here:

http://www.raspberrypi-spy.co.uk/2012/0 ... -and-pins/

And my only mistake was connecting to "Pin 17" instead of "GPIO17" (Which is actually Pin 11).

Works perfectly, and I have this code running, that I will add to /etc/rc.local

<removed CPU eating code lol>

My code needs work.

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

Posted: Sun Feb 03, 2013 12:55 am
by dextrus
Just have a backup image on another SD card. Yank the power on a daily basis. If you are unlucky enough to suffer corruption to the SD card data, employ the back-up SD-card and re-image the now defunct one.

When I really *try* and corrupt data cards, I can succeed almost all of the time. With an application like yours, I can't imagine a problem occurring more than monthly at most. I have no data as yet to back this up though. Sorry.

/Dextrus

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

Posted: Sun Feb 03, 2013 12:58 am
by tonyhughes
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.

:-)

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

Posted: Sun Feb 03, 2013 1:00 am
by jozien17
piglet wrote:Our school has a large screen in the reception area which has a rolling display of nice photos. It's currently driven by a laptop.

Getting inspiration from this thread http://www.raspberrypi.org/phpBB3/viewt ... 63&t=17797 I'm going to try replacing this with a "Poor Man's Photo Kiosk".

Set up the way described there I've got a Pi that you can plug in, switch on and will autologin, auto start x, and then run the rolling display of pictures.

Here comes the issue. This pi will be running "head-only" as opposed to "headless". ie. it will have a screen but no network, mouse or keyboard. How do I safely shut it down?

as root, just '# halt".

That shuts down the monitor and halts the rpi.

jozien17

I assume there must be some way to have:

1) a push-button linked to +5v pin and a gpio pin (and a maybe pull-down resistor on the gpio to ground *says he hoping that people don't laugh at how little he knows*)
2) a background task/interrupt to then initiate "sudo shutdown now" when that pin goes to +5v


How to do 1, and 2 and have 2 running all the time is beyond me. Is anyone willing to help?

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

Posted: Sun Feb 03, 2013 1:05 am
by tonyhughes
Heres my Python code. Borrowed from here there and everywhere, as I have never written/used Python before today.

Saved as /usr/bin/run.py
Called from /etc/rc.local with the line "python /usr/bin/run.py"

Code: Select all

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
prev_input = 0
while True:
  input = GPIO.input(17)
  if ((not prev_input) and input):
    os.system("sudo shutdown -h now")
  prev_input = input
  time.sleep(0.05)
Tested and working perfectly. Only catches one button press (so only runs your os.system command once, so you could happily run another script (BASH or whatever) from there).

Its also very instant. Catches the shortest button press I can manage! So I know its worked.

Negligible amounts of mem and CPU used...

top - 14:04:01 up 4 min, 1 user, load average: 0.06, 0.25, 0.13
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2810 root 20 0 6584 3600 2060 S 0.0 0.7 0:00.71 python

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

Posted: Sun Feb 03, 2013 1:07 am
by tonyhughes
jozien17 wrote:
piglet wrote:Our school has a large screen in the reception area which has a rolling display of nice photos. It's currently driven by a laptop.

Getting inspiration from this thread http://www.raspberrypi.org/phpBB3/viewt ... 63&t=17797 I'm going to try replacing this with a "Poor Man's Photo Kiosk".

Set up the way described there I've got a Pi that you can plug in, switch on and will autologin, auto start x, and then run the rolling display of pictures.

Here comes the issue. This pi will be running "head-only" as opposed to "headless". ie. it will have a screen but no network, mouse or keyboard. How do I safely shut it down?

as root, just '# halt".

That shuts down the monitor and halts the rpi.

jozien17

I assume there must be some way to have:

1) a push-button linked to +5v pin and a gpio pin (and a maybe pull-down resistor on the gpio to ground *says he hoping that people don't laugh at how little he knows*)
2) a background task/interrupt to then initiate "sudo shutdown now" when that pin goes to +5v


How to do 1, and 2 and have 2 running all the time is beyond me. Is anyone willing to help?
Your reply was entirely formatted as a quote. Hard to figure what, if any, is anything you have added.

What are you trying to ask?

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

Posted: Sun Feb 03, 2013 1:50 am
by tonyhughes
Just thought I would mention that basically any momentary switch would do fine. I bought a $5 one, because it looks nice, as if it was stock. Most electronics parts stores should have switches from as low 30 - 80 cents. You just need to pay some thought to how you will mount it nicely.

If anyone wants some clear & unambiguous photos of my wiring setup for this switch, let me know.

To connect to the GPIO pins, I scavenged header plugs from a internal 3.5" bay memory card reader out of an old desktop PC.