RaspberryPi/boot script: Unterschied zwischen den Versionen

Aus Wiki CCC Göttingen
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
 
(4 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
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.
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.  
Fortunately, both of these issues can be remedied using the GPIO-port.  
Here is a python script that does both. Details are in the script.
Here is a little python script based on the work of flipdot that does both. Details are in the script.






<highlightSyntax language="python">
<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. Layout (board rev 2.0):
# 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 | 15 |  18 | GND | 23 | 24   | GND | 25 | 8 | 7  
# pins in parentheses are commonly used specific purposes
# +3.3V |   2 |     3 | 4 | GND |  17 |  27 | 22 | +3.3V | 10 | 9 | 11 | GND   
#(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=14
LEDpin=22
# 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
#
#HifiBerry Digi and AMp use GPIO 2,3,28,29,30,31
#HifiBerry DAC+, Digi+, Amp+ use GPIO 2,3,18,19,20,21
 
ButtonPin=23
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 53: Zeile 63:


def blink(cycles, period, pin):
def blink(cycles, period, pin):
for n in range(0, 2*cycles+1):
for n in range(0, cycles):
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)
GPIO.output(pin, GPIO.HIGH)
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 83:
# print(n)
# print(n)
if n==TimesChecked-1:
if n==TimesChecked-1:
print 'System would shut down now.'
# print 'System will shut down now.'
# blink a few times
# blink a few times
blink(10, 0.6, LEDpin)
blink(3, 0.6, LEDpin)
os.system("sudo shutdown -h now")
os.system("sudo shutdown -h now")
Zeile 84: Zeile 95:


# Blink slowly a few times when booting
# Blink slowly a few times when booting
blink(5, 1.2, LEDpin)
blink(2, 1.2, LEDpin)
# Turn on the LED as a power LED
# Turn on the LED as a power LED
GPIO.output(LEDpin,GPIO.LOW)
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)
</highlightSyntax>
</source>

Aktuelle Version vom 24. März 2016, 09:02 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>

  1. This script will wait for a button to be pressed and then shutdown
  2. the Raspberry Pi.
  3. It will also turn on a LED once the Rapspberry Pi has finished
  4. booting which will flash to acknowledge the button press and remain
  5. on as long as the Raspberry Pi remains operational.
  6. REMEMBER: you will need resistors between the button and the LED
  7. and the naked pins or you will damage your Raspberry Pi!
  8. You can run this script automatically by adding the line
  9. python [FILENAME_OF_THIS_SCRIPT] &
  10. to your /etc/rc.local
  1. http://kampis-elektroecke.de/?page_id=3740
  2. http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio
  3. https://pypi.python.org/pypi/RPi.GPIO
  4. 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

  1. set pin numbers. Pin layouts (looking at the board so that the pin in the ede of the boad is top left):
  2. pins in parentheses are commonly used specific purposes
  3. (board Raspberry Pi 1 model B rev 2.0):
  4. +5V | +5V | GND |(14)| (15)| 18 | GND | 23 | 24 | GND | 25 | (8)| (7)
  5. +3.3V | (2)| (3) | (4)| GND | 17 | 27 | 22 | +3.3V | (10)| (9)|(11)| GND
  6. +5V | 28 | 30 | GND
  7. +3.3V | 29 | 31 | GND
  8. Raspberry Pi 2 model B rev 1.1
  9. +5V | +5V | GND |(14)| (15)| 18 | GND | 23 | 24 | GND | 25 | (8)| (7) |(1)| GND | 12 | GND | 16 | 20 | 21
  10. +3.3V | (2)| (3) | (4)| GND | 17 | 27 | 22 | +3.3V | (10)| (9)|(11)| GND |(0)| 5 | 6 | 13 | 19 | 26 | GND
  11. HifiBerry Digi and AMp use GPIO 2,3,28,29,30,31
  12. HifiBerry DAC+, Digi+, Amp+ use GPIO 2,3,18,19,20,21

ButtonPin=23 LEDpin=24

  1. set how often to check if the button is really pressed.
  2. TimeInterval in seconds.

TimesChecked=3 TimeInterval=0.2

  1. Since we use powers beyond the author's control, we need to suppress warnings

GPIO.setwarnings(False)

  1. we will use the pin numbering of the SoC, so our pin numbers in the code are
  2. the same as the pin numbers on the gpio headers

GPIO.setmode(GPIO.BCM)

  1. ButtonPin will be input and will have his pull up resistor activated
  2. so we only need to connect a button to ground

GPIO.setup(ButtonPin, GPIO.IN, pull_up_down = GPIO.PUD_UP)

  1. LEDpin is configured as output
  2. GPIO.setmode(GPIO.BOARD)

GPIO.setup(LEDpin, GPIO.OUT)

def blink(cycles, period, pin): for n in range(0, cycles): GPIO.output(pin, GPIO.HIGH) time.sleep(period/2) GPIO.output(pin, GPIO.LOW) time.sleep(period/2)


  1. ISR: if our button is pressed, we will have a falling edge on ButtonPin
  2. 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(3, 0.6, LEDpin) os.system("sudo shutdown -h now")


  1. Now we are programming ButtonPin as an interrupt input
  2. 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)

  1. Blink slowly a few times when booting

blink(2, 1.2, LEDpin)

  1. Turn on the LED as a power LED

GPIO.output(LEDpin,GPIO.HIGH)

  1. do nothing while waiting for button to be pressed

while 1:

       time.sleep(3600)

</source>