• Recent
  • Tags
  • Popular
  • Home
  • Docs
  • Register
  • Login
RetroPie forum home
  • Recent
  • Tags
  • Popular
  • Home
  • Docs
  • Register
  • Login
Please do not post a support request without first reading and following the advice in https://retropie.org.uk/forum/topic/3/read-this-first

Shutdown button not working :(

Scheduled Pinned Locked Moved Help and Support
shutdown switchraspberry pi 3retropie 4.1
6 Posts 4 Posters 2.9k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A
    anasazi29
    last edited by 6 Apr 2017, 21:48

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

    import RPi.GPIO as GPIO
    import time
    import subprocess

    we 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 = buttonState1

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


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

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

    I 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

    1 Reply Last reply Reply Quote 0
    • M
      mediamogul Global Moderator
      last edited by 7 Apr 2017, 02:18

      This is really better suited for the 'Help and Support' section.

      RetroPie v4.5 • RPi3 Model B • 5.1V 2.5A PSU • 16GB SanDisk microSD • 512GB External Drive

      1 Reply Last reply Reply Quote 0
      • H
        HoolyHoo
        last edited by 8 Apr 2017, 15:06

        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.

        1 Reply Last reply Reply Quote 0
        • A
          anasazi29
          last edited by 11 Apr 2017, 01:48

          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.

          1. never copy and paste the python script. HAND type it. its not that big and it wont take that long.

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

          M B 2 Replies Last reply 11 Apr 2017, 01:51 Reply Quote 1
          • M
            mediamogul Global Moderator @anasazi29
            last edited by 11 Apr 2017, 01:51

            @anasazi29

            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.

            RetroPie v4.5 • RPi3 Model B • 5.1V 2.5A PSU • 16GB SanDisk microSD • 512GB External Drive

            1 Reply Last reply Reply Quote 1
            • B
              backstander @anasazi29
              last edited by 11 Apr 2017, 03:19

              @anasazi29

              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.

              1 Reply Last reply Reply Quote 2
              6 out of 6
              • First post
                6/6
                Last post

              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.

                This community forum collects and processes your personal information.
                consent.not_received