Sorry, the code did not paste nicely when copied directly from Linux. I'll try again with a copy from windows.

  1. !/usr/bin/env python

import RPi.GPIO as GPIO import time import os import datetime import struct import smbus import sys


  1. Start Config

SHUTDOWN_TRIGGER = "All" # (AC Power,Capacity,Voltage,All) - determines what settings will trigger a shutdown. AC_LOSS_WAIT_MINUTES = 15 # Number of minutes after power loss before shutdown is issued CAPACITY_THRESHOLD = 50 # Shutdown will occur when capacity drops below the stated percentage CAPACITY_STATUS_LOW = 20 # Low capacity warning below this level VOLTAGE_THRESHOLD = 3.70 # Shutdown will occur when voltage drops below the stated percentage VOLTAGE_STATUS_LOW = 3.50 # Low valtage warning below this level TEST_MODE = "N" # (Y,N) - Will show extra output and will not perform shutdown when TEST_MODE = "Y", normal opperation TEST_MODE = "N" BUZZER_ON = "N" # (Y,N) - BUZZER_ON = "Y" and buzzer will beep when power is initially lost and right before shutdown BUZZER_SECONDS = 0.5 # Number of seconds the buzzer will sound

  1. End Config

WAIT_SECONDS = AC_LOSS_WAIT_MINUTES * 6 WAIT_STR = str(AC_LOSS_WAIT_MINUTES)

GPIO_PORT = 26 I2C_ADDR = 0x36

GPIO.setmode(GPIO.BCM) GPIO.setup(GPIO_PORT, GPIO.OUT)

PLD_PIN = 6 BUZZER_PIN = 20 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(PLD_PIN, GPIO.IN) GPIO.setup(BUZZER_PIN, GPIO.OUT) GPIO.output(BUZZER_PIN, 0) POWER_LOST_SEC = 0

def soundBuzzer(BUZZER_ON,BUZZER_SECONDS,BUZZER_PIN):

   if ( BUZZER_ON == "Y" ):
       GPIO.output(BUZZER_PIN, 1)
       time.sleep(BUZZER_SECONDS)
       GPIO.output(BUZZER_PIN, 0)

def acPower(PLD_PIN):

   AC_STATUS = "GOOD"
   i = GPIO.input(PLD_PIN)
   if ( i == 0 ):
       AC_STATUS = "GOOD"
   elif ( i == 1 ):
       AC_STATUS = "LOST"
   return AC_STATUS

def readVoltage(bus,VOLTAGE_STATUS_LOW):

   address = I2C_ADDR
   read = bus.read_word_data(address, 2)
   swapped = struct.unpack("<H", struct.pack(">H", read))[0]
   VOLTAGE = swapped * 1.25 /1000/16
   VOLTAGE_STATUS = "Good"
   if ( VOLTAGE <= VOLTAGE_STATUS_LOW ):
       VOLTAGE_STATUS = "Low"
   return VOLTAGE,VOLTAGE_STATUS

def readCapacity(bus,CAPACITY_STATUS_LOW):

   address = I2C_ADDR
   read = bus.read_word_data(address, 4)
   swapped = struct.unpack("<H", struct.pack(">H", read))[0]
   CAPACITY = swapped/256
   CAPACITY_STATUS = "Good"
   if ( CAPACITY >= 100 ):
       CAPACITY_STATUS = "Full"
   elif ( CAPACITY <= CAPACITY_STATUS_LOW ):
       CAPACITY_STATUS = "Low"
   return CAPACITY,CAPACITY_STATUS

def safeShutdown(GPIO_PORT):

   GPIO.output(GPIO_PORT, GPIO.HIGH)
   os.system('sudo shutdown -h now')
   time.sleep(3)
   GPIO.output(GPIO_PORT, GPIO.LOW)

bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)

now = datetime.datetime.now() date_time = now.strftime("%m/%d/%Y, %H:%M:%S")

AC_STATUS = acPower(PLD_PIN) VOLTAGE,VOLTAGE_STATUS = readVoltage(bus,VOLTAGE_STATUS_LOW) CAPACITY,CAPACITY_STATUS = readCapacity(bus,CAPACITY_STATUS_LOW)

print ("******************************************") print(date_time + " : System Startup") print(date_time + " : AC Power " + AC_STATUS) print(date_time + " : Battery Capacity " + CAPACITY_STATUS + " (" + str(round(CAPACITY,2)) + "%)" ) print(date_time + " : Battery Voltage " + VOLTAGE_STATUS + " (" + str(round(VOLTAGE,2)) + "V)" ) if ( TEST_MODE == "Y" ):

   print(date_time + " : Started in Test Mode, No actual shutdown will occur, program will exit after shutdown triggered.")

while True:

  1. print ("******************************************")
   now = datetime.datetime.now()
   date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
   AC_STATUS = acPower(PLD_PIN)
   VOLTAGE,VOLTAGE_STATUS = readVoltage(bus,VOLTAGE_STATUS_LOW)
   CAPACITY,CAPACITY_STATUS = readCapacity(bus,CAPACITY_STATUS_LOW)
   if ( SHUTDOWN_TRIGGER == "AC Power" or SHUTDOWN_TRIGGER == "All" ):
       if ( AC_STATUS == "GOOD" ):
           if ( POWER_LOST_SEC > 0 ):
               print(date_time + " : AC Power Restored - Shutdown cancelled")
               POWER_LOST_SEC = 0
           else:
               if ( TEST_MODE == "Y" ):
                   print(date_time + " : AC Power " + AC_STATUS)
       elif ( AC_STATUS == "LOST" ):
           if ( TEST_MODE == "Y" ):
               print(date_time + " : AC Power " + AC_STATUS)
           POWER_LOST_SEC += 1
           if ( POWER_LOST_SEC == 1 ):
               print(date_time + " : AC Power Lost - Shutdown in " + str(AC_LOSS_WAIT_MINUTES) + "  minutes")
               soundBuzzer(BUZZER_ON,BUZZER_SECONDS,BUZZER_PIN)
           if ( POWER_LOST_SEC >= WAIT_SECONDS ):
               print(date_time + " : Shutdown in progress due to AC power loss")
               soundBuzzer(BUZZER_ON,BUZZER_SECONDS,BUZZER_PIN)
               if ( TEST_MODE != "Y" ):
                   safeShutdown(GPIO_PORT)
               else:
                   print(date_time + " : Started in Test Mode, No actual shutdown will occur, program will exit.")
                   break
   if ( SHUTDOWN_TRIGGER == "Capacity" or SHUTDOWN_TRIGGER == "All" ):
       if ( CAPACITY > CAPACITY_THRESHOLD ):
           if ( TEST_MODE == "Y" ):
               print(date_time + " : Battery Capacity " + CAPACITY_STATUS + " (" + str(round(CAPACITY,2)) + "%)" )
       elif ( CAPACITY <= CAPACITY_THRESHOLD ):
           print(date_time + " : Battery Capacity " + CAPACITY_STATUS + " (" + str(round(CAPACITY,2)) + "%)" )
           print(date_time + " : Shutdown in progress due to battery Capacity below " + str(CAPACITY_THRESHOLD) + "% threshold")
           soundBuzzer(BUZZER_ON,BUZZER_SECONDS,BUZZER_PIN)
           if ( TEST_MODE != "Y" ):
               safeShutdown(GPIO_PORT)
           else:
               print(date_time + " : Started in Test Mode, No actual shutdown will occur, program will exit.")
               break
   if ( SHUTDOWN_TRIGGER == "Voltage" or SHUTDOWN_TRIGGER == "All" ):
       if ( VOLTAGE > VOLTAGE_THRESHOLD ):
           if ( TEST_MODE == "Y" ):
               print(date_time + " : Battery Voltage " + VOLTAGE_STATUS + " (" + str(round(VOLTAGE,2)) + "V)" )
       elif ( VOLTAGE <= VOLTAGE_THRESHOLD ):
           print(date_time + " : Battery Voltage " + VOLTAGE_STATUS + " (" + str(round(VOLTAGE,2)) + "V)" )
           print(date_time + " : Shutdown in progress due to battery Voltage below " + str(VOLTAGE_THRESHOLD) + "V threshold")
           soundBuzzer(BUZZER_ON,BUZZER_SECONDS,BUZZER_PIN)
           if ( TEST_MODE != "Y" ):
               safeShutdown(GPIO_PORT)
           else:
               print(date_time + " : Started in Test Mode, No actual shutdown will occur, program will exit.")
               break
   time.sleep(10)

GPIO.cleanup()