JohnnyGrey86

low_bat_shutdown

Mar 8th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.83 KB | None | 0 0
  1. #!/bin/bash
  2. # LiPoPi
  3. # Shut down the Pi if the GPIO goes high indicating low battery
  4.  
  5. # GPIO Port
  6. gpio_port="15"
  7.  
  8. # Enable GPIO
  9. if [ ! -d "/sys/class/gpio/gpio$gpio_port" ]; then
  10.   echo $gpio_port > /sys/class/gpio/export || { echo -e "Can't access GPIO $gpio_port" 1>&2; exit 1; }
  11. fi
  12.  
  13. # Set it to input
  14. echo "in" > /sys/class/gpio/gpio$gpio_port/direction || { echo -e "Can't set GPIO $gpio_port to an input" 1>&2; exit 1; }
  15.  
  16. # Set it as active high
  17. echo 0 > /sys/class/gpio/gpio$gpio_port/active_low || { echo -e "Can't set GPIO $gpio_port to active high" 1>&2; exit 1; }
  18.  
  19. # If its low (low battery light is on), shutdown
  20. if [ "`cat /sys/class/gpio/gpio$gpio_port/value`" != 1 ]; then
  21.   echo "Shutting down due to low power `date`"
  22.   /sbin/shutdown -h now || { echo -e "Can't halt the system" 1>&2; exit 1; }
  23. fi
  24.  
  25. exit 0
Advertisement
Add Comment
Please, Sign In to add comment