RaspberryPi/boot script: Unterschied zwischen den Versionen
Moeb (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
Moeb (Diskussion | Beiträge) (Cleaned up, added pin layout for RasPi 2) |
||
Zeile 5: | Zeile 5: | ||
< | <source lang=python> | ||
# This script will wait for a button to be pressed and then shutdown | # This script will wait for a button to be pressed and then shutdown | ||
# the Raspberry Pi. | # the Raspberry Pi. | ||
Zeile 26: | Zeile 26: | ||
import os | import os | ||
# set pin numbers. | # set pin numbers. Pin layouts (looking at the board so that the pin in the ede of the boad is top left): | ||
# +5V | +5V | GND | 14 | | # pins in parentheses are commonly used specific purposes | ||
# +3.3V | | #(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 | # +5V | 28 | 30 | GND | ||
# +3.3V | 29 | 31 | GND | # +3.3V | 29 | 31 | GND | ||
ButtonPin= | # | ||
LEDpin= | # 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. | # set how often to check if the button is really pressed. | ||
# TimeInterval in seconds. | # TimeInterval in seconds. | ||
Zeile 54: | Zeile 61: | ||
def blink(cycles, period, pin): | def blink(cycles, period, pin): | ||
for n in range(0, 2*cycles+1): | for n in range(0, 2*cycles+1): | ||
GPIO.output(pin, GPIO.HIGH) | |||
time.sleep(period/2) | |||
GPIO.output(pin, GPIO.LOW) | GPIO.output(pin, GPIO.LOW) | ||
time.sleep(period/2) | time.sleep(period/2) | ||
# ISR: if our button is pressed, we will have a falling edge on ButtonPin | # ISR: if our button is pressed, we will have a falling edge on ButtonPin | ||
Zeile 72: | Zeile 80: | ||
# print(n) | # print(n) | ||
if n==TimesChecked-1: | if n==TimesChecked-1: | ||
print 'System | # print 'System will shut down now.' | ||
# blink a few times | # blink a few times | ||
blink(10, 0.6, LEDpin) | blink(10, 0.6, LEDpin) | ||
Zeile 86: | Zeile 94: | ||
blink(5, 1.2, LEDpin) | blink(5, 1.2, LEDpin) | ||
# Turn on the LED as a power LED | # Turn on the LED as a power LED | ||
GPIO.output(LEDpin,GPIO. | GPIO.output(LEDpin,GPIO.HIGH) | ||
# do nothing while waiting for button to be pressed | # do nothing while waiting for button to be pressed | ||
while 1: | while 1: | ||
time.sleep(3600) | time.sleep(3600) | ||
</ | </source> |
Version vom 23. Januar 2016, 14:57 Uhr
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>