Skip to content

Instantly share code, notes, and snippets.

@benjaminrau
Created April 6, 2016 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminrau/1f6585591dad469aee4e0520677e91ac to your computer and use it in GitHub Desktop.
Save benjaminrau/1f6585591dad469aee4e0520677e91ac to your computer and use it in GitHub Desktop.
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