Create a gist now

Instantly share code, notes, and snippets.

@fvdbosch /powerlogger.py Secret
Last active Feb 9, 2018

Embed
What would you like to do?
Power Outage Monitor with PiJuice and PaPiRus
#!/usr/bin/env python
import os
import RPi.GPIO as GPIO
from pijuice import PiJuice
from papirus import PapirusTextPos
from time import sleep
import datetime
# Run as root
user = os.getuid()
if user != 0:
print "Please run script as root"
exit()
# Set up GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.IN)
GPIO.setup(26, GPIO.IN)
GPIO.setup(20, GPIO.IN)
GPIO.setup(21, GPIO.IN)
pijuice = PiJuice(1,0x14)
display = PapirusTextPos(rotation=180)
display.partialUpdates = True
previousMenu = 0
currentMenu = 1
currentPowerStatus = "a"
previousPowerStatus = "b"
interruption = ""
currentWifiStatus = True
previousWifiStatus = True
logfile = "/home/pi/poweroutage.csv"
def my_callback(channel):
global previousMenu, currentMenu, currentWifiStatus
previousMenu = currentMenu
if(channel == 16):
initMenuOne()
currentMenu = 1
elif(channel == 26):
currentMenu = 0
currentWifiStatus = not currentWifiStatus
initMenuTwo()
currentMenu = 2
elif(channel == 20):
currentMenu = 3
elif(channel == 21):
currentMenu = 4
def initMenuOne():
display.Clear()
display.RemoveText("Wifi")
display.RemoveText("IP")
display.AddText("", 10, 10, Id="Power")
display.AddText("", 10, 60, 50, Id="Timer")
def initMenuTwo():
display.Clear()
display.RemoveText("Power")
display.RemoveText("Timer")
display.AddText("", 10, 10, Id="Wifi")
display.AddText("", 10, 30, Id="IP")
def checkPower():
global currentPowerStatus
status = pijuice.status.GetStatus()
status = status['data']['powerInput5vIo']
if status == "PRESENT":
currentPowerStatus = "PRESENT"
elif status == "NOT_PRESENT":
currentPowerStatus = "ABSENT"
else:
currentPowerStatus = "ERROR"
return currentPowerStatus
def updatePower(status):
if currentPowerStatus != previousPowerStatus or currentMenu != previousMenu:
display.UpdateText("Power", "Power: " + status)
def updateTimer(status):
global interruption
if status == "PRESENT":
content = ""
elif currentPowerStatus != previousPowerStatus and status == "ABSENT":
interruption = datetime.datetime.now().replace(microsecond=0)
content = "00:00:00"
else:
now = datetime.datetime.now().replace(microsecond=0)
delta = now - interruption
content = "%.2d:%.2d:%.2d" % (delta.seconds//3600,(delta.seconds//60)%60, delta.seconds%60)
if currentPowerStatus != previousPowerStatus or status == "ABSENT":
display.UpdateText("Timer", content)
def updateLog(status, file):
if currentPowerStatus != previousPowerStatus:
currentTime = datetime.datetime.now().strftime('%d-%m-%Y %H:%M:%S')
data = "{var1},{var2}".format(var1=currentTime, var2=status)
logData(data, file)
def logData(data, file):
os.system("echo '{var1}' >> {var2}".format(var1=data, var2=file))
def menuOne():
global previousPowerStatus
status = checkPower()
updatePower(status)
updateTimer(status)
updateLog(status, logfile)
previousPowerStatus = currentPowerStatus
def menuTwo():
global previousWifiStatus
status = ""
ipaddress = ""
if previousWifiStatus != currentWifiStatus:
if currentWifiStatus:
os.system("sudo ifconfig wlan0 down")
status = "OFF"
display.UpdateText("IP", "")
else:
os.system("sudo ifconfig wlan0 up")
status = "ON"
while ipaddress == "":
ipaddress = os.popen("ifconfig wlan0 | grep 'inet ' | awk -F: '{print $1}' | awk '{print $2}'").read()
sleep(1)
display.UpdateText("IP", "IP: " + ipaddress)
display.UpdateText("Wifi", "Wifi: " + status)
previousWifiStatus = currentWifiStatus
def menuThree():
print 3
def menuFour():
print 4
def main():
my_callback(16)
while(1):
if currentMenu == 1:
menuOne()
elif currentMenu == 2:
menuTwo()
elif currentMenu == 3:
menuThree()
elif currentMenu == 4:
menuFour()
sleep(0.1)
end
# Wait for button presses
GPIO.add_event_detect(16, GPIO.FALLING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(26, GPIO.FALLING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(20, GPIO.FALLING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(21, GPIO.FALLING, callback=my_callback, bouncetime=300)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment