Sorry I have not post here in a while since I have not done much with this since I set it up. I went and updated to the latest version of RetroPie yesterday and found my Mausberry Circuit was shutting down the system shortly after booting with the in/out leads connected to the pins I always used. So something has been changed and reversed, will be playing with this to figure out what is happening...
I see that Elliot B added GPIO lines to set a GPIO output pin, not sure if something changed in the code to require that. They are using a momentary switch where as mine is a toggle (Switch from an NES deck)
But this behavior happens even before I install the script regardless of the position of the switch...
Edit: So I ended up having to add lines to set the other GPIO pin at an output (which is connected to the IN on the Mausberry)... Not sure why, since before I was able to sense a rise and fall using just one GPIO, maybe when updating something changed about this functionality?
Here is my script now:
Code: Select all
#!/usr/bin/env python
# Import the RPi.GPIO and OS
import RPi.GPIO as GPIO
import os
# GPIO port setup
GPIO.setmode(GPIO.BCM) # Set pin numbering to GPIO numbering
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Setup GPIO6 as an input, pulled downsude
GPIO.setup(5, GPIO.OUT) # Setup GPIO5 as an output
GPIO.output(5, 1) # Setup initial value of GPIO to 1
# Power button event
GPIO.wait_for_edge(6, GPIO.RISING) # Stop script till button released
# Power button action
os.system('poweroff') # action performed on button press
I also changed my GPIO connections to 5 (Mausberry IN) and 6 (Mausberry OUT) since I am on a Pi 3 and no longer need to use the passthrough pins on my Petrock GPIO adapter...
Also for those who come across this and are interested, I am running this script via another script that runs as a service...
Code: Select all
#!/bin/sh
### BEGIN INIT INFO
# Provides: powerbutton.py
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Power Button Listen Script
# Description: Service that runs /usr/local/bin/powerbutton.py Python script to watch GPIO pins.
### END INIT INFO
# Change the next 3 lines to suit where you install your script and what you want to call it
DIR=/usr/local/bin/
DAEMON=$DIR/powerbutton.py
DAEMON_NAME=powerbutton
# This next line determines what user the script runs as.
# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
DAEMON_USER=root
# The process ID of the script when it runs is stored here:
PIDFILE=/var/run/$DAEMON_NAME.pid
. /lib/lsb/init-functions
do_start () {
log_daemon_msg "Starting system $DAEMON_NAME daemon"
start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON
log_end_msg $?
}
do_stop () {
log_daemon_msg "Stopping system $DAEMON_NAME daemon"
start-stop-daemon --stop --pidfile $PIDFILE --retry 10
log_end_msg $?
}
case "$1" in
start|stop)
do_${1}
;;
restart|reload|force-reload)
do_stop
do_start
;;
status)
status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
exit 1
;;
esac
exit 0
service script goes in /etc/init.d
power button script goes in /usr/local/bin
make both executable using chmod
add symbolic links to /etc/rc?.d by running
Code: Select all
sudo update-rc.d /etc/init.d/nameofservicescript.sh defaults
Runs as expected now at startup and just as little overhead as before...