Start By Making The File:
nano shutdown.py
And Paste In The Following Code And Save:
# !/bin/python
# Reboot/Shutting Down A Raspberry Pi Using A Momentary Switch
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Change GPIO PIN If Needed
def Shutdown(channel):
print("Reboot/Shutting Down")
time.sleep(3) #Time In Seconds Before Reboot/Shutdown
os.system("sudo shutdown") #reboot/shutdown command
GPIO.add_event_detect(4, GPIO.FALLING, callback=Shutdown, bouncetime=2000) #Change GPIO PIN If Needed
while 1:
time.sleep(1)
sudo nano /etc/rc.local
And Add The Following Line ABOVE THE EXIT 0 LINE And Save:
sudo python /home/pi/shutdown.py &