Page 2 of 5
Re: Using GPIO to measure energy
Posted: Monday 15 December 2014 19:23
by ThinkPad
Yes, i think it is a problem caused by Domoticz. The Raspberry itself is more than fast enough, see this project:
http://www.hyggeit.dk/2013/04/super-che ... power.html
I connected the photodiode to the Raspberry, and when moving a flashlight slowly back and forth over the diode, i clearly saw the graph in Domoticz go up and down (switch on and off). However, when mounted to the kWh-meter, it only writes 'off' to the log (i have 'gpio edge 17 both' to have both rising/falling). So i think it has something to do with the 'driver' that Domoticz uses for reading the GPIO port.
And i already have a YouLess, but i also want to read gas. And sell the YouLess, because i think with two cheap modules (photodiode for kWh & TCRT5000 IR for gas) it can be done directly on GPIO.
But i am not skilled enough on going further to fix this now

Using GPIO to measure energy
Posted: Tuesday 16 December 2014 21:21
by hansrune
Here is a working approach based on using a small C program with an interrupt handler. Interrupt handlers should have no problems keeping up with most meter pulse counters.
All I use in terms of hardware is a 3.3k pull-up resistor for my GPIO input pin. The counter end is an optical coupler (applies to most S0 interfaces). Maybe even the GPIO pullup is sufficient
In reverse order of operation:
Step 1: Create a dummy RFXmeter counter device. Under the devices, make note of the device index (117 in my setup)
Step 2: I used a LUA time based script to feed this dummy device from a
memory based file named
/var/run/shm/isrcounter1 . The script is available from
http://hansrune.net/domo/script_time_energy.lua. You will need to adapt this to your device index from step 1.
Step 3: The interrupt handler is adapted from the wiringPi samples. Under
wiringPi/examples, add the C program isrcounter1.c. I also added an output for a feedback LED in series with a current-limiting resistor. The LED output changes every other pulse received. Adjust pin numbers as needed in the C program, then
make isrcounter1. Copy the compiled code to
/usr/local/bin/isrcounter1. Source code can be downloaded from
http://hansrune.net/domo/isrcounter1.c
Step 4: Add a system startup / service script to
/etc/init.d/isrcounter. You can download my version from
http://hansrune.net/domo/isrcounter
Step 5: Wire your input signal, and optionally the feedback output
Step 6: Under domoticz application settings, adjust the number of pulses and costs per unit of consumption
One of the nice features is that you can adjust the displayed value to match your real meter reading. Stop the isrcounter service first. If your meter reads 128192 and there are 500 pulses per unit, do this:
Code: Select all
expr 128192 '*' 500 > /var/log/isrcounter1
... then start the isrcounter service
Hope that gives your enough details to try it out.
Re: Using GPIO to measure energy
Posted: Tuesday 16 December 2014 21:25
by ThinkPad
Wow that is really cool hansrune! Thank you very much!
I am going to try it very soon, when i have some spare time to give a detailed look at it.
Can i just use my module i mentioned, to supply a falling (going to 0V at pulse) signal to the GPIO ?
And the 128192, is that just 128192, or 128.192 (with decimals) ?
Re: Using GPIO to measure energy
Posted: Tuesday 16 December 2014 22:05
by hansrune
Interrupts can be triggered on rising and falling edge , so yes for to 0
This is set up in the C program:
wiringPiISR (IN_PIN, INT_EDGE_FALLING, &myInterrupt);
128192 was just an arbitrary whole number. Most meters have from 100 to 10000 pulses per units, and domoticz will use the value in the RAM file divided by the per units pulse settings in the application settings. The C program reads the RAM file value during startup, and that file must contain only the total number of pulses
Re: Using GPIO to measure energy
Posted: Saturday 27 December 2014 15:30
by sincze
hansrune wrote:Here is a working approach based on using a small C program with an interrupt handler. Interrupt handlers should have no problems keeping up with most meter pulse counters.
All I use in terms of hardware is a 3.3k pull-up resistor for my GPIO input pin. The counter end is an optical coupler (applies to most S0 interfaces). Maybe even the GPIO pullup is sufficient
......
Hope that gives your enough details to try it out.
Hi Hans.
Great work. After one day of work, not succeeding with GPIO7 on header pin 7 I switched to:
HeaderPin 1: 3,3 volt
HeaderPin 9: Ground
HeaderPin 12: GPIO1
Pulses are being written to the file now.
I had to change "USERNAME=pi" in the startup to USERNAME=root to have it start at boot time.
Now I've to modigy Monit so it will keep an eye on your process.
Again, many tmx.
Re: Using GPIO to measure energy
Posted: Sunday 28 December 2014 20:16
by sincze
hansrune wrote:Interrupts can be triggered on rising and falling edge , so yes for to 0
This is set up in the C program:
wiringPiISR (IN_PIN, INT_EDGE_FALLING, &myInterrupt);
128192 was just an arbitrary whole number. Most meters have from 100 to 10000 pulses per units, and domoticz will use the value in the RAM file divided by the per units pulse settings in the application settings. The C program reads the RAM file value during startup, and that file must contain only the total number of pulses
Seems I have a little problem.
- Used to Scotch Magic tape to reduce reflection.
- What happens, Light is off.... 1 interrupt, 1 interrupt and the light goes on.
- And then, 1 interrupt, 1 interrupt and the light goes off.
So 2 interrupts before the light goes on and 2 before off. Any ideas how I can correct this in the interrupt .c ?
Keep in mind that the water meter might stop after 2 interrupts. Keeping the light on a long time
Suggestions are welcome.
Re: Using GPIO to measure energy
Posted: Thursday 01 January 2015 23:26
by webster
i also
are trying to get this working
i converted a eneco metersensor to do the job
i have pulses on the input now but i need help how to compile the c program and putting it all up
can someone help me with the shell command's
many thanks in advance
Re: Using GPIO to measure energy
Posted: Friday 02 January 2015 10:41
by sincze
webster wrote:i also
are trying to get this working
i converted a eneco metersensor to do the job
i have pulses on the input now but i need help how to compile the c program and putting it all up
can someone help me with the shell command's
many thanks in advance
Do you mean:
Code: Select all
gcc -o isrcounter2 isrcounter2.c -lwiringPi
This will compile the isrcounter2.c file into a isrcounter2 executable.
Re: Using GPIO to measure energy
Posted: Friday 02 January 2015 11:16
by webster
Thanks
got it compiled
and the script is running
i got some double counts when i trigger the sensor so that is the next challange

Re: Using GPIO to measure energy
Posted: Friday 02 January 2015 14:57
by sincze
webster wrote:Thanks
got it compiled
and the script is running
i got some double counts when i trigger the sensor so that is the next challange

See also this post:
http://www.domoticz.com/forum/viewtopic ... =20#p29971
I did find some reference material for the tcrt5000 sensor. Maybe you have to change the iterrupt.
Do you know how many double counts you have?
Re: Using GPIO to measure energy
Posted: Friday 02 January 2015 16:06
by webster
It changes every time
some times it is 2 other times it is 4 or 16
the problem that i have at this moment is that the intensity of the led is to high so the pin voltage is not rising above 1,5 volts
so i am going to make a small opamp circuit so it triggers at 0 volts and controls a transistor to make a nice single puls to the raspberry
still work in progress
will change the interrupt to see if its getting better
Re: Using GPIO to measure energy
Posted: Friday 02 January 2015 16:45
by webster
Oke
small update
the change off interrupt in the script is perfect
now it counts ok but still 2 pulses 1 at beginning of detection 1 at end off detection
so set 2000 pulses for this moment
i am going to see if it is possible to put some debounce time in the script to fade 1 puls away
Re: Using GPIO to measure energy
Posted: Friday 02 January 2015 17:58
by sincze
webster wrote:Oke
small update
the change off interrupt in the script is perfect
now it counts ok but still 2 pulses 1 at beginning of detection 1 at end off detection
so set 2000 pulses for this moment
i am going to see if it is possible to put some debounce time in the script to fade 1 puls away
Did you experiment with RISING as well??
I have FALLING and modified the script:
First create an extra variable
Code: Select all
static volatile unsigned long globalCounter = 0 ;
static volatile unsigned long interrupt_correction_value = 0 ;
Then modify the function:
Code: Select all
void myInterrupt (void)
{
pullUpDnControl(IN_PIN, PUD_UP);
globalCounter++ ;
if (interrupt_correction_value == 1)
{
globalCounter-- ;
interrupt_correction_value = 0;
}
else
{
interrupt_correction_value++ ;
}
delay( 1000 ) ;
}
If all goes well you should now be ready.
Re: Using GPIO to measure energy
Posted: Friday 02 January 2015 18:14
by webster
I got the following error during compile
pi@domoticzpi ~ $ gcc -o isrcount.sh isrcounter1.c -l wiringPi
isrcounter1.c:75:2: error: invalid preprocessing directive #{
isrcounter1.c:77:4: error: invalid preprocessing directive #++
isrcounter1.c:79:4: error: invalid preprocessing directive #digitalWrite
isrcounter1.c:80:2: error: invalid preprocessing directive #}
pi@domoticzpi ~ $
stupid me was thinking in other compiler software
corrected the mistake and yes nice pulses now
Re: Using GPIO to measure energy
Posted: Friday 02 January 2015 18:47
by sincze
webster wrote:I got the following error during compile
pi@domoticzpi ~ $ gcc -o isrcount.sh isrcounter1.c -l wiringPi
isrcounter1.c:75:2: error: invalid preprocessing directive #{
isrcounter1.c:77:4: error: invalid preprocessing directive #++
isrcounter1.c:79:4: error: invalid preprocessing directive #digitalWrite
isrcounter1.c:80:2: error: invalid preprocessing directive #}
pi@domoticzpi ~ $
stupid me was thinking in other compiler software
corrected the mistake and yes nice pulses now
Great!
Keep me posted about the results

Re: Using GPIO to measure energy
Posted: Friday 02 January 2015 18:56
by webster
Did a few tests
and 1 puls per 1 litre so this is working fine now
only problem that i have i don't know how to stop the script so i can update the value
other thing i have seen is that when i reboot the value in the counter file is reset to 0
so at reboot your meter reads 0,00 again
Re: Using GPIO to measure energy
Posted: Friday 02 January 2015 19:43
by sincze
webster wrote:Did a few tests
and 1 puls per 1 litre so this is working fine now
only problem that i have i don't know how to stop the script so i can update the value
other thing i have seen is that when i reboot the value in the counter file is reset to 0
so at reboot your meter reads 0,00 again
No worries
Scoll upwards:
"Step 4: Add a system startup / service script to /etc/init.d/isrcounter. You can download my version from
http://hansrune.net/domo/isrcounter"
Place the script in /etc/init.d
Now you can start / stop and check the status of the counter process.

Re: Using GPIO to measure energy
Posted: Monday 05 January 2015 0:32
by mbruijn
I've followed thesteps above and the script is counting something as I can see the value in /var/run/shm/isrcounter1 is changing. I don't know what I've done wrong, but the value doesn't seem te be read by the lua script. Th RFX meter still shows "0" while the file shows "618". Do I need to make the lua script executable of change file permissions to make this work?
What does the 618 mean? What kind of value is this? When I watch the status of isrcounter I get:
Code: Select all
pi@domoticzpi ~ $ sudo isrcounter1 status
Waiting ... Done. counter: 36: 0
Waiting ... Done. counter: 42: 36
Waiting ... Done. counter: 85: 6
Waiting ... Done. counter: 109: 43
Waiting ... Done. counter: 169: 24
Waiting ... Done. counter: 195: 63
Waiting ... Done. counter: 202: 23
Waiting ... Done. counter: 242: 7
Waiting ... Done. counter: 258: 40
Waiting ... Done. counter: 297: 16
Waiting ... Done. counter: 300: 39
Waiting ... Done. counter: 328: 3
Waiting ... Done. counter: 332: 28
Waiting ... Done. counter: 361: 4
Waiting ... Done. counter: 364: 29
Waiting ... Done. counter: 404: 3
Waiting ... Done. counter: 417: 40
Waiting ... Done. counter: 444: 13
Waiting ... Done. counter: 478: 27
Waiting ... Done. counter: 481: 34
Waiting ... Done. counter: 511: 3
Waiting ... Done. counter: 521: 30
Waiting ... Done. counter: 523: 10
Waiting ... Done. counter: 531: 2
Waiting ... Done. counter: 559: 8
Waiting ... Done. counter: 583: 28
Waiting ... Done. counter: 613: 24
I also tried to change the meter value in the file with
Code: Select all
expr 15699 '*' 100 > /var/run/shm/isrcounter1
but I get
Code: Select all
-bash: /var/run/shm/isrcounter1: Permission denied
. With sudo I get no error, but the file doesn't change. I want to change the value to 15699,98m3, dis I use the correct code? My meter make 100 rotations per m3.
Re: Using GPIO to measure energy
Posted: Monday 05 January 2015 20:30
by sincze
mbruijn wrote:I've followed thesteps above and the script is counting something as I can see the value in /var/run/shm/isrcounter1 is changing. I don't know what I've done wrong, but the value doesn't seem te be read by the lua script. Th RFX meter still shows "0" while the file shows "618". Do I need to make the lua script executable of change file permissions to make this work?
What does the 618 mean? What kind of value is this? When I watch the status of isrcounter I get:
Code: Select all
pi@domoticzpi ~ $ sudo isrcounter1 status
Waiting ... Done. counter: 36: 0
Waiting ... Done. counter: 42: 36
Waiting ... Done. counter: 85: 6
Waiting ... Done. counter: 109: 43
Waiting ... Done. counter: 169: 24
Waiting ... Done. counter: 195: 63
Waiting ... Done. counter: 202: 23
Waiting ... Done. counter: 242: 7
Waiting ... Done. counter: 258: 40
Waiting ... Done. counter: 297: 16
Waiting ... Done. counter: 300: 39
Waiting ... Done. counter: 328: 3
Waiting ... Done. counter: 332: 28
Waiting ... Done. counter: 361: 4
Waiting ... Done. counter: 364: 29
Waiting ... Done. counter: 404: 3
Waiting ... Done. counter: 417: 40
Waiting ... Done. counter: 444: 13
Waiting ... Done. counter: 478: 27
Waiting ... Done. counter: 481: 34
Waiting ... Done. counter: 511: 3
Waiting ... Done. counter: 521: 30
Waiting ... Done. counter: 523: 10
Waiting ... Done. counter: 531: 2
Waiting ... Done. counter: 559: 8
Waiting ... Done. counter: 583: 28
Waiting ... Done. counter: 613: 24
I also tried to change the meter value in the file with
Code: Select all
expr 15699 '*' 100 > /var/run/shm/isrcounter1
but I get
Code: Select all
-bash: /var/run/shm/isrcounter1: Permission denied
. With sudo I get no error, but the file doesn't change. I want to change the value to 15699,98m3, dis I use the correct code? My meter make 100 rotations per m3.
Ok 618 is the amount of interrupts the sensor noticed and incremented the counter.
If I look at:
Code: Select all
Waiting ... Done. counter: 559: 8
Waiting ... Done. counter: 583: 28
Waiting ... Done. counter: 613: 24
I would have expected 613 to be the number, but this can be a timing issue before you posted on the forum

.
Are you using tcrt5000?
It seems a lot of interrupts in a small time frame
Code: Select all
Waiting ... Done. counter: 559: 8
Waiting ... Done. counter: 583: 28
Waiting ... Done. counter: 613: 24
That is 8,28,24.
Did you adjust the script with:
Code: Select all
void myInterrupt (void)
{
pullUpDnControl(IN_PIN, PUD_UP);
globalCounter++ ;
if (interrupt_correction_value == 1)
{
globalCounter-- ;
interrupt_correction_value = 0;
}
else
{
interrupt_correction_value++ ;
}
delay( 1000 ) ;
}
The delay will cause only 1 interrupt per second. More than enough for the watermeter.
if you are unable to update the file isrcounter1, what happens if you
and then create a new file
followed by:
Code: Select all
echo 'actual meter value' > isrcounter1
Does your domoticz log show:
Code: Select all
"
2015-01-05 20:29:00 LUA: isrcounter1 update to 1139
So you can see it is running?
Re: Using GPIO to measure energy
Posted: Monday 05 January 2015 20:45
by hansrune
Please note: The original startup script saves the counter to /var/log/isrcounter1 file when the service stops , and reads from the same file into /var/run/shm/isrcounter1 during startup. Hence, the correct file to update is /var/log/isrcounter1 AFTER stopping the service. Outside of the service/init script you can use the in-memory file directly. As you probably figured out, this is due to one file used for persistent storage, while the other is memory based to limit memory card write cycles and wear