MeterN and Raspberry Pi with a pulse power meter

pd1acf
Posts: 35
Joined: Mon Jan 05, 2015 6:58 pm

Re: MeterN and Raspberry Pi with a impulse power meter

Post by pd1acf » Wed Feb 17, 2016 7:10 pm

Hi George,

From the top of my head, MeterN needs this as input :
For an electrical meter this could be like this : The main command return Watt per hour, the live command return Watt (live mode) or on/off (state mode).

This would mean in your case 4000 impulses, is 1000 W.
This would mean that the script should need an internal counter, which results in 4 internal passes, and then adds '1' towards the output file ?

If you believe this is also correct, we should be able to program that in the python script !

Let me know if i interpret this correct?


Ron

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

Re: MeterN and Raspberry Pi with a impulse power meter

Post by gdrak0 » Wed Feb 17, 2016 11:22 pm

Hi pd1acf
First of all thank you very much for your interest.
I think you got right, we want an internal counter to count as you say.
I get impulses by a counter Landis + Gyr e350 and in the manual explains that every impulse is equal to 0,25Wh.
So with an internal counter the script will be able to adapt to any pulse meter.

George

pd1acf
Posts: 35
Joined: Mon Jan 05, 2015 6:58 pm

Re: MeterN and Raspberry Pi with a impulse power meter

Post by pd1acf » Wed Feb 17, 2016 11:55 pm

George,

Can you see if the below code works for you ( change/compare the part below the 'SETUP':

Code: Select all

#### setup

GPIO.setmode(GPIO.BCM)
GPIO.setup(mygpiopin, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

signal.signal(signal.SIGINT, signal_handler)

writevar=0
counter=readvalue(keepfile)
divide_count=0

########## Main Loop

while True:

        # wait for pin going up
        GPIO.wait_for_edge(mygpiopin, GPIO.RISING)

        if divide_count == 4:
                counter+=1
                writevar+=1

                writevalue(logfile,counter)

                if writevar >= 10:
                        keepvalue(keepfile,counter)
                        writevar=0

                divide_count=0

        # and wait for pin going down
        GPIO.wait_for_edge(mygpiopin, GPIO.FALLING)

        divide_count+=1



I added a loop with the 'divide_count' variable.
I guess this should suffice the goal.
If the code seems to produce increasing output, also please verify the increment with the real Kwh meter...


Let me know how you go,

ron

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

Re: MeterN and Raspberry Pi with a impulse power meter

Post by gdrak0 » Thu Feb 18, 2016 8:28 am

Hello
I tried it and it seems to measure correctly..and I will observe throughout the day to have a better view of the measurements.
But in live power at metern I think that we should see only the current power and the value should be increased or decreased depending electrical loads.
Now I see the sum of Watts which is constantly increasing..
That's right ?

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

Re: MeterN and Raspberry Pi with a impulse power meter

Post by jeanmarc » Thu Feb 18, 2016 4:34 pm

I am a bit afraid: if you continuously write the counter on your disk, it will screw your SD. If you write in a SHM/tmpfs, you'll lost values at reboot..
:idea: ps: Read the trick use in poolmeter, look at the pooler.php job.

pd1acf
Posts: 35
Joined: Mon Jan 05, 2015 6:58 pm

Re: MeterN and Raspberry Pi with a impulse power meter

Post by pd1acf » Thu Feb 18, 2016 5:24 pm

Jean Marc,

In my case i DO read all the single outputs onto a TMPFS, and each cycle of 10 the same value is written onto a USB stick also mounted on the RPI.
This way i do not wreck the SD card.

The script accepts/requires two paths, which can be anything....

Grtz,

ron

pd1acf
Posts: 35
Joined: Mon Jan 05, 2015 6:58 pm

Re: MeterN and Raspberry Pi with a impulse power meter

Post by pd1acf » Thu Feb 18, 2016 5:31 pm

Which 'TAB' do you see that in ?
Index , Detail, Reading, Dashboard, or Infos ?


gdrak0 wrote:Hello
I tried it and it seems to measure correctly..and I will observe throughout the day to have a better view of the measurements.
But in live power at metern I think that we should see only the current power and the value should be increased or decreased depending electrical loads.
Now I see the sum of Watts which is constantly increasing..
That's right ?

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

Re: MeterN and Raspberry Pi with a impulse power meter

Post by jeanmarc » Fri Feb 19, 2016 9:58 am

Hi pd1acf,
Even 1 of 10Wh is still a lot of I/O. It's even worst with a 4 pulses per Wh meter ! :?
Check with iotop -tbod10 > io.log

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

Re: MeterN and Raspberry Pi with a impulse power meter

Post by gdrak0 » Fri Feb 19, 2016 10:04 am

Hello pd1acf

I have this indicator on the dashboard and index.. look the images

https://drive.google.com/file/d/0B79ugZ ... sp=sharing
https://drive.google.com/file/d/0B79ugZ ... sp=sharing

That I want to have from metern is I can find it every 2 -3 minutes the current power (Amps)
I'm not a programmer but below I mention a way how could this be done..

1.We will need to script the following variables

#Value for Time interval measurement of Wh .. example for 3mins =0,05 , for 5mins=0,08
# x=3mins/60mins=0,05 or x=5mins/60mins=0,08
interval_time= 0,08
#The number of phases
phases= 3
#power factor
power_factor= 0,9
#path for temp log where keep every (interval_time) the sum of Wh (Perhaps this can be done with an internal counter without having to write a log)
log_interval_Wh= /var/www/metern/int_Wh.log
#Path for log where write the value of current power(Amps)
current_power_log= /var/www/metern/current.log

2.Now if we have the above is possible to know the current value of amps and appears at metern.
Suppose we have the following situation..
Within 3 minutes have accumulated 10 kWh and have 3 phases 230 volts with power factor 0.9
The script now will do the following calculate with the value from int_Wh.log and the result will record in file current.log
10 kWh/0,08 = 125000 Watts / (3*230*0,9) = 201,2 Amps
Now the current log will put on live comand of metern
"more /var/www/metern/current.log"

Perhaps some of the above to be a much better way or metern can already do now but as I said I have no knowledge of programming.
Just give you an idea of how it could be implemented..
Thank you
George

pd1acf
Posts: 35
Joined: Mon Jan 05, 2015 6:58 pm

Re: MeterN and Raspberry Pi with a impulse power meter

Post by pd1acf » Tue Feb 23, 2016 9:02 pm

jeanmarc wrote:Hi pd1acf,
Even 1 of 10Wh is still a lot of I/O. It's even worst with a 4 pulses per Wh meter ! :?
Check with iotop -tbod10 > io.log

Jean Marc,

You are right, and using the latest value in the metern .csv file is sufficient. No need to keep these values seperate.
I adapted my script, and tested it the last couple of days.

Code: Select all

#!/usr/bin/python

import RPi.GPIO as GPIO
import datetime
import sys
import signal
import psutil
import subprocess

# CHANGE to the desired mtern path!
keepfile="/usr/share/nginx/www/metern/data/csv"

mydate=datetime.datetime.now()
todayY=mydate.strftime('%Y')
todayM=mydate.strftime('%m')
todayD=mydate.strftime('%d')
meternfile=keepfile + "/" + str(todayY) + str(todayM) + str(todayD) + ".csv"



def printusage(progname):
        print progname + ' <gpio-pin-number> <filename> <file available from metern csv files>'
        print 'Example usage: '
        print progname + ' 23 /path/to/tempfs_ramdisk /path/to/metern/csv_files'
        sys.exit(-1)

def signal_handler(signal, frame):
        GPIO.cleanup()
        sysiexit(0)


def writevalue(myworkfile,value):
        f = open(myworkfile, 'w')
        #1(12555*W)
        f.write(('1('+ str(counter)+ '*Wh)'+ '\n'))
        f.close()


def readoldmetern():
        cmd = subprocess.Popen(["tail", "-1", meternfile], stdout=subprocess.PIPE)
        line, err = cmd.communicate()
        value = line.split(',' ,1 )
        return int(value[1])


######### Initialization

#### get input parameters:

try:
        mygpiopin = int(sys.argv[1])
        logfile = sys.argv[2]
        errorfile = sys.argv[2]+'.err'
except:
        printusage(sys.argv[0])


#### check for duplicate process
PROCNAME='gpio-counter.py'
procnum=0

for proc in psutil.process_iter():
        if proc.name == PROCNAME:
                if procnum >= 1:
                        writevalue(errorfile,'ERROR, process already running,exiting...')
                        sys.exit(0)
                procnum+=1


#### setup

GPIO.setmode(GPIO.BCM)
GPIO.setup(mygpiopin, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

signal.signal(signal.SIGINT, signal_handler)

writevar=0
counter=readoldmetern()

########## Main Loop

while True:
        # wait for pin going up
        GPIO.wait_for_edge(mygpiopin, GPIO.RISING)

        counter+=1
        writevar+=1

        writevalue(logfile,counter)

        # and wait for pin going down
        GPIO.wait_for_edge(mygpiopin, GPIO.FALLING)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest