#!/usr/bin/python # shutdown a Raspberry pi under control from a GPIO pin # Based on a script by George on the Ham Nation video series # Modified by G. Forrest Cook, W0RIO # # Run this script from the /etc/rc.local file: # /home/pi/bin/PiShutdown.py & # # Note that the Pi GPIO input pins are very noise-sensitive, even with the # internal pull-up enabled. GPIO inputs should be controlled by a spdt switch # with the ends connected to the ground and +3V rails, or to a transistor with # an external pull-up resistor tied to +3.3V so that the input always has a # low impedances path to the rails. # # Pins used on the 40 pin Raspberry Pi header: # 1 - +3.3V: switch N.C. or an external pull-up resistor on GPIO 23 # 14 - Ground: switch N.O. or reference ground for external transistor emitter # 16 - GPIO 23: shutdown switch center or external transistor collector import RPi.GPIO as gpio import time import os SHUTDOWN_PIN = 23 gpio.setmode(gpio.BCM) gpio.setup(SHUTDOWN_PIN, gpio.IN, pull_up_down = gpio.PUD_UP) def Shutdown(channel): os.system("shutdown -h now") gpio.add_event_detect(SHUTDOWN_PIN, gpio.FALLING, callback = Shutdown, bouncetime = 500) while 1: time.sleep(1)