If you can't use GPIO3 and/or, looking for something a little more visual, here is what I use. I have it set up to run as a SYSTEMD job so the blinking LED is not seen until the job is started. Not the most neat code.
Code:
#!/usr/bin/env python# BUTTON_ACTION.PY - Display Status of PI & Provide manual reboot/shutdown button# This is based on the project at https://gist.github.com/lbussy/9e81cbcc617952f1250e353bd42e7775# I chose to implement the 'scripted method' since the project was# going to run 24x7 and I only needed to provide some way for# someone else to reboot/shutdown the PI.# There are print statements since the service definition I used# included specs for capturing out/err to a log file.# This program needs to be run with the -u Python3 option so as not# to buffer output.# Here is the meaning of the led# BLINKING - Running, waiting for reboot/shutdown button to be pushed# ON has 2 meanings# BUTTON HELD - Waiting x seconds to ensure reboot/shutdown requested# BUTTON RELEASED - An error has occurred. This is likely short# lived since the service will restart and# the light will start blinking again.# OFF - PI is shutting down or starting up.__version__ = "V2.7"__program__ = "BUTTON_ACTION"__status__ = "Production"__success__ = 0__failure__ = 1from gpiozero import LED, Buttonfrom datetime import datetimefrom subprocess import callimport subprocessimport argparsefrom time import sleepimport datetime as dttmfrom sys import exc_infoimport sysfrom sys import exit as sys_exit # Named so to avoid confusion with Pt exit()from traceback import extract_tbimport traceback#************************************************************************************************# PDT_FUNCTION - Print standard date/time to precede line for WC programs# V1.0 - Initialdef pdt(): now = dttm.datetime.now() return "["+now.strftime("%m-%d %H:%M:%S")+"] "#************************************************************************************************# https://docs.python.org/3/library/argparse.htmldef doArgs(): # parser will print errors. Only need to trap to return failure try: parser = argparse.ArgumentParser() parser.add_argument("--shutdown", action="store_true",\ help="Shutdown instead of rebooting", default=False) args = parser.parse_args() except: return False # --debug Provide information using for debugging global shutdown shutdown = args.shutdown return True# End of doArgs()# Mainlinedef main(): print(pdt(),__program__,__version__) # Parse Arguements if not doArgs(): return False if shutdown: print(pdt()+"Will shutdown instead of rebooting") led = LED(19) # GPIO 19 led.blink() # Indicates running and waiting for button push button = Button(6) # GPIO6 # Loop checking every few seconds to see if the button has been# pressed and held. When the button is first pressed, the led# changes from blinking to on. This is visual feedback to the# operator. If the button is held down, the led is turned off to# indicate the PI is rebooting/shutting down.# If the led is left on, a problem has occurred. loop_sleep_time = 2 # Time to wait between checking for button pressed next_check_net_cnt = net_check_after + 1 # For check on first pass print_net_info_once = True try: # ***** MAIN LOOP **** while True: # Ends only with a reboot/shutdown or exception exit if button.is_pressed: print(pdt()+__program__+": Button pressed") led.on() # Led user know the button pressed detected sleep(5) # User has to hold the button down if button.is_pressed: led.off() # Let user know reboot/shutdown in progress sleep(1) if shutdown: print(pdt()+"Shutting down") call(["sudo","shutdown","-h","now"]) else: print(pdt(),"Rebooting") call(["sudo","reboot"]) return __failure__ # Should never get here else: led.blink() # Let user know reboot/shutdown was not initiated sleep(loop_sleep_time) # Don't make too short or higher CPU is used. # There should not be exceptions# If there is, make sure we log it nicely except: led.on() # Keep light on to say there is a problem. exc_type, exc_value, exc_traceback = exc_info() type=exc_type.__name__ summ = str(extract_tb(exc_traceback)) print (pdt()+__program__+": Exception '"+type+"="+str(exc_value)+"'. Traceback: "+summ) return __failure__ # Return error# return __success__ # We should never get here. # MAIN if __name__ == "__main__": rtn=main() sys_exit(rtn)
Statistics: Posted by DS256 — Wed Nov 06, 2024 3:15 pm