start and shutdown switch [raspberrypi]

wpid-wp-1425371079159.png
pi1
based on:
http://www.forum-raspberrypi.de/Thread-tutorial-hoch-und-runterfahren-mittels-taster-incl-status-led?page=21
install software:
sudo apt-get install python-dev -y && sudo apt-get install python-rpi.gpio -y

create python shutdown script:
sudo nano /home/pi/.shutdown.py

# shutdown script for Raspberry Pi
# watch LOW level on pin 5 to enter sleep mode
# status led on pin 7: ON = ready, BLINK = confirm button

import RPi.GPIO as GPIO
import os
import time
#import subprocess

#def ShutDownSh():
#    subprocess.call('/home/pi/.shutdownsh.sh', shell=True)

# use the pin number as on the raspi board

GPIO.setmode(GPIO.BOARD)

# set pin 7 as output and HIGH, pin 5 is input

GPIO.setup(7, GPIO.OUT)
GPIO.output(7, True)
GPIO.setup(5, GPIO.IN)

# start the loop for every .5 seconds, waiting for LOW on pin 5
# then 2 short flashes with led to confirm and shutdown to sleep mode

while True:
        if not (GPIO.input(5)):
                GPIO.output(7, False)
                time.sleep(.1)
                GPIO.output(7, True)
                time.sleep(.1)
                GPIO.output(7, False)
                time.sleep(.1)
                GPIO.output(7, True)
                os.system("sudo shutdown -h now")
#                ShutDownSh()
        time.sleep(.5)

make the script executeable:
sudo chmod +x /home/pi/.shutdown.py

add the script to startup:
sudo nano /etc/rc.local
and insert the following line before exit0:
sudo python /home/pi/.shutdown.py &

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s