volumio_gpio.py

#!/usr/bin/env python
# created by chris@drumminhands.com

import os
import glob
import time
from time import sleep
import RPi.GPIO as GPIO
import atexit
import sys

########################
### Variables Config ###
########################
led_pin = 15 # LED 1
button_pin = 22 # pin for button to shutdown the pi
debounce = 0.2

####################
### Other Config ###
####################
GPIO.setmode(GPIO.BOARD)
GPIO.setup(led_pin,GPIO.OUT) # LED 1
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # falling edge detection on button 2
GPIO.output(led_pin,False);

#################
### Functions ###
#################

def cleanup(): #if anything happens, cleanup the GPIO pins
print(‘Ended abruptly’)
GPIO.cleanup()
atexit.register(cleanup)

def shut_it_down(channel): #shutdown the pi
print “Shutting down…”
GPIO.output(led_pin,True);
time.sleep(3)
os.system(“sudo halt”)

####################
### Main Program ###
####################

# when a falling edge is detected on button_pin and button3_pin, regardless of whatever
# else is happening in the program, their function will be run
# GPIO.add_event_detect(button_pin, GPIO.FALLING, callback=shut_it_down, bouncetime=300)

print “Volumio Button Running…”
GPIO.output(led_pin,True); #light up the lights to show the app is running
time.sleep(3)
GPIO.output(led_pin,False); #turn off the lights

while True:
GPIO.wait_for_edge(button_pin, GPIO.FALLING)
time.sleep(debounce) #debounce
shut_it_down()