Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Onion Omega with Adafruit Power Board 500 mA, shutting down the onion when battery is now or power disconnected
import sys, os, datetime
from gpiohelper import GPIOHelper
helper = GPIOHelper()
vInPin = 12
lowBatteryPin = 8
pinToCheck = vInPin;
durationOfNotOKInMinutesBeforeShutdown = 1
# GPIO value interpretation is different for vIn and battery
pinToCheckOKValue = 1 if pinToCheck == vInPin else 0;
vInInitialValue = helper.getPin(vInPin)
pinToCheckLastValue = 1 if vInInitialValue else 0
# this check is to ensure the system is not shutdown by this script when onion was powered on in battery mode
if pinToCheck != vInPin or vInInitialValue:
while True:
if helper.getPin(pinToCheck) != pinToCheckOKValue and helper.getPin(pinToCheck) != pinToCheckLastValue:
print "lowBatteryShutdown: Power Source failure. Shutdown in {} minutes.".format(durationOfNotOKInMinutesBeforeShutdown)
elif helper.getPin(pinToCheck) != pinToCheckOKValue:
if lastOKTime + datetime.timedelta(minutes=durationOfNotOKInMinutesBeforeShutdown) < datetime.datetime.utcnow() and helper.getPin(pinToCheck) != pinToCheckLastValue:
print "lowBatteryShutdown: Shutdown system now."
os.system("halt -f")
elif helper.getPin(pinToCheck) == pinToCheckOKValue and helper.getPin(pinToCheck) != pinToCheckLastValue:
print "lowBatteryShutdown: Power Source recovered."
else:
lastOKTime = datetime.datetime.utcnow()
pinToCheckLastValue = helper.getPin(pinToCheck)
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.