Skip to content

Instantly share code, notes, and snippets.

@K-Ko
Created January 22, 2018 10:18
Show Gist options
  • Save K-Ko/fcb040de2cb5f015fec84a42df5b8848 to your computer and use it in GitHub Desktop.
Save K-Ko/fcb040de2cb5f015fec84a42df5b8848 to your computer and use it in GitHub Desktop.
Shutdown script for Raspberry Pi
#!/usr/bin/env python
#
# Shutdown script for Raspberry Pi
#
# Based on
# http://www.forum-raspberrypi.de/Thread-tutorial-hoch-und-runterfahren-mittels-taster-incl-status-led
#
# Watch LOW level on pin 5 to enter sleep mode
# Use power LED as status LED on pin 35: BLINK = confirm button
import os, time
import RPi.GPIO as GPIO
# Power LED
LED = 35
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(LED, GPIO.OUT)
GPIO.output(LED, True)
# MUST be GPIO3, no chance to change!
GPIO.setup(3, GPIO.IN)
count = 0
# Start the loop for every .3 seconds, waiting for 3 times LOW on pin 3
while True:
if not (GPIO.input(3)):
# Count button pressed
count = count + 1
else:
# Reset counter
count = 0
# Button was pressed for at least 3 loops (~1 sec.)
if count >= 3:
# Blink 5 times
for i in range(5):
GPIO.output(LED, False)
time.sleep(.1)
GPIO.output(LED, True)
time.sleep(.1)
# Power off LED
GPIO.output(LED, False)
# Halt
print("::: Shut down now ...")
os.system("shutdown -h now")
# Loop
time.sleep(.3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment