User avatar
LinuxHippy2002
Posts: 107
Joined: Sun Jul 13, 2014 12:07 am
Location: Philadelphia, PA-USA

Solderless clean shutdown/boot switch

Sat Apr 04, 2015 2:24 am

A couple days ago I got a Raspberry Pi A+ after loving my 2 Raspberry Pi models B. I've been using raspbian and that's what the included scripts are for. The one thing that has bothered me about these pi computers is that they don't come with a power button for a clean shutdown. I searched around the net and found that there are 2 holes labeled "RUN" on the pi A+ and B+ where pins can be soldered and then a power switch fitted onto these new pins. That was news to me but unfortunately I cannot solder but did pay for 40 soldered pins :)

So I came across some more web pages that described fitting a power switch onto a pair of existing GPIO pins. The soldering was already done but a shutdown script would need to be linked to the GPIO pins. Just what I was looking for! This isn't new to a lot of people, but it may help some.

I bought a nice power switch with a mount so that it could be attached to a case when I get one (mine is in the mail). Here's some pictures of my setup:
far.JPG
far.JPG (57.08 KiB) Viewed 21604 times
near.JPG
near.JPG (56.78 KiB) Viewed 21604 times
The pins I used are GPIO3 or pins 5 & 6. This python script makes everything work and should be saved as shutdown.py:

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import subprocess

GPIO.setmode(GPIO.BOARD)
# set pin 5 to input, and enable the internal pull-up resistor
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)

oldButtonState1 = True
while True:
    buttonState1 = GPIO.input(5)

    if buttonState1 != oldButtonState1 and buttonState1 == False :
        # print "Button 1 pressed"
        subprocess.call("init 0", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    oldButtonState1 = buttonState1

    time.sleep(.1)
Then make a bash script in the same directory and call this shutdown.sh:

Code: Select all

#!/bin/sh
cd /
cd home/pi
sudo python shutdown.py
cd /
Next, create a directory for crontab logs:

Code: Select all

mkdir shutdown-logs
Finally, add an entry to the root crontab to start the bash script at boot with sudo crontab -e

Code: Select all

@reboot sh /home/pi/shutdown.sh >/home/pi/shutdown-logs/cronlog 2>&1
Reboot and you will see a new file in your shutdown-logs directory called cronlog that will contain this to show (I think) that it's watching pin 5

Code: Select all

shutdown.py:8: RuntimeWarning: A physical pull up resistor is fitted on this channel!
  GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
Now the fun begins. Press your new button and your pi will do a clean shutdown. Once the green LED stops blinking, it's done. After 10 seconds, press the button again and it will reboot and you will see the green LED start to blink again.

If you want to check on the integrity of your micro-sd then do this:

Code: Select all

dmesg | grep corrupt
If clean, it will return nothing. If it does return fsck lines, then you should clean up your micro-sd with dosfstools. This package is in apt-get, but it doesn't seem to clean cards properly. I compiled this package:

Code: Select all

git clone http://daniel-baumann.ch/git/software/dosfstools.git
Then cd into dosfstools and do a make (you may need to get the make package from apt-get). Once compiled it can be used right away without sudo make install. Just do this in your dosfstools directory:

Code: Select all

sudo umount /boot

sudo ./fsck.fat -V /dev/mmcblk0p1

pick 1 to remove dirty bit

sudo ./fsck.fat -a /dev/mmcblk0p1


sudo ./fsck.fat -V /dev/mmcblk0p1

sudo mount /boot
When all is fixed, you will get this:

Code: Select all

fsck.fat 3.0.26 (2014-03-07)
Starting check/repair pass.
Starting verification pass.
/dev/mmcblk0p1: 52 files, 2091/7161 clusters
Your system should never show any corrupt or fsck messages after this if you use that button for a clean shutdown

:)

Marty

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: Solderless clean shutdown/boot switch

Sat Apr 04, 2015 3:03 am

How does that button cause the Pi to restart after it has shut down? I can see the mechanism used to shut down, but I don't understand how the same button boots the Pi again.

User avatar
LinuxHippy2002
Posts: 107
Joined: Sun Jul 13, 2014 12:07 am
Location: Philadelphia, PA-USA

Re: Solderless clean shutdown/boot switch

Sat Apr 04, 2015 3:47 am

Not sure the why but I know it does. Maybe that's a built in function of the pi board?

Marty

User avatar
rpdom
Posts: 15417
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Solderless clean shutdown/boot switch

Sat Apr 04, 2015 5:13 am

ame wrote:How does that button cause the Pi to restart after it has shut down?
The pi never fully shuts down (unless the power is removed). What actually happens when it shuts down is that it starts a reboot but stops and waits in the very early (GPU) stages. Then it monitors pin 5 and continues when the pin goes low.

By the way, there is no need to enable the internal pull-up resistor on GPIO 3, as there is already a strong external pull-up resistor on the board.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: Solderless clean shutdown/boot switch

Sat Apr 04, 2015 6:34 am

rpdom wrote:
ame wrote:How does that button cause the Pi to restart after it has shut down?
The pi never fully shuts down (unless the power is removed). What actually happens when it shuts down is that it starts a reboot but stops and waits in the very early (GPU) stages. Then it monitors pin 5 and continues when the pin goes low.
Got it. There is some documentation here (Wake from halt):
http://elinux.org/RPI_safe_mode

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: Solderless clean shutdown/boot switch

Sat Apr 04, 2015 7:43 am

Actually, I really like this. I have been thinking about its general application.

Without the script, the button will not cause a shutdown, but any shutdown will leave the Pi in a state where it can restart by pressing the button. This should work out of the box right now.

With the script, the button will initiate a shutdown, but any other shutdown will work as expected. Furthermore, any shutdown can be re-started by pressing the button (not just a shutdown which was initiated by the button).

Finally, cutting and reapplying power will allow the Pi to boot normally (no button-press required), regardless of the state the Pi was in. Presumably, a second button connected to P6 (Reset) would also do the same thing, i.e. reboot the Pi regardless of the state it was in, without requiring a button press to continue.

This is generally what you want. I think there are two things it doesn't do. First, there is no method to prevent the Pi from booting when power is applied. However, I think this is an uncommon requirement. Secondly, the shutdown button would not work if the script stopped running, or if the Pi crashed. In this case you'd have to pull the power (or have a reset button on P6).

I also think a timer in the shutdown script would be a good idea, so that the button must be held for a couple of seconds to initiate a shutdown. This should guard against accidentally pressing the shutdown button.

OP did not mention the original source of this idea, but I think it's here:
http://raspberrypi.stackexchange.com/qu ... ake-button

User avatar
LinuxHippy2002
Posts: 107
Joined: Sun Jul 13, 2014 12:07 am
Location: Philadelphia, PA-USA

Re: Solderless clean shutdown/boot switch

Sat Apr 04, 2015 1:29 pm

ame wrote:Actually, I really like this. I have been thinking about its general application.

Without the script, the button will not cause a shutdown, but any shutdown will leave the Pi in a state where it can restart by pressing the button. This should work out of the box right now.

With the script, the button will initiate a shutdown, but any other shutdown will work as expected. Furthermore, any shutdown can be re-started by pressing the button (not just a shutdown which was initiated by the button).

Finally, cutting and reapplying power will allow the Pi to boot normally (no button-press required), regardless of the state the Pi was in. Presumably, a second button connected to P6 (Reset) would also do the same thing, i.e. reboot the Pi regardless of the state it was in, without requiring a button press to continue.

This is generally what you want. I think there are two things it doesn't do. First, there is no method to prevent the Pi from booting when power is applied. However, I think this is an uncommon requirement. Secondly, the shutdown button would not work if the script stopped running, or if the Pi crashed. In this case you'd have to pull the power (or have a reset button on P6).

I also think a timer in the shutdown script would be a good idea, so that the button must be held for a couple of seconds to initiate a shutdown. This should guard against accidentally pressing the shutdown button.

OP did not mention the original source of this idea, but I think it's here:
http://raspberrypi.stackexchange.com/qu ... ake-button
I went to a bunch of websites that were doing this but with different scripts...some were python and some were just bash. The python scripts seemed to work the fastest but it just may have been because they used different values in sleep and I coulda saved myself 5 hours.

So, I wasn't sure which page I went to at the time but after going through my history from 2 days ago I figured it out. It was here:

http://raspberrypi.stackexchange.com/qu ... ake-button

So you were right.

I did change halt to init 0 as I read init 0 produces a cleaner shutdown presently in the pi. I'm not sure about that, but didn't want to nuke my micro-sd that actually cost about the same as the pi (I have a Sony micro-sd).

Marty

User avatar
DougieLawson
Posts: 36322
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Solderless clean shutdown/boot switch

Sat Apr 04, 2015 1:32 pm

Halt is identical to init 0.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
LinuxHippy2002
Posts: 107
Joined: Sun Jul 13, 2014 12:07 am
Location: Philadelphia, PA-USA

Re: Solderless clean shutdown/boot switch

Sat Apr 04, 2015 1:46 pm

DougieLawson wrote:Halt is identical to init 0.
Good, because halt actually powers down the USB slot too.

Marty

Edit:

Some people may think that's wrong. It depends on what's plugged into the USB slot. init 0 takes down all the lights on my USB ethernet adapter but it doesn't on my USB WiFi adapter.

User avatar
LinuxHippy2002
Posts: 107
Joined: Sun Jul 13, 2014 12:07 am
Location: Philadelphia, PA-USA

Re: Solderless clean shutdown/boot switch

Sat Apr 04, 2015 2:32 pm

By the way, there is no need to enable the internal pull-up resistor on GPIO 3, as there is already a strong external pull-up resistor on the board.

Do you mean soldering pins onto the pi? If there are already power pins on the A+ that I could easily plug a switch on, please let me know where!

Marty

User avatar
rpdom
Posts: 15417
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Solderless clean shutdown/boot switch

Sat Apr 04, 2015 2:53 pm

LinuxHippy2002 wrote:
By the way, there is no need to enable the internal pull-up resistor on GPIO 3, as there is already a strong external pull-up resistor on the board.
All I meant was that the pull_up_down=GPIO.PUD_UP in "GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)" isn't needed.

That code and switch will work without it.
[/quote]

User avatar
LinuxHippy2002
Posts: 107
Joined: Sun Jul 13, 2014 12:07 am
Location: Philadelphia, PA-USA

Re: Solderless clean shutdown/boot switch

Sat Apr 04, 2015 3:06 pm

Oh...I just didn't wanna reinvent the wheel if power pins were provided already. I don't know much about python.

Marty

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

Re: Solderless clean shutdown/boot switch

Tue May 05, 2015 6:48 pm

Instead of the constant polling within the while loop, I suggest you use the GPIO interrupt feature. Much more efficient.
Paul

User avatar
LinuxHippy2002
Posts: 107
Joined: Sun Jul 13, 2014 12:07 am
Location: Philadelphia, PA-USA

Re: Solderless clean shutdown/boot switch

Tue May 05, 2015 9:20 pm

Could you give me an example? I don't know much about writing scripts.

Marty

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Solderless clean shutdown/boot switch

Wed May 06, 2015 7:37 am

Anyone would think that soldering is somewhere between brain surgery and nuclear fission on the difficulty scale the way some people avoid it.

One of the easiest, most therapeutic, most satisfying and liberating things you can do on a table top IMO.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

User avatar
LinuxHippy2002
Posts: 107
Joined: Sun Jul 13, 2014 12:07 am
Location: Philadelphia, PA-USA

Re: Solderless clean shutdown/boot switch

Wed May 06, 2015 9:35 am

It does take skill that I don't have... if you can do it, more power to you!

My brother learned how to solder real well years ago and gets paid good money now :)

Marty
Last edited by LinuxHippy2002 on Wed May 06, 2015 10:40 am, edited 1 time in total.

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Solderless clean shutdown/boot switch

Wed May 06, 2015 9:53 am

A soldering iron, some solder and random lumps of wire and you can learn it in 15 minutes. I'd stay way from new fangled lead free solder though.....

Hobbyists don't need to be ROHS compliant. ;)
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

User avatar
LinuxHippy2002
Posts: 107
Joined: Sun Jul 13, 2014 12:07 am
Location: Philadelphia, PA-USA

Re: Solderless clean shutdown/boot switch

Wed May 06, 2015 11:02 am

Hone your craft so you don't mess up the board. My brother says he can solder a connection on the end of a ball point pen...now that's accuracy with precision!

On another note, the soldering connections on my Samsung S3 are breaking on the power button. Good soldering connections shouldn't be so brittle that they break after a few years!

Marty

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

Re: Solderless clean shutdown/boot switch

Wed May 06, 2015 11:37 am

LinuxHippy2002 wrote:Could you give me an example? I don't know much about writing scripts.
Marty, I'm pretty sure none of us were born with that skill.
Without trying to be smart, you need to use your search skills to find the relevant information. It's all there.
Furthermore, looking at other people's code helps to get you started and lots and lots of experimenting helps to increase your skill level.

Having said that, this snippet will get you on the right path:

Code: Select all

# Put this definition right after your port definition and port assignment
#
GPIO.add_event_detect(CHANNEL, GPIO.FALLING, callback=do_shutdown, bouncetime=200)
# 
# This definition will detect a falling edge on channel and debounce it with 200mSec. 
# This is actually an interrupt, so there is no need for a polling while loop in your main code anymore.
# The interrupt service routine do_shutdown gets called when the event is detected and 
# that code should handle the shutdown
Enjoy!

User avatar
LinuxHippy2002
Posts: 107
Joined: Sun Jul 13, 2014 12:07 am
Location: Philadelphia, PA-USA

Re: Solderless clean shutdown/boot switch

Wed May 06, 2015 11:51 am

Of course people aren't born with search skills. I was in the car about to drive when I tapped that out and didn't have time to search.

Thanks for the code!

Marty

User avatar
LinuxHippy2002
Posts: 107
Joined: Sun Jul 13, 2014 12:07 am
Location: Philadelphia, PA-USA

Re: Solderless clean shutdown/boot switch

Wed May 06, 2015 3:06 pm

paulv wrote:Instead of the constant polling within the while loop, I suggest you use the GPIO interrupt feature. Much more efficient.
Paul
When you say "more efficient" what do you mean? Less lines of code or does the CPU use less power?

Marty

Edit:

I think this is one of those cultural misinterpretations. In America, efficiency usually relates to power where brevity or conciseness would relate to shortening the way you communicate. You probably meant brevity. I cannot see how 1 line of code as opposed to 6 lines would impact the CPU much.

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: Solderless clean shutdown/boot switch

Wed May 06, 2015 3:58 pm

LinuxHippy2002 wrote: When you say "more efficient" what do you mean? Less lines of code or does the CPU use less power?

Marty
He means fewer lines of code and fewer CPU cycles. Interrupts allow it to wait for an event to occur without wasting resources constantly polling for a button press each time through the loop. You set the interrupt outside the loop and it's done. When the button is pressed and the interrupt is called it stops whatever it's doing in the loop and processes the interrupt, then continues on where it left off. If you have a "busy" loop that may take a second or more to complete it has to complete the loop before it can start over and poll the button again. Using an interrupt you could even have several seconds of delay in the loop, but button response would still be immediate since the interrupt is outside the loop and always waiting for a changed condition.

User avatar
LinuxHippy2002
Posts: 107
Joined: Sun Jul 13, 2014 12:07 am
Location: Philadelphia, PA-USA

Re: Solderless clean shutdown/boot switch

Wed May 06, 2015 5:14 pm

I see. So it's shorter code and cpu efficient.

Marty

User avatar
LinuxHippy2002
Posts: 107
Joined: Sun Jul 13, 2014 12:07 am
Location: Philadelphia, PA-USA

Re: Solderless clean shutdown/boot switch

Wed May 06, 2015 6:22 pm

BTW, thought I'd mention that as soon as it warmed up here that power switch broke (it only lasted 2 weeks). It just wasn't made to sit in a warm car all day. What happened was that the wire connection pulled off of the wires because there wasn't anything keeping it there except for the "stiffness" of the wires themselves. Once they got flexible they pulled off the connectors (not the rpi) and just putting the wires back didn't fix it. The switch was just cheaply made.

What I have been doing when it's not on a network to use SSH is using a jumper from the back of a hard drive like the one pictured below.

I don't know how the rpi would respond to fingers on the GPIO pins when it's powered on, so I try not to touch the pins and use plastic tweezers if I can.

EDIT:

That jumper is rarely used. Usually the rpi is on my car network and I just use SSH and sudo init 0 from my phone.

Marty
Attachments
jumper-scaled.jpg
jumper-scaled.jpg (13.68 KiB) Viewed 20699 times

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: Solderless clean shutdown/boot switch

Sun May 31, 2015 9:43 am

I tried this today. It's great!

I simplified it a bit. Here's shutdown.py

Code: Select all

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import subprocess

GPIO.setmode(GPIO.BOARD)
# set pin 5 to input, and enable the internal pull-up resistor
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# wait for the button to be pressed (input is pulled low)
while GPIO.input(5):
    time.sleep(.1)

subprocess.call("/sbin/shutdown -h now", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Make it executable with chmod +x shutdown.py

It is not necessary to make a shell script. Here's root's crontab:

Code: Select all

@reboot /home/pi/shutdown.py
I also didn't bother with a log.

I didn't bother using the interrupt option because the Python script has to spin its wheels in a while loop anyway as it can not be allowed to exit.

Anyway, it's neat. I press the button and get a console message:

Broadcast message from [email protected] (Sun May 31 18:22:09 2015):

The system is going down for system halt NOW!


Then the Pi shuts down.

After the green light flashes 10 times (system has halted) I press the button again and it restarts.

Next I might add an indicator LED and have the while loop flash it on and off. Or something.

Return to “Troubleshooting”