Using GPIO to measure energy

User avatar
ThinkPad
Posts: 1754
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Using GPIO to measure energy

Post by ThinkPad » Monday 15 December 2014 20:23

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 :(
ThinkTheme - theme for Domoticz
My (Dutch) blog: http://thinkpad.tweakblogs.net - My Domoticz scripts: Bitbucket
I'm not (very) active anymore on this forum as i don't use Domoticz anymore.

hansrune
Posts: 73
Joined: Monday 07 October 2013 10:51
Target OS: Raspberry Pi
Domoticz version:
Contact:

Using GPIO to measure energy

Post by hansrune » Tuesday 16 December 2014 22:21

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.
Last edited by hansrune on Saturday 10 January 2015 11:38, edited 2 times in total.

User avatar
ThinkPad
Posts: 1754
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Using GPIO to measure energy

Post by ThinkPad » Tuesday 16 December 2014 22:25

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) ?
ThinkTheme - theme for Domoticz
My (Dutch) blog: http://thinkpad.tweakblogs.net - My Domoticz scripts: Bitbucket
I'm not (very) active anymore on this forum as i don't use Domoticz anymore.

hansrune
Posts: 73
Joined: Monday 07 October 2013 10:51
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by hansrune » Tuesday 16 December 2014 23:05

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

User avatar
sincze
Posts: 988
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Using GPIO to measure energy

Post by sincze » Saturday 27 December 2014 16:30

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.
Attachments
Water_Meter.jpg
Water_Meter.jpg (316.34 KiB) Viewed 6723 times
Last edited by sincze on Friday 02 January 2015 11:42, edited 1 time in total.
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
sincze
Posts: 988
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Using GPIO to measure energy

Post by sincze » Sunday 28 December 2014 21:16

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 :D

Suggestions are welcome.
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

webster
Posts: 52
Joined: Saturday 13 December 2014 23:16
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by webster » Friday 02 January 2015 0:26

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
RPI B+
RPI 2
Fibaro motion sensors , Philips Hue, Z-Wave dimmers, Eneco Toon
Honeywell Evohome 1-Wire Wifi sensors

User avatar
sincze
Posts: 988
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Using GPIO to measure energy

Post by sincze » Friday 02 January 2015 11:41

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.
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

webster
Posts: 52
Joined: Saturday 13 December 2014 23:16
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by webster » Friday 02 January 2015 12:16

Thanks :mrgreen:


got it compiled

and the script is running

i got some double counts when i trigger the sensor so that is the next challange ;)
RPI B+
RPI 2
Fibaro motion sensors , Philips Hue, Z-Wave dimmers, Eneco Toon
Honeywell Evohome 1-Wire Wifi sensors

User avatar
sincze
Posts: 988
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Using GPIO to measure energy

Post by sincze » Friday 02 January 2015 15:57

webster wrote:Thanks :mrgreen:


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?
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

webster
Posts: 52
Joined: Saturday 13 December 2014 23:16
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by webster » Friday 02 January 2015 17:06

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 :D

will change the interrupt to see if its getting better
RPI B+
RPI 2
Fibaro motion sensors , Philips Hue, Z-Wave dimmers, Eneco Toon
Honeywell Evohome 1-Wire Wifi sensors

webster
Posts: 52
Joined: Saturday 13 December 2014 23:16
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by webster » Friday 02 January 2015 17:45

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
RPI B+
RPI 2
Fibaro motion sensors , Philips Hue, Z-Wave dimmers, Eneco Toon
Honeywell Evohome 1-Wire Wifi sensors

User avatar
sincze
Posts: 988
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Using GPIO to measure energy

Post by sincze » Friday 02 January 2015 18:58

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.
Last edited by sincze on Saturday 03 January 2015 14:33, edited 1 time in total.
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

webster
Posts: 52
Joined: Saturday 13 December 2014 23:16
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by webster » Friday 02 January 2015 19:14

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 :oops:

corrected the mistake and yes nice pulses now
RPI B+
RPI 2
Fibaro motion sensors , Philips Hue, Z-Wave dimmers, Eneco Toon
Honeywell Evohome 1-Wire Wifi sensors

User avatar
sincze
Posts: 988
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Using GPIO to measure energy

Post by sincze » Friday 02 January 2015 19:47

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 :oops:

corrected the mistake and yes nice pulses now
Great!
Keep me posted about the results :D
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

webster
Posts: 52
Joined: Saturday 13 December 2014 23:16
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by webster » Friday 02 January 2015 19:56

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
RPI B+
RPI 2
Fibaro motion sensors , Philips Hue, Z-Wave dimmers, Eneco Toon
Honeywell Evohome 1-Wire Wifi sensors

User avatar
sincze
Posts: 988
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Using GPIO to measure energy

Post by sincze » Friday 02 January 2015 20:43

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.

Code: Select all

sudo isrcounter status 
:D
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

mbruijn
Posts: 4
Joined: Tuesday 11 November 2014 1:37
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by mbruijn » Monday 05 January 2015 1:32

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.

User avatar
sincze
Posts: 988
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Using GPIO to measure energy

Post by sincze » Monday 05 January 2015 21:30

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 :D .

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

Code: Select all

sudo rm isrcounter1
and then create a new file

Code: Select all

touch isrcounter1
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?
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

hansrune
Posts: 73
Joined: Monday 07 October 2013 10:51
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by hansrune » Monday 05 January 2015 21:45

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

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest