#!/usr/bin/env python

from requests import get
from requests.exceptions import Timeout, ConnectionError
from time import time, strftime, asctime, sleep
from sys import stdout
from subprocess import check_output
import RPi.GPIO as GPIO

try:
	from config import *
except ImportError:
	from defaults import *
	print 'Warning! copy defaults.py to config.py and edit that file!'


nTimesPinRising = 0
def pinRisingCallback(pin):
	global nTimesPinRising
	nTimesPinRising += 1


def waitForLedFlashWithCallback():
	global nTimesPinRising
	n = nTimesPinRising
	while n == nTimesPinRising:	#let's hope this does not get optimized away
		sleep(0.2)


def waitForLedFlash():
	while GPIO.input(ldr_gpio_pin) == GPIO.LOW:	#Wait for a pin rising
		sleep(0.00001) #minimal sleep
	sleep(0.2)	#debounce sleep
	while GPIO.input(ldr_gpio_pin) != GPIO.LOW:	#Make really really sure we get a LOW here
		sleep(0.00001) #minimal sleep


def	main():
	#simply connect ldr_gpio_pin to 5V because we use a pulldown resistor from software

	GPIO.setmode(GPIO.BCM)
	GPIO.setup(ldr_gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
	#GPIO.add_event_detect(ldr_gpio_pin, GPIO.RISING, pinRisingCallback, 200)
	waitForLedFlash()	#Skip first led flash to get a proper duration for the first one we'll use

	lastPvOutputTime = lastLedFlashTime = time()	#first impression duration will be inaccurate
	nLedFlashes = 0

	while True:
		waitForLedFlash()

		now = time()
		current_usage = '%s : %4d Watt' % (asctime(), 3600 / (now - lastLedFlashTime))
		lastLedFlashTime = now
		nLedFlashes += 1

		print current_usage
		try:
			if webcache_enabled:
				get('http://127.0.0.1:8083/watt/' + current_usage, timeout=1.0)	#update webcache
		except ConnectionError:
			print 'Warning: webcache update failed'
		except Timeout:
			print 'Warning: webcache update timed out'

		if pvoutput_interval and now >= lastPvOutputTime + pvoutput_interval:
			watt_average = nLedFlashes * 3600 / (now - lastPvOutputTime)
		 	#print 'Watt Average %d' % watt_average
			payload = {
				'key' : pvoutput_key,
				'sid' : pvoutput_sid,
				'd'   : strftime('%Y%m%d'),
				't'   : strftime('%H:%M'),
				'v4'  : watt_average
				}
			try:
				get('http://pvoutput.org/service/r2/addstatus.jsp', params=payload, timeout=5.0)
			except ConnectionError:
				print 'Warning: pvoutput update failed'
			except Timeout:
				print 'Warning: pvoutput timed out'

			lastPvOutputTime = now
			nLedFlashes = 0

		stdout.flush()


if __name__ == '__main__':
	main()
	#GPIO.cleanup()

@@ -375,273 +375,8 @@
 '%0A%0A%0A
-nTimesPinRising = 0%0Adef pinRisingCallback(pin):%0A%09global nTimesPinRising%0A%09nTimesPinRising += 1%0A%0A%0Adef waitForLedFlashWithCallback():%0A%09global nTimesPinRising%0A%09n = nTimesPinRising%0A%09while n == nTimesPinRising:%09#let's hope this does not get optimized away%0A%09%09sleep(0.2)%0A%0A%0A
 def 
@@ -504,16 +504,17 @@
 leep(0.2
+5
 )%09#debou
@@ -767,16 +767,16 @@
 IO.BCM)%0A
+
 %09GPIO.se
@@ -834,83 +834,8 @@
 WN)%0A
-%09#GPIO.add_event_detect(ldr_gpio_pin, GPIO.RISING, pinRisingCallback, 200)%0A
 %09wai

Remove currently unusable interrupt/callback stuff