Raspberry Pi reading a led on the meter - HOW TO.

Post Reply
Posts: 7
Joined: Wed May 21, 2014 10:31 am

Raspberry Pi reading a led on the meter - HOW TO.

Post by xsherlock » Fri Dec 05, 2014 3:33 pm

I just finished my setup and I thought I'll share on how I did it, since I think I made it in a much simpler way then using arduino board to read pulses.

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.write(meter_id + "(" + str(int(round(power)))+ "*W)")
        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_pt.write(meter_id + "(" + str(power_int)+ "*Wh)")


def main():
        while True:
        GPIO.cleanup()           # clean up GPIO on normal exit
        file_pn.write(meter_id + "(0*W)")

if __name__=='__main__':

It is an awfull hack but I'm new to python and that was my very first programing effort.
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%


Posts: 1564
Joined: Thu Aug 29, 2013 7:16 am

Re: Raspberry Pi reading a led on the meter - HOW TO.

Post by jeanmarc » Fri Dec 05, 2014 4:11 pm

Thanks for the feedback. You may put some delay to avoid intensive CPU usage.
You should also write in a ramdisk/tmpfs, it avoid burning your SD card ;)

Posts: 8
Joined: Thu Jan 21, 2016 9:41 pm

Re: Raspberry Pi reading a led on the meter - HOW TO.

Post by gdrak0 » Thu Jan 28, 2016 12:56 pm

Hello All

I use the above method of xsherlock and all working normally.
Now I get the impulses from one meter Landis + Gyr e350 and has matching 1 imp / 0.25 Wh.
Μy problem is how i can adjust the metern to know that 4000 impulse is 1 kWh

Thank you

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest