import RPi.GPIO as GPIO # import GPIO library import subprocess # needed for subprocess call from time import sleep # this lets us have a time delay GPIO.setmode(GPIO.BCM) # set up BCM GPIO numbering GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # set up pin 26 for input detection of 3.3V init = 1 # set inital state for boot message try: while True: # this will carry on until you hit CTRL+C if GPIO.input(26): # if port 26 == 1 (there is power) if init == 1: # we must have cold booted on purpose or from a failure on UPS dead battery extended outage email = 'echo "REBOOT from extended power failure or device reset. Power is ON." | mail email1@email.com, email2@email.com' subprocess.call(email, shell=True) # send email command else: email = 'echo "Power restored at site. Commercial power is ON." | mail email1@email.com, email2@email.com' subprocess.call(email, shell=True) # send email command while GPIO.input(26): # do nothing if the power state stays on sleep(0.1) else: # there is no power detected at pin 26 init = 0 # why bother checking every time it happens only once email = 'echo "Power failure at site. Commercial power is OFF." | mail email1@email.com, email2@email.com' subprocess.call(email, shell=True) # send email command while GPIO.input(26) == 0: # do nothing if the power state stays off sleep(0.1) finally: # this block will run no matter how the try block exits GPIO.cleanup() # clean up on exit