I used a photo resistor circuit that I found here https://github.com/kieranc/power
You don't have to follow the rest of the kieranc project, as it is old and now python has GPIO driven interupts so you do not need his C code prosthetic. Just take the photo resistor circuit.
https://github.com/kieranc/power/blob/m ... ematic.jpg
I then turned it into the sensor with a bit of sugru. https://www.dropbox.com/s/hp3kfv0bmy234 ... R.jpg?dl=0
And hooked up to the RPi https://www.dropbox.com/s/sa89fv56f7q90 ... R.jpg?dl=0 (that it wrongly conected to the RPi on this particular photo)
Then a bit of python script does the reading.
Code: Select all
import time,os from datetime import datetime import RPi.GPIO as GPIO timeprev = datetime.now() timenow = datetime.now() file_pn = open("/var/www/metern/comapps/power_now.txt","r+") file_pt = open("/var/www/metern/comapps/power_total.txt","r+") meter_id = "2" def impuls (pin): global timenow global timeprev global file_pn timenow = datetime.now() duration = timenow - timeprev div = 0.0 power = 0.0 div = float(duration.seconds)+(float(duration.microseconds)/1000000) power = 7200/div #print "flashed:" , timenow.strftime('%s.%f'), " sec:" , div, "power:" , int(round(power)), "W" file_pn.seek(0) file_pn.write(meter_id + "(" + str(int(round(power)))+ "*W)") file_pn.truncate() timeprev = timenow file_pc = open("/var/www/metern/comapps/power_count.txt","r+") power_count = file_pc.readline() #print power_count power_int = int(power_count) + 2 #print power_int file_pc.seek(0) file_pc.write(str(power_int)) file_pc.truncate() file_pc.close() file_pt.seek(0) file_pt.write(meter_id + "(" + str(power_int)+ "*Wh)") file_pt.truncate() GPIO.setmode(GPIO.BCM) GPIO.setup(4,GPIO.IN) GPIO.add_event_detect(4,GPIO.FALLING,bouncetime=200) GPIO.add_event_callback(4,impuls) def main(): try: while True: time.sleep(1) finally: GPIO.cleanup() # clean up GPIO on normal exit file_pn.seek(0) file_pn.write(meter_id + "(0*W)") file_pn.truncate() file_pn.close() file_pt.close() if __name__=='__main__': main()
I do write every pulse to file over and over. This is the way I pass the variables to metern. I'm sure that can by done in a more efficient way. But it works. (Is constant writing to SD card is not going to kill it?)
If you want to reuse the script make sure that you create the 3 files mentioned and fill the power_count.txt with some number as initial readout. Just put 0 in a file and it is fine. chown the files so the www user can read them.
Then in metern I just simply cat the file that holds the up to date readout in a file, by puting a live command as "cat /var/www/metern/comapps/power_now.txt"
To my supprise that works very nice and I do not see it hoging the CPU to much as my RPI with 123solar and metern sits at 7%