How to do it...

  1. Create the shtdwn.py script as follows:
#!/usr/bin/python3 #shtdwn.py import time import RPi.GPIO as GPIO import os # Shutdown Script DEBUG=True #Simulate Only SNDON=True #HARDWARE SETUP # GPIO # 2[==X==L=======]26[=======]40 # 1[===1=========]25[=======]39 #BTN CONFIG - Set GPIO Ports GPIO_MODE=GPIO.BOARD SHTDWN_BTN = 7 #1 LED = 12 #L def gpio_setup(): #Setup the wiring GPIO.setmode(GPIO_MODE) #Setup Ports GPIO.setup(SHTDWN_BTN,GPIO.IN,pull_up_down=GPIO.PUD_UP) GPIO.setup(LED,GPIO.OUT) def doShutdown(): if(DEBUG):print("Press detected") time.sleep(3) if GPIO.input(SHTDWN_BTN): if(DEBUG):print("Ignore the shutdown (<3sec)") else: if(DEBUG):print ("Would shutdown the RPi Now") GPIO.output(LED,0) time.sleep(0.5) GPIO.output(LED,1) ...

Get Raspberry Pi 3 Cookbook for Python Programmers - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.