#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time

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)

try:
  while True:
      i = GPIO.input(PLD_PIN)
      if i == 0:
          print("AC Power OK")
          GPIO.output(BUZZER_PIN, 0)
      # You can remove the following comment lines (remove the # character) if you want buzzer sounds alarm.
      elif i == 1:
          print("Power Supply A/C Lost")
          GPIO.output(BUZZER_PIN, 1)
          time.sleep(0.1)
          GPIO.output(BUZZER_PIN, 0)
          time.sleep(0.1)
      time.sleep(1)

except KeyboardInterrupt:
    # here you put any code you want to run before the program
    # exits when you press CTRL+C
    print("Exiting") # print value of counter

except:
    # this catches ALL other exceptions including errors.
    # You won't get any error messages for debugging
    # so only use it once your code is working
    print("Exiting") # print value of counter

finally:
    GPIO.cleanup() # this ensures a clean exit
    print("Other error or exception occurred!")
