Advertisement
Guest User

Untitled

a guest
Apr 20th, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. # off_button.py
  2. #
  3.  
  4. import time
  5. import RPi.GPIO as GPIO
  6.  
  7. # Pin definition
  8. shutdown_pin = 17
  9.  
  10. GPIO.setwarnings(False)
  11.  
  12. #Use BCM pin numbering (i.e. the GPIO number, not pin number)
  13. GPIO.setmode(GPIO.BCM)
  14.  
  15. # Use built-in internal pullup resistor so the pin is not floating
  16. # if using a momentary push button without a resistor.
  17. GPIO.setup(shutdown_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  18.  
  19. # modular function to shutdown Pi
  20. def shut_down():
  21. print("shutting down")
  22. command = "/usr/bin/sudo /sbin/shutdown -h now"
  23. import subprocess
  24. process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
  25. output = process.communicate()[0]
  26. print(output)
  27.  
  28. # Check button if we want to shutdown the Pi safely
  29. while True:
  30. #short delay, otherwise this code will take up a lot of the Pi's processing power
  31. time.sleep(0.5)
  32.  
  33. # For troubleshooting, uncomment this line to output buton status on command line
  34. #print('GPIO state is = ', GPIO.input(shutdown_pin))
  35. if GPIO.input(shutdown_pin)== False:
  36. shut_down()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement