I followed the project described in Raspberry Pi Shutdown Switch – Safely Turning off the Pi, but set pin 17 to be internally pulled up. Then connect the wires like the above project, but leave out any resistors and add the following Python script in /etc/rc.local
:
python shutdown_button.py
File shutdown_button.py
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down = GPIO.PUD_UP)
while True:
print GPIO.input(17)
if (GPIO.input(17) == False):
os.system("sudo shutdown -h now")
break
time.sleep(1)