Shutdown button not working :(
-
I dont typically like to ask for help, but I am going to blow my brains out on this one. I have used 3 different websites for using GPIO pins to shutdown my Raspberry Pi 3 and none of them work. I have used a breadboard with a simple button and a soldered board with a button. Both, the button on the breadboard and the soldered button on a board are connected to GPIO3 (PIN#5) and ground on PIN#6. If I used the controller and shutdown the Pi, then pushing this button will turn my Pi on. But none of the Python scripts will turn my Pi off.
__ attempted Python Script 1
#!/usr/bin/pythonimport RPi.GPIO as GPIO
import time
import subprocesswe will use the pin numbering to match the pins on the Pi, instead of the
GPIO pin outs (makes it easier to keep track of things)
GPIO.setmode(GPIO.BOARD)
use the same pin that is used for the reset button (one button to rule them all!)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)
oldButtonState1 = True
while True:
#grab the current button state
buttonState1 = GPIO.input(5)check to see if button has been pushed
if buttonState1 != oldButtonState1 and buttonState1 == False:
subprocess.call(“shutdown -h now”, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
oldButtonState1 = buttonState1time.sleep(.1)
__ attempted Python Script 2
#!/bin/python
# Simple script for shutting down the raspberry Pi at the press of a button.
import RPi.GPIO as GPIO
import time
import os
# Use the Broadcom SOC Pin numbers
# Setup the Pin with Internal pullups enabled and PIN in reading mode.
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)
# Our function on what to do when the button is pressed
def Shutdown(channel):
os.system("sudo shutdown -h now")
# Add our function to execute when the button pressed event happens
GPIO.add_event_detect(5, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)
# Now wait!
while 1:
time.sleep(1)__ attempted Python Script 3
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import subprocessGPIO.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)
the first 2 I added to the /etc/rc.local under fi
sudo python /home/pi/Scripts/shutdown_pi.py &the 3rd one, I was trying a different one and made an SH file and added it to crontab
@reboot sh /home/pi/shutdown.sh >/home/pi/shutdown-logs/cronlog 2>&1I get an error message on the 3rd script, which I don't know what it wants
python script line 13 buttonState1 = gpio.input(5) syntax errorI have been banging my head on this for 2 days and just don't understand why I cant get it to work. It shouldn't be that difficult, but somehow I have managed to make it hard :-/
Thanks,
Anasazi -
This is really better suited for the 'Help and Support' section.
-
Why don't you try using GPIO Zero and use this Python script on startup?
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()
Works perfectly.
-
not sure how to mark this as resolved. But i have it working. Started from scratch with a fresh installation.
A couple of things to keep in mind, for anyone else that reads this.-
never copy and paste the python script. HAND type it. its not that big and it wont take that long.
-
I misunderstood what number went into the python script. The PIN number on the Raspberry Pi or the GPIO labeled number. and that all depends on where you connect the button to turn off an on too.
GPIO18 is on PIN12 and typically people will use PIN14 for the ground on that button.
So the python script might be working appropriately for some, but if they put the #18 in the script, but connect your button to PIN18 and PIN20 below it for ground, then the python script is looking at a different set of PIN and the button doesn't work, but can be confusing about why.
I'm sure there are some ppl just <sighing> about this and think that its silly that I got these confused, but I am a systems and network engineer, but that doesn't mean I understand GPIO PINs and how they are properly related to in a python script.
Thanks, :)
Anasazi -
-
not sure how to mark this as resolved.
Using 'Topic Tools' under either the first or last post, select 'Ask as Question'. Afterwards, you can then mark it as being solved in the same location.
-
never copy and paste the python script. HAND type it. its not that big and it wont take that long.
Now that you said this, I noticed on your original post that you had something like
“shutdown -h now”
but those quotes aren't recognized as quote by the RPi. They need to be ASCII quotes like this"shutdown -h now"
so when you retyped it by hand you actually fixed these quotes!We just talked about this the other day.
I'm sure there are some ppl just <sighing> about this and think that its silly that I got these confused
I wouldn't worry about that and most of the ppl on this forum are here to help and to learn. Hopefully someone else finds this and it helps them resolve their wrong use of quotation dilemmas lol.
Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.
Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.