I have a Raspberry Pi 4 (running retropie) that I am trying to get a shutdown button to work. I have a momentary switch wired to GPIO pins 5 and 6.
I have a python script:
import RPi.GPIO as GPIO
import time
import subprocess
from threading import Timer
SHUTDOWN_HOLD_TIME =3 # Time in seconds that power button must be held
SHUTDOWN_PIN = 5 # Pin to trigger shutdown, pin 5 will also start up the pi when it's off
# GPIO.BOARD means use pin numbering to matching the pins on the Pi, instead of the
# CPU's actual pin numbering (makes it easier to keep track of things)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(SHUTDOWN_PIN, GPIO.IN, pull_up_down = GPIO.PUD_UP)
buttonReleased = True
while buttonReleased:
GPIO.wait_for_edge(SHUTDOWN_PIN, GPIO.FALLING)
# button has been pressed
buttonReleased = False
for i in range(SHUTDOWN_HOLD_TIME * 10):
time.sleep(0.1)
if GPIO.input(SHUTDOWN_PIN):
buttonReleased = True
break
GPIO.cleanup()
subprocess.call("shutdown -h now", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
This doesn't seem to work. I test it by running it via SSH and it returned this message:
Traceback (most recent call last):
File "shutdown.py", line 1, in <module>
import RPi.GPIO as GPIO
RuntimeError: This module can only be run on a Raspberry Pi!
I made sure I had the latest Python3 and GPIO and yet still not working. Any help would be greatly appreciated!