RaspberryPi/boot script: Unterschied zwischen den Versionen

Aus Wiki CCC Göttingen
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 90: Zeile 90:
while 1:
while 1:
         time.sleep(3600)
         time.sleep(3600)
<highlightSyntax>
</highlightSyntax>

Version vom 4. Januar 2014, 22:34 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 python script that does both. Details are in the script.


<highlightSyntax language="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. Layout (board rev 2.0):
  2. +5V | +5V | GND | 14 | 15 | 18 | GND | 23 | 24 | GND | 25 | 8 | 7
  3. +3.3V | 2 | 3 | 4 | GND | 17 | 27 | 22 | +3.3V | 10 | 9 | 11 | GND
  4. +5V | 28 | 30 | GND
  5. +3.3V | 29 | 31 | GND

ButtonPin=14 LEDpin=22

  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, 2*cycles+1): GPIO.output(pin, GPIO.LOW) time.sleep(period/2) GPIO.output(pin, GPIO.HIGH) 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 would shut down now.' # blink a few times blink(10, 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(5, 1.2, LEDpin)

  1. Turn on the LED as a power LED

GPIO.output(LEDpin,GPIO.LOW)

  1. do nothing while waiting for button to be pressed

while 1:

       time.sleep(3600)

</highlightSyntax>