I got the battery shutdown script working!
I didn't even need to use a GPIO it's all scripted to take the measurements from the fuel gauge board over the I2C bus. I've even got it all running in just one script now.
Because I can take accurate battery measurements now I can script actions at any voltage and I can trigger different led flashes for low battery and critical battery as well as the shutdown.
That's next.
(along with play and pause toggle on the main button now that Bob made media keys work)
------
pi@raspberrypi ~ $ sudo ./gpio.py
Battery OK: 3.8119 volts
Battery OK: 3.8119 volts
Battery OK: 3.8119 volts
Battery Lo: 3.7045 volts - Checking for 20 seconds: 0
Battery Lo: 3.7045 volts - Checking for 20 seconds: 1
Battery Lo: 3.7045 volts - Checking for 20 seconds: 2
Battery Lo: 3.7032 volts - Checking for 20 seconds: 3
Battery Lo: 3.7032 volts - Checking for 20 seconds: 4
Battery Lo: 3.6996 volts - Checking for 20 seconds: 5
Battery Lo: 3.702 volts - Checking for 20 seconds: 6
Battery Lo: 3.702 volts - Checking for 20 seconds: 7
Battery Lo: 3.702 volts - Checking for 20 seconds: 8
Battery Lo: 3.702 volts - Checking for 20 seconds: 9
Battery Lo: 3.702 volts - Checking for 20 seconds: 10
Battery Lo: 3.702 volts - Checking for 20 seconds: 11
Battery Lo: 3.702 volts - Checking for 20 seconds: 12
Battery Lo: 3.7008 volts - Checking for 20 seconds: 13
Battery Lo: 3.7008 volts - Checking for 20 seconds: 14
Battery Lo: 3.7008 volts - Checking for 20 seconds: 15
Battery Lo: 3.6996 volts - Checking for 20 seconds: 16
Battery Lo: 3.6996 volts - Checking for 20 seconds: 17
Battery Lo: 3.6996 volts - Checking for 20 seconds: 18
Battery Lo: 3.6996 volts - Checking for 20 seconds: 19
Battery Lo: 3.6996 volts - Checking for 20 seconds: 20
Battery Low For More Than 20 Seconds Shutting Down:
Broadcast message from root@raspberrypi (Tue May 5 20:45:17 2015):
System halted by Low Battery Alert
The system is going down for system halt NOW!
Battery Lo: 3.6996 volts - Checking for 20 seconds: 21
Battery Low For More Than 20 Seconds Shutting Down:
Broadcast message from root@raspberrypi (Tue May 5 20:45:18 2015):
System halted by Low Battery Alert
The system is going down for system halt NOW!
Battery Lo: 3.6605 volts - Checking for 20 seconds: 22
Battery Low For More Than 20 Seconds Shutting Down:
Broadcast message from root@raspberrypi (Tue May 5 20:45:19 2015):
System halted by Low Battery Alert
The system is going down for system halt NOW!
---------
Here's the cleaned up script with the battery monitor set for 3.2v - my scripting is getting a little tidier.
#!/usr/bin/env python2.7
from time import sleep
import os
import subprocess
import RPi.GPIO as GPIO
import fuel
BUTTON1 = 17 # GPIO channel 17 - RestartMC/Shutdown/Reboot
LED1 = 26 # GPIO channel 26 - Power LED
#BATTERY1 = 23 # GPIO channel 23 - Low Battery
POWEROFF1 = 22 # GPIO channel 22 - Power off kill switch
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED1, GPIO.OUT, pull_up_down=GPIO.PUD_UP)
GPIO.output(LED1, 1)
GPIO.setup(BATTERY1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(POWEROFF1, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN)
fuel.writeAlertThreshold(5)
def button_action(BUTTON1):
print('Button press = negative edge detected on Button %s'%BUTTON1)
button_press_timer = 0
while True:
if (GPIO.input(BUTTON1) == False) : # while button is still pressed down
button_press_timer += 1 # keep counting until button is released
else: # button is released, figure out for how long
if (button_press_timer > 7) : # pressed for > 7 seconds
print "long press > 7 : ", button_press_timer
# do what you need to do before halting
subprocess.call(['sudo reboot &'], shell=True)
elif (button_press_timer > 3 < 7) : # pressed for > 3 < 7 seconds
print "medium press > 3 < 7 : ", button_press_timer
# do what you need to do before rebooting
subprocess.call(['./RestartMC20.sh & echo "Killing MC20" &'], shell=True)
elif (button_press_timer > 1 < 3) : # press for > 1 < 3 seconds
print "short press > 1 < 3 : ", button_press_timer
# do what you need to do before restarting mediacenter20
subprocess.call(['shutdown -h now "System halted by GPIO action" &'], shell=True)
button_press_timer = 0
sleep(1)
def battery_monitor():
batt_timer = 0
while True:
if (fuel.readVCell()) >= 3.2: #check battery voltage
#print("Battery OK: {0} volts".format(fuel.readVCell()))
batt_timer = 0
else: # batt is low
print "Battery Low: {0} volts".format(fuel.readVCell()), "- Checking for 20 seconds: {0}".format(batt_timer)
batt_timer += 1 # keep counting
if (batt_timer > 20) : # batt low for > 20 seconds
print "Battery Low For More Than 20 Seconds - Shutting Down: "
subprocess.call(['shutdown -h now "System halted by Low Battery Alert" &'], shell=True)
sleep(1)
GPIO.add_event_detect(BUTTON1, GPIO.FALLING, callback=button_action, bouncetime=200)
#GPIO.add_event_detect(BATTERY1, GPIO.FALLING, callback=battery_monitor, bouncetime=200)
try:
while True:
sleep(5)# sleep and do the battery monitor loop
battery_monitor()
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit