pvout_blink.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
#Author: Lennart Kühlmeier, Denmark Feb 28th 2014
#Count number of blinks using simple photoresistor circuit demonstrated at:
#http://learn.adafruit.com/basic-resistor-sensor-reading-on-raspberry-pi/basic-photocell-reading 
#Recalculate blinks to average power and total energy.
#Upload as consumption to pvoutput.org
 
import subprocess
from time import strftime
import RPi.GPIO as GPIO, time

t_date = str(format(strftime('%Y%m%d'))) 
f = open('/home/pi/blink/%s.txt'%t_date,'w+')
f.write('Time Watts Wh\n')
f.close()
total_e_today=0
t_date_today=t_date

while (t_date==t_date_today):
    t_date = str(format(strftime('%Y%m%d')))
    blink_count=0
    DEBUG = 1
    GPIO.setmode(GPIO.BCM)
    SYSTEMID="YOUR SYSTEM ID"
    APIKEY="YOUR API KEY"
     
    def RCtime (RCpin):
            reading = 0
            GPIO.setup(RCpin, GPIO.OUT)
            GPIO.output(RCpin, GPIO.LOW)
            time.sleep(.001) # Time to let the capacitor discharge 0.01 should be enough with 0.1 uF
            GPIO.setup(RCpin, GPIO.IN)
            # Count as fast as you can until pin 18 is again on high.
            # In darkness the resistance is high and the time until GPIO #18 is back on high is longer.
            # So this will return a higher number for "reading" since the while loop runs for longer time
            # The loop has been tested to run about 85.000 times/sec on a RPI version B
            while (GPIO.input(RCpin) == GPIO.LOW):
                    reading += 1
            return reading
    
    Measure_time = 300 
    stop = time.time()+Measure_time
    while time.time() < stop:
        blink_trig = 0 # Blink trigger to ensure that light "on" event is conted as one blink only
        while (RCtime(18)<500): # stay in loop until light is low again (blink ended) enter threshold for light value on RHS of <
            blink_trig +=1 # Increase blink_trig with one to ensure that light "on" event is conted as one blink only
            if (blink_trig==1):
                blink_count +=1
                total_e_today +=1
    Watts = float(blink_count)/10*(3600/Measure_time)
    Wh = float(total_e_today)/10
    t_time = format(strftime('%Y:%m:%d:%H:%M:%S'))
    tc_time = format(strftime('%H:%M'))
    print (('%s Watts') % Watts)
    print (('%s Wh') % Wh)
    blink_count=0

    with open('/home/pi/blink/%s.txt'%t_date, "a") as f:
        f.write(('%s %s %s\n') % (t_time, Watts, Wh))
    f.close()
        
    cmd=('curl -d "d=%s" -d "t=%s" -d "v3=%s" -d "v4=%s" -H \
    "X-Pvoutput-Apikey: %s" -H \
    "X-Pvoutput-SystemId: %s" \
    http://pvoutput.org/service/r2/addstatus.jsp'\
    %(t_date, tc_time,Wh, Watts,\
    APIKEY,SYSTEMID))
    ret = subprocess.call(cmd,shell=True)

2 thoughts on “pvout_blink.py”

  1. This program doesn’t work (anymore?).

    Error message:
    user@raspberrypi:/home/pi# python /home/pi/TESTGPIO.py
    File “/home/pi/TESTGPIO.py”, line 67

    ^
    SyntaxError: EOL while scanning string literal

    Problem is here:
    cmd=(‘curl -d “d=%s” -d “t=%s” -d “v3=%s” -d “v4=%s” -H \
    “X-Pvoutput-Apikey: %s” -H \
    “X-Pvoutput-SystemId: %s” \

    Help 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

HTML Snippets Powered By : XYZScripts.com