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-03-24 10:56:28 +01:00
|
|
|
|
|
|
|
|
2015-04-05 13:03:56 +02:00
|
|
|
def trans():
|
|
|
|
queuelast = time.time()
|
2015-03-24 10:56:28 +01:00
|
|
|
while True:
|
2015-04-05 13:03:56 +02:00
|
|
|
queuedata = powerqueue.get()
|
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 ((queuetime - queuelast) >= 5):
|
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-04-05 13:17:50 +02:00
|
|
|
print("request https://strom.ccc-ffm.de:2342/get.php => "+repr(payload))
|
|
|
|
r = requests.post("https://strom.ccc-ffm.de:2342/get.php", data=payload, verify=False, auth=('CCC', 'Freundschaft'))
|
2015-04-05 13:03:56 +02:00
|
|
|
if sys.stdout.isatty():
|
|
|
|
print(r)
|
|
|
|
queuelast = queuetime
|
2015-03-24 10:56:28 +01:00
|
|
|
|
|
|
|
|
2015-04-05 13:03:56 +02:00
|
|
|
def readgpio():
|
2015-04-08 13:41:58 +02:00
|
|
|
gpio = open("/sys/class/gpio/gpio24/value", "r")
|
|
|
|
gpiopoll = poll()
|
|
|
|
gpiopoll.register(gpio, POLLERR)
|
|
|
|
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....")
|
|
|
|
gpioevent = gpiopoll.poll()
|
|
|
|
last = time.time()
|
|
|
|
gpio.read()
|
|
|
|
gpio.seek(0)
|
|
|
|
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)
|
2015-04-05 16:40:30 +02:00
|
|
|
if gpioval == '0':
|
|
|
|
now = time.time()
|
|
|
|
if ((now-last) >= 0.2):
|
|
|
|
power = round(1800 / (now-last),2)
|
|
|
|
if sys.stdout.isatty():
|
|
|
|
print("Current power consumption: " + str(power) + " Watt")
|
|
|
|
print(repr(gpioval))
|
|
|
|
powerqueue.put([now, power])
|
|
|
|
last = now
|
|
|
|
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__":
|
|
|
|
try:
|
|
|
|
if not os.path.exists("/sys/class/gpio/gpio24"):
|
|
|
|
gpioinit = open("/sys/class/gpio/export", "w")
|
|
|
|
gpioinit.write("24\n")
|
|
|
|
gpioinit.close()
|
|
|
|
gpiopin = open("/sys/class/gpio/gpio24/direction", "w")
|
|
|
|
gpiopin.write("in")
|
|
|
|
gpiopin.close()
|
|
|
|
gpiotype = open("/sys/class/gpio/gpio24/edge", "w")
|
|
|
|
gpiotype.write("falling")
|
|
|
|
gpiotype.close()
|
|
|
|
except:
|
|
|
|
sys.stderr.write("can't initialize gpio interface\n")
|
|
|
|
sys.stderr.flush()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
powerqueue = queue.Queue()
|
2015-03-24 10:56:28 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
while True:
|
2015-04-08 13:41:58 +02:00
|
|
|
time.sleep(1)
|