Using GPIO to measure energy

GAEvakYD
Posts: 12
Joined: Saturday 16 August 2014 13:34
Target OS: Raspberry Pi
Domoticz version:
Contact:

Using GPIO to measure energy

Post by GAEvakYD » Saturday 16 August 2014 13:47

Hi,

I'm using a Raspberry PI with GPIO board. In the past I''ve written an own c++ program to measure the current amount of energy used. This with a photoransistor and a resistor that gives a pulse when the digital meter blink. My own program has a poll interval of 10000 micro seconds (0.1 seconds). The C++ program counts the pulses and the time between the pulses, so I know the current amount of energy using.

Now I want to got this information into Domoticz, so I can use one central system.

Two questions about this:
1)I've exported the GPIO Pin and installed GPIO port as a Contact device in Domoticz, but no pulse of signal is found. I think the interval must me changed to poll more frequent. Can anyone tell me where I can found this setting?
2)I need to do some logic with the incomming pulse, to calculate the amount of energy using. Can this in Domoticz or do I need this in C++ and push that data into Domoticz?

Thanks for helping.

RobinS
Posts: 67
Joined: Friday 13 September 2013 0:24
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by RobinS » Saturday 16 August 2014 15:37

Hi,

What you are describing is basically how an S0 meter works.
1 pulse = a certain amount of energy, most common for S0 = 1 pulse = 1 watt.
In Domoticz you have settings for certain types, like Energy, nr of pulse / kwh, Water nr of pulses per m3 etc. so you can configure the pulse count your need per kwh.
Domoticz works with event messages, with different types of devices, fi. light switch, counter (the type your need).
A contact / switch type will only report level, on or off and a counter type will report pulses.
Look into the source code in the RFX / S0 / P1 meter types to see how to a counter event is formatted.

If you do not want to go to this trouble, then checkout PiFace, the domoticz PiFace driver has pulse count support, and you can use the global settings to configure your pulses per kwh.

Best Regards,

Robin
My domoticz setup:
PI [Doorbell on Audio out], DS1307 RTC, BMP085, PiFace [4* S0 meter, Doorbell In, PIR, GBS and Door sensors] and RFXCom 433 [5 HomeEasy, 2 Impluz]
2x P1 smartmeter on USB
Gammu-smsd with USB 3G modem
Andromoticz on control tablet

GAEvakYD
Posts: 12
Joined: Saturday 16 August 2014 13:34
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by GAEvakYD » Saturday 16 August 2014 21:22

Thanks for helping me Robin.

I'm in doubt about buying an PiFace or a YouLess meter. Writing a custom plugin isn't an option. Been there done that, now I want to use a more standard solution.

One of the reason's why I'm hesitation about the PiFace is because it also use the wiringPI lib. That's a cool library, but with a very frustrating bug:
https://projects.drogon.net/and-on-the- ... t-stopped/ My current C++ program got that error and I must reboot my PI to got it work correctly.

RobinS
Posts: 67
Joined: Friday 13 September 2013 0:24
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by RobinS » Saturday 16 August 2014 21:35

Hi,

Regarding the PiFace support in domoticz, it does not use WiringPi, but a custom driver (I developed it).
I use the PiFace to read 4 S0 pulse meters, and some digital inputs.
However please be aware that the Piface driver runs in user space, so it can not read high frequency counters, a few (5-6) Hz is OK, but much higher the PiFace driver will miss samples. (See the PiFace topic in the forum, I published some measurements there).

I am not familiar with YouLess meter, but only with the PiFace and its support in Domoticz, so I can not advise you on both.

Best Regards,

Robin
My domoticz setup:
PI [Doorbell on Audio out], DS1307 RTC, BMP085, PiFace [4* S0 meter, Doorbell In, PIR, GBS and Door sensors] and RFXCom 433 [5 HomeEasy, 2 Impluz]
2x P1 smartmeter on USB
Gammu-smsd with USB 3G modem
Andromoticz on control tablet

GAEvakYD
Posts: 12
Joined: Saturday 16 August 2014 13:34
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by GAEvakYD » Sunday 17 August 2014 15:08

Thanks. I think I will go for a PiFace device.

Can you show some screenshots of Domoticz with PiFace and enerycount functions?

User avatar
Keptenkurk
Posts: 440
Joined: Wednesday 21 August 2013 17:24
Target OS: -
Domoticz version:
Location: Waalre, The Netherlands
Contact:

Re: Using GPIO to measure energy

Post by Keptenkurk » Sunday 17 August 2014 15:54

At the time i didn't have a smartmeter i counted pulses from my meter with rotary disc (see here). The output is a pretty rough bargraph as you can see.
The Smart meter gives way more instantanious values like this:
energychart.jpeg
energychart.jpeg (9.62 KiB) Viewed 14471 times
(actually, you can see the fridge switching every hour)
/paul

RobinS
Posts: 67
Joined: Friday 13 September 2013 0:24
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by RobinS » Monday 18 August 2014 20:53

Here are the screenshots of of one of my S0 meters.
It does not look so fancy as the smartmeter keptenkurk showed you, but is still very usefull.

Best Regards,

Robin
Attachments
s0 logpage part2.jpg
S0 meter page part 2
s0 logpage part2.jpg (103.25 KiB) Viewed 14449 times
s0 logpage part1.jpg
S0 meter page part 1
s0 logpage part1.jpg (124.85 KiB) Viewed 14449 times
utility page.jpg
domoticz utility page
utility page.jpg (194.78 KiB) Viewed 14449 times
My domoticz setup:
PI [Doorbell on Audio out], DS1307 RTC, BMP085, PiFace [4* S0 meter, Doorbell In, PIR, GBS and Door sensors] and RFXCom 433 [5 HomeEasy, 2 Impluz]
2x P1 smartmeter on USB
Gammu-smsd with USB 3G modem
Andromoticz on control tablet

RobinS
Posts: 67
Joined: Friday 13 September 2013 0:24
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by RobinS » Monday 18 August 2014 20:54

The last image
Attachments
meter instellingen.jpg
meter settings
meter instellingen.jpg (61.72 KiB) Viewed 14450 times
My domoticz setup:
PI [Doorbell on Audio out], DS1307 RTC, BMP085, PiFace [4* S0 meter, Doorbell In, PIR, GBS and Door sensors] and RFXCom 433 [5 HomeEasy, 2 Impluz]
2x P1 smartmeter on USB
Gammu-smsd with USB 3G modem
Andromoticz on control tablet

GAEvakYD
Posts: 12
Joined: Saturday 16 August 2014 13:34
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by GAEvakYD » Monday 18 August 2014 21:16

Nice. That's looks great.

What kind of hardware did you use Robin? Specially the seperated counters for different groups are great. I want to measure the main kWh meter, but more details are welcome. Specially from my electric car of my heating pump.

RobinS
Posts: 67
Joined: Friday 13 September 2013 0:24
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by RobinS » Tuesday 19 August 2014 17:55

Meterkast_800.JPG
Meterkast_800.JPG (293.15 KiB) Viewed 14424 times
Hi,

I have the following setup: (see pictures below)
Witte groep: (Section of 3 electricity groups), monitored with one S0 meter
Blauwe groep: (Section of 3 electricity groups), monitored with one S0 meter
Groene groep: (Section of 3 electricity groups), monitored with one S0 meter
Zonnepanelen: Output of solar inverter with a S0 meter connected in reverse, so it monitors the current to the mains instead of from the mains.
Electriciteitsmeter: P1 smart meter
Gasmeter: P1 smart meter

Please be aware that the S0 meters only registers current in one direction (this is perfect for most users, but not for the ones that supply energy back to the grid).
The P1 smartmeter shows the current in both directions.

The S0 meters that I use are two different types, because I bought one later on.
As the S0 output is standardized, it basically does not matter which manufacturer you buy it from, but just make sure that they all supply the same number of pulses per kwh, as domoticz only has one divider for electricity. (mine supply 1 pulse per watt (most common), but some supply 1 pulse per 0.5 watt)
I purchased my S0 meters @ http://www.kwhmeter.nl (found them one of the cheapest suppliers).

The S0 meter output is isolated from the mains and requires at least 12V DC, keptenkurk documented (I think in the wiki or otherwise in the original PiFace thread) an interface circuit to connect the meter to the piface.

Although the S0 meters have LCD readouts, the interface only provide pulses, so the actual count can not be read. But the LCD readout is quite handy though...

Pictures of my setup:


Best Regards,

Robin
Attachments
P1_800.JPG
P1_800.JPG (278.84 KiB) Viewed 14424 times
My domoticz setup:
PI [Doorbell on Audio out], DS1307 RTC, BMP085, PiFace [4* S0 meter, Doorbell In, PIR, GBS and Door sensors] and RFXCom 433 [5 HomeEasy, 2 Impluz]
2x P1 smartmeter on USB
Gammu-smsd with USB 3G modem
Andromoticz on control tablet

GAEvakYD
Posts: 12
Joined: Saturday 16 August 2014 13:34
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by GAEvakYD » Saturday 06 September 2014 17:15

Wow very nice Robin. Sorry for the late reaction but had some serious workload for office thing. :(

In the meantime my PiFace is arrived. I've used my old setting with the phototransistor and resistor to give me the pulses to the GPIO input. I've used the Python libs to test the hardware and it works.

Code: Select all

from time import sleep
import pifacedigitalio

DELAY = 0.2  # seconds
if __name__ == "__main__":
        pifacedigital = pifacedigitalio.PiFaceDigital()
        pifacedigital.output_pins[0].turn_off()
        while True:
                print (pifacedigital.input_pins[0].value)
                sleep(DELAY)
This polls the gpio input and print a 1 when there's a pulse.

Ok, stage 1 completes. Now stage 2 to get that input into domoticz.

First I exports the input, as the manual describes. I add it to the domoticz startup script :

Code: Select all

#export pin  0 voor Domoticz space
gpio export 0 in
gpio edge 0 both
After that in Domoticz I configured the Piface hardware. So in the devices tab the GPIO input comes available as RFXmeter. I add the GPIO device with ID 0 and unit 0 with type RFXMeter. When I look into the log of that device I see only zero's, no single 1 pulse.

Do I need to change some settings to get that pulses into Domoticz? May be a poll interval? I saw something about a piface.conf file.

GAEvakYD
Posts: 12
Joined: Saturday 16 August 2014 13:34
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by GAEvakYD » Saturday 06 September 2014 17:30

Ow my piface config looks like this:
piface.0.input.0.enabled=true
piface.0.input.0.pin_type=rising
piface.0.input.0.count_enabled=false
piface.0.input.0.count_update_interval_sec=1

RobinS
Posts: 67
Joined: Friday 13 September 2013 0:24
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by RobinS » Saturday 06 September 2014 20:19

GAEvakYD wrote:Ow my piface config looks like this:
piface.0.input.0.enabled=true
piface.0.input.0.pin_type=rising
piface.0.input.0.count_enabled=false
piface.0.input.0.count_update_interval_sec=1
First of all, forget all about your Python script, as domoticz will handle the PiFace hardware by it self, so only use this.

Set your settings to:
piface.0.input.0.enabled=false
piface.0.input.0.pin_type=rising
piface.0.input.0.count_enabled=true
piface.0.input.0.count_update_interval_sec=10

This will disable the on / off switch for piface input 0 in domoticz, and enable a pulse counter on piface input 0 (note both can be enabled simulainously).
The pulse counts are reported to domoticz each 10 seconds, which is fast enough and will not overload domoticz.
All pulses that are given in the 10 second period will be counted by the piface driver and accumulated for the next reporting moment (so nothing is lost), so you could basically set it to 3600 seconds... Although I would not recommend this, because if there is power outage or domoticz is stopped all non reported counts will be lost.

Best Regards,

Robin
My domoticz setup:
PI [Doorbell on Audio out], DS1307 RTC, BMP085, PiFace [4* S0 meter, Doorbell In, PIR, GBS and Door sensors] and RFXCom 433 [5 HomeEasy, 2 Impluz]
2x P1 smartmeter on USB
Gammu-smsd with USB 3G modem
Andromoticz on control tablet

GAEvakYD
Posts: 12
Joined: Saturday 16 August 2014 13:34
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by GAEvakYD » Sunday 07 September 2014 10:43

Thanks. Looks like this works. The counter value on the device tabs begins counting at 0.17 kWh, so not 0.0. When opening the log it's empty but I think the need a few hours to build up a nice graphic.

In the settings in the RFXMeter/Counter Dividers section I changed the Energy value from 1000 to 500. This because my energymeter says 500 Imp / kw. So 2 W per pulse.

fisics
Posts: 80
Joined: Friday 12 July 2013 15:51
Target OS: -
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by fisics » Monday 08 September 2014 23:25

Hey guys,

Glad I found this thread, must of missed it earlier while searching.

After reading it seems that PiFace is the way forward to get counters to work easily with Domoticz.

I have a few simple questions...

Do you have a circuit diagram for counting pulses from an electric meter?

My gas meter is pretty close by the electric meter - can I measure this at the same time? Will it affect accuracy / performance? This will be done with a Reed switch, I'm guessing this is simple direct wiring from the reed switch to an input and 0V.

Appreciate the help!

fisics
Posts: 80
Joined: Friday 12 July 2013 15:51
Target OS: -
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by fisics » Monday 08 September 2014 23:49

Hey guys,

Is this the simple circuit diagram for the phototransistor (LDR) to PiFace?

Do you need a resistor?

http://www.piface.org.uk/assets/tom/one ... sistor.png

Thanks

GAEvakYD
Posts: 12
Joined: Saturday 16 August 2014 13:34
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by GAEvakYD » Wednesday 10 September 2014 13:56

This is my schema:
https://drive.google.com/file/d/0B3C0u0 ... sp=sharing

You can also use it with the 5v of the PiFace.

fisics
Posts: 80
Joined: Friday 12 July 2013 15:51
Target OS: -
Domoticz version:
Contact:

Re: Using GPIO to measure energy

Post by fisics » Wednesday 10 September 2014 21:33

Hi GAEvakYD,

Thanks for the circuit diagram.

Can you tell me if this is correct in wiring the circuit on the PiFace?
PiFace-LDR.png
PiFace-LDR.png (216.44 KiB) Viewed 14248 times
Also I don't have any of the BPW96B Phototransistor yet or a 7.7 Kilo Ohm resistor so I thought I'd try it with a Light Dependent Resistor GL5528 like these:

http://www.ebay.co.uk/itm/5pcs-Photores ... 35c4e594eb

I'm not sure if a Light Dependent Resistor is the same as a Phototransistor? Also it seems like 7.7 Kilo Ohm resistors aren't that common is that true (couldn't find on eBay)?

I changed the piface.conf to only:
piface.0.input.0.enabled=false
piface.0.input.0.pin_type=rising
piface.0.input.0.count_enabled=true
piface.0.input.0.count_update_interval_sec=10

It seems to be working... but I only have 1K resistor at the moment, will this break something?
PiFace-LDR.png
PiFace-LDR.png (216.44 KiB) Viewed 14248 times
Thanks again for your help and I'll do a How-To for other newbies once I get it all worked out.
Attachments
PiFace-Domoticz.png
PiFace-Domoticz.png (84.83 KiB) Viewed 14248 times

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 » Sunday 14 December 2014 0:54

Is a PiFace really needed for detecting pulses? I have aimed this module: http://img.dxcdn.com/productimages/sku_152006_1.jpg at the blinking LED of my kWh-meter and tied its digital output (0V or 3,3V) to a GPIO pin, which is configured as an input in Domoticz.

However, i noticed that Domoticz is too slow to detect the quick flashes of the pulse LED. When i hover back and forth with a flashlight over the module it detects the on/off spikes nicely. So it is just a speed issue

Is there any way to improve the speed of the detection? I don't want to buy the PiFace, because i have the idea i am already nearly there ;)

Also i found this: http://www.hyggeit.dk/2013/04/super-che ... power.html which seems to work perfect (also see the video). So it should be possible, maybe with a different GPIO driver for Domoticz ?
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.

User avatar
Conn-artist
Posts: 80
Joined: Saturday 12 July 2014 12:06
Target OS: Raspberry Pi
Domoticz version: 3.8367
Location: 127.0.0.1
Contact:

Re: Using GPIO to measure energy

Post by Conn-artist » Monday 15 December 2014 19:50

ThinkPad wrote:However, i noticed that Domoticz is too slow to detect the quick flashes of the pulse LED. When i hover back and forth with a flashlight over the module it detects the on/off spikes nicely. So it is just a speed issue
Are you sure? Interrupts are usually pretty fast, by definition - I don't know the RPi's interrupt specs but I do have experience with Arduino's interrupts and they are more than fast enough for this kind of work.

Or you could go the easy way and get a YouLess ;)
--
Martijn

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest