powerpi/pi/power.py

157 lines
4.3 KiB
Python
Raw Normal View History

2015-04-05 13:03:56 +02:00
#!/usr/bin/env python3
2015-03-08 16:24:31 +01:00
2015-04-05 13:03:56 +02:00
from select import poll, POLLPRI, POLLIN, POLLERR
2015-03-24 10:56:28 +01:00
import sys
2015-04-05 13:03:56 +02:00
import os
2015-03-24 10:56:28 +01:00
import threading
2015-04-05 13:03:56 +02:00
import time
import queue
import requests
2015-05-02 11:16:56 +02:00
import configparser
2015-03-24 10:56:28 +01:00
2015-04-05 13:03:56 +02:00
def trans():
2015-05-02 11:16:56 +02:00
global serverconf
# build together url and headers
uri = ""
if serverconf.getboolean("ssl") == True:
uri = "https://"
else:
uri = "http://"
uri = uri + serverconf["url"] + "/get.php"
if sys.stdout.isatty():
print("Remote URI: " + uri)
2015-04-05 13:03:56 +02:00
queuelast = time.time()
2015-03-24 10:56:28 +01:00
while True:
# get value from queue (blocking)
2015-04-05 13:03:56 +02:00
queuedata = powerqueue.get()
powerqueue.task_done()
2015-04-05 13:17:50 +02:00
queuetime = int(queuedata[0])
2015-04-05 13:03:56 +02:00
queueval = str(queuedata[1])
# if more than five seconds passed since last event
2015-04-05 13:03:56 +02:00
if ((queuetime - queuelast) >= 5):
# put payload together
2015-04-05 13:17:50 +02:00
payload = {"val": str(queuetime) + ";" + str(queueval)}
2015-04-05 13:03:56 +02:00
if sys.stdout.isatty():
2015-05-02 11:16:56 +02:00
print("request " + uri + " => "+repr(payload))
# send to webserver via http post
2015-05-02 11:16:56 +02:00
#
# Um den Krempel hier muss noch mal eine While Schleife rum, damit die Daten nicht verloren gehen.
#
try:
2015-05-02 11:16:56 +02:00
if serverconf.getboolean("basicauth") == True:
r = requests.post(uri, data=payload, verify=False, auth=(serverconf["user"], serverconf["password"]), timeout=10, headers={'connection':'close'})
else:
r = requests.post(uri, data=payload, verify=False, timeout=10, headers={'connection':'close'})
except:
2015-05-02 11:16:56 +02:00
print("Exception raised!")
else:
if sys.stdout.isatty():
print("server response: " + str(r.status_code))
2015-04-05 13:03:56 +02:00
queuelast = queuetime
else:
# drop data if less than five seconds passed since last event
del queuedata
if sys.stdout.isatty():
print("Drop queued element")
2015-03-24 10:56:28 +01:00
2015-04-05 13:03:56 +02:00
def readgpio():
2015-05-02 11:16:56 +02:00
global gpiopath
global gpioconf
global smconf
# open gpio filehandle
2015-05-02 11:16:56 +02:00
gpio = open(gpiopath + "value", "r")
# setup polling
2015-04-05 13:03:56 +02:00
gpiopoll = poll()
gpiopoll.register(gpio, POLLERR)
# wait for two interrupts to have a "clean" starting point
2015-04-05 13:03:56 +02:00
if sys.stdout.isatty():
print("wait for 2 interrupts...")
gpioevent = gpiopoll.poll()
gpio.read()
gpio.seek(0)
if sys.stdout.isatty():
print("wait for 1 interrupt....")
2015-04-05 13:03:56 +02:00
gpioevent = gpiopoll.poll()
last = time.time()
2015-04-05 13:03:56 +02:00
gpio.read()
gpio.seek(0)
# start readgpio mainloop
2015-04-05 13:03:56 +02:00
if sys.stdout.isatty():
print("start readgpio mainloop")
while True:
gpioevent = gpiopoll.poll()
gpioval = gpio.read(1)
2015-04-05 13:03:56 +02:00
gpio.seek(0)
# check if interrupt was a falling edge (yes, really!)
if gpioval == '0':
now = time.time()
# plausibility check
2015-05-02 11:16:56 +02:00
if ((now-last) >= gpioconf.getfloat("mintime")):
# calculate current power consumption
2015-05-02 11:16:56 +02:00
power = round((3600000/smconf.getint("impkwh")) / (now-last),2)
if sys.stdout.isatty():
print("Current power consumption: " + str(power) + " Watt")
print("GPIO state: " + gpioval)
# put measured value on queue
powerqueue.put([now, power])
last = now
# error if a tty is connected and a raising edge was triggered
else:
if sys.stdout.isatty():
print("ERROR: not a falling edge! Ignoring interrupt")
2015-03-24 10:56:28 +01:00
2015-04-05 13:03:56 +02:00
if __name__ == "__main__":
2015-05-02 11:16:56 +02:00
# read config
conf = configparser.ConfigParser()
conf.read("powerpi.conf")
conf.sections()
gpioconf = conf['gpio']
serverconf = conf['server']
smconf = conf['smartmeter']
if serverconf["url"] == "power.example.com":
print("FATAL: configuration not adapted! Aborting!")
exit(1)
gpiopath = "/sys/class/gpio/gpio" + gpioconf["port"] + "/"
# initialise gpio interfaces
2015-04-05 13:03:56 +02:00
try:
2015-05-02 11:16:56 +02:00
if not os.path.exists(gpiopath):
# if not already exported, export gpio port
2015-04-05 13:03:56 +02:00
gpioinit = open("/sys/class/gpio/export", "w")
2015-05-02 11:16:56 +02:00
gpioinit.write(gpioconf["port"] + "\n")
2015-04-05 13:03:56 +02:00
gpioinit.close()
2015-05-02 11:16:56 +02:00
# set direction of gpio port to "IN"
gpiopin = open(gpiopath + "direction", "w")
2015-04-05 13:03:56 +02:00
gpiopin.write("in")
gpiopin.close()
# set trigger to falling edge
2015-05-02 11:16:56 +02:00
gpiotype = open(gpiopath + "edge", "w")
2015-04-05 13:03:56 +02:00
gpiotype.write("falling")
gpiotype.close()
except:
# if not able to initialise gpios: abort with error
2015-04-05 13:03:56 +02:00
sys.stderr.write("can't initialize gpio interface\n")
sys.stderr.flush()
sys.exit(1)
# defining queue for measured values
2015-04-05 13:03:56 +02:00
powerqueue = queue.Queue()
# disable ssl "no verification" warnings
2015-05-02 11:16:56 +02:00
if serverconf.getboolean("sslself") == True:
requests.packages.urllib3.disable_warnings()
2015-03-24 10:56:28 +01:00
# initialising and starting threads
2015-04-05 13:03:56 +02:00
th1 = threading.Thread(target=readgpio)
th2 = threading.Thread(target=trans)
2015-03-24 10:56:28 +01:00
th1.setDaemon(True)
th2.setDaemon(True)
th1.start()
2015-04-05 13:03:56 +02:00
th2.start()
2015-03-24 10:56:28 +01:00
#busy loop
2015-03-24 10:56:28 +01:00
while True:
2015-04-05 13:03:56 +02:00
time.sleep(1)