Created
April 6, 2016 15:34
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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