Pi shutdown button

I use OctoRemote

This is the guide I used. I'm also running my Pi off my Ender 3's PSU using a buck converter. I press the button to shut the Pi down, then I flip my printer's switch off. When the printer is turned back on, the Pi will turn on again automatically without needing to press the button again.

This is the shutdown command I use for my Octopi installs.

https://github.com/adafruit/Adafruit-GPIO-Halt.

You need to ssh to a terminal to install. Instructions are in README.md

The topic of this discussion slightly shifted, but since I've promised... Here is my (hopefully) final version of shutdown script.

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

Buzzer = 35      # pin-35 BCM-19/GPIO-24
Button = 11      # pin-11 BCM-17/GPIO-0
Freq   = 300

def setup(buzzer_pin, button_pin):
    GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
    GPIO.setup(buzzer_pin, GPIO.OUT)
    global Buzz # Assign a global variable to replace GPIO.PWM
    GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def beep(buzzer_pin):
    Buzz = GPIO.PWM(buzzer_pin, Freq) # Set initial frequency
    Buzz.start(50) # Start buzzer_pin with 50% duty ratio
    time.sleep(0.25)
    Buzz.stop()

def destroy():
    GPIO.cleanup()

def loop(button_pin, buzzer_pin):
    n = 0
    beep(buzzer_pin)    # Beep once when script starts
    while True:
        # The script will stop @ GPIO.wait_for_edge waiting for button_pin to go from high to low
        GPIO.wait_for_edge(button_pin, GPIO.FALLING)
        # Starting the loop to make sure button is pressed for 5 sec
        while True:
            if GPIO.input(button_pin):
                # Button was released in less than 5 sec; going back to waiting for GPIO.FALLING
                n = 0
                break
            else:
                n += 1
            if n >= 5:
            # 5 seconds passed; shutting RPI down
                beep(buzzer_pin)
                time.sleep(1)
                from subprocess import call
                call("sudo shutdown -h now", shell=True)
                # print("Shutting down")
                destroy()
                sys.exit()
            time.sleep(1)
        time.sleep(3)

if __name__ == '__main__':     # Program starts here
    setup(Buzzer, Button)

    try:
        loop(Button, Buzzer)      
    except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed
        destroy()

Please note that with this revision pull-up resistor is not required.

Thanks

3 Likes

I made a new circuit / board with 2 buttons "Raspi-On-Off", a modified version of this, with addition of AC-detection from a (TP-Link-)MainsSwitch. If interested, download my instructions, circuit and case here. It contains the original scritps and instruction from here. So with this, switching the RasPi (model 3B+) remote and safely On/Off works like a charm! :slight_smile:
Att: AC-Mains-Power (here 230VAC) is deadly dangerous! Don't handle with it if you are not 100% sure of what you do! - if you copy my work/circuit/board-layout (free to download or modify) I don't take responsibility of all kind of consequences, so it's up to you to pay attention to dangerous parts, specially to insulations concerning the case, (mains-)cables, etc.!