RaspberryPi/boot script
The Raspberry Pi is a great little computer, but it suffers from the fact that there is neither an ACPI-like shutdown button nor a power LED. Fortunately, both of these issues can be remedied using the GPIO-port. Here is a little python script based on the work of flipdot that does both. Details are in the script.
<source lang=python>
- This script will wait for a button to be pressed and then shutdown
- the Raspberry Pi.
- It will also turn on a LED once the Rapspberry Pi has finished
- booting which will flash to acknowledge the button press and remain
- on as long as the Raspberry Pi remains operational.
- REMEMBER: you will need resistors between the button and the LED
- and the naked pins or you will damage your Raspberry Pi!
- You can run this script automatically by adding the line
- python [FILENAME_OF_THIS_SCRIPT] &
- to your /etc/rc.local
- http://kampis-elektroecke.de/?page_id=3740
- http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio
- https://pypi.python.org/pypi/RPi.GPIO
- http://spaceblogs.org/flipdot/2013/06/shut-down-your-raspberry-pi-on-button-press-and-add-reset-function/
import RPi.GPIO as GPIO import time import os
- set pin numbers. Pin layouts (looking at the board so that the pin in the ede of the boad is top left):
- pins in parentheses are commonly used specific purposes
- (board Raspberry Pi 1 model B rev 2.0):
- +5V | +5V | GND |(14)| (15)| 18 | GND | 23 | 24 | GND | 25 | (8)| (7)
- +3.3V | (2)| (3) | (4)| GND | 17 | 27 | 22 | +3.3V | (10)| (9)|(11)| GND
- +5V | 28 | 30 | GND
- +3.3V | 29 | 31 | GND
- Raspberry Pi 2 model B rev 1.1
- +5V | +5V | GND |(14)| (15)| 18 | GND | 23 | 24 | GND | 25 | (8)| (7) |(1)| GND | 12 | GND | 16 | 20 | 21
- +3.3V | (2)| (3) | (4)| GND | 17 | 27 | 22 | +3.3V | (10)| (9)|(11)| GND |(0)| 5 | 6 | 13 | 19 | 26 | GND
ButtonPin=18 LEDpin=24
- set how often to check if the button is really pressed.
- TimeInterval in seconds.
TimesChecked=3 TimeInterval=0.2
- Since we use powers beyond the author's control, we need to suppress warnings
GPIO.setwarnings(False)
- we will use the pin numbering of the SoC, so our pin numbers in the code are
- the same as the pin numbers on the gpio headers
GPIO.setmode(GPIO.BCM)
- ButtonPin will be input and will have his pull up resistor activated
- so we only need to connect a button to ground
GPIO.setup(ButtonPin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
- LEDpin is configured as output
- GPIO.setmode(GPIO.BOARD)
GPIO.setup(LEDpin, GPIO.OUT)
def blink(cycles, period, pin): for n in range(0, 2*cycles+1): GPIO.output(pin, GPIO.HIGH) time.sleep(period/2) GPIO.output(pin, GPIO.LOW) time.sleep(period/2)
- ISR: if our button is pressed, we will have a falling edge on ButtonPin
- this will trigger this interrupt:
def Int_shutdown(channel): for n in range(0,TimesChecked): time.sleep(TimeInterval) # print(GPIO.input(ButtonPin)) if (GPIO.input(ButtonPin)): # print 'exiting loop' break # shutdown our Raspberry Pi # print 'loop complete' # print(n) if n==TimesChecked-1: # print 'System will shut down now.' # blink a few times blink(10, 0.6, LEDpin) os.system("sudo shutdown -h now")
- Now we are programming ButtonPin as an interrupt input
- it will react on a falling edge and call our interrupt routine "Int_shutdown"
GPIO.add_event_detect(ButtonPin , GPIO.FALLING, callback = Int_shutdown, bouncetime = 2000)
- Blink slowly a few times when booting
blink(5, 1.2, LEDpin)
- Turn on the LED as a power LED
GPIO.output(LEDpin,GPIO.HIGH)
- do nothing while waiting for button to be pressed
while 1:
time.sleep(3600)
</source>