Using GPIO to measure energy

Moderator: leecollings

User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Using GPIO to measure energy

Post by sincze »

:D no
hansrune wrote: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
Thanks for the update. Indeed a nice safeguard for preserving the SD card. :D
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Using GPIO to measure energy

Post by sincze »

sincze wrote::D no
hansrune wrote: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
my /var/run and var/log are both kept in RAM

Code: Select all

tmpfs				38M	1.6M	36M	5%	/run
tmpfs				5.0M	0	5.0M	0%	/run/lock
tmpfs				75M	4.0K	75M	1%	/run/shm
/dev/mmcblk0p1	56M	9.7M	47M	18%	/boot
tmpfs				5.0M	816K	4.3M	16%	/var/log
tmpfs				1.0M	0	1.0M	0%	/var/tmp
In order to keep the isrcounter1 file safe after reboot I modified the script to:

Code: Select all

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
DESC="ISR pulse counter and uploader"
NAME=isrcounter1
USERNAME=root
LOGFILE="/var/run/shm/$NAME"
#SAVEFILE="/var/log/$NAME"
SAVEFILE="/home/pi/domoticz/watermeter/$NAME"
DEVIDX="172"
DAEMON="/usr/local/bin/$NAME"
DAEMON_ARGS="$LOGFILE"
SCRIPTNAME=/etc/init.d/$NAME
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Using GPIO to measure energy

Post by roblom »

I was trying to use your script but i'm stuck at Step 3.
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
* I installed WiringPi (I missed that in your steps but I assumed is is necessary :-) )
* Added the isrcounter1.c to wiringPi/examples
Then I'm lost. Where did you added it and what should I do with it (how is the hardware connected, etc.?) Besides that I don't understand I think I can skip it and not using the LED. I also use a TCRT5000 so it already uses a LED but an extra LED can be nice. I left it for a later stage.
* I adjusted the pin numbers in the C program
Then I'm lost again, as I'm no programmer and I learnt all this by searching the internet I know what compiling is but I don't know how to do it. So please enlighten me :-)
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Using GPIO to measure energy

Post by roblom »

I also have a question on step 2.
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.
So the LUA script is feeded with the memory based "/var/run/shm/isrcounter1" file. But where do i place the LUA script? In "/home/pi/domoticz/scripts/lua", is this correct?
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Using GPIO to measure energy

Post by sincze »

roblom wrote:I also have a question on step 2.
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.
So the LUA script is feeded with the memory based "/var/run/shm/isrcounter1" file. But where do i place the LUA script? In "/home/pi/domoticz/scripts/lua", is this correct?
Yes the lua scripts goes into /domoticz/scripts/lua directory.
Pretty much the only change needs to go here: meteridx=<YOUR OWN ID HERE>

Code: Select all

--
-- $Id: script_time_energy.lua,v 1.1 2014/11/06 20:12:00 pi Exp $
--
logging = true
debug = true
--

metername="isrcounter1"
meteridx=<YOUR OWN ID HERE>

--
-- No changes should be needed below here
--
countfile = "/var/run/shm/" .. metername

--
-- for i, v in pairs(devicechanged) do print("Changed Device=" ..  i .. " Value=" .. v .. "<--") end
-- for i, v in pairs(termodevidx) do print("Idx=" ..  i .. " Value=" .. v .. "<--") end
-- for i, v in pairs(otherdevices_svalues) do print("Idx=" ..  i .. " svalue=" .. v .. "<--") end
-- for i, v in pairs(otherdevices) do print("Idx=" ..  i .. " otherdevice=" .. v .. "<--") end
--

commandArray = {}
local file = io.open(countfile, "r")
if (file)
then
        metercount = file:read()
        file:close()
        if (metercount)
        then
                if (debug) then print(metername .. " update to " .. metercount ) end
                commandArray['UpdateDevice']=meteridx .. "|0|" .. metercount
        else
                if (debug) then print("No metercount from file " .. countfile) end
        end
else
        if (logging) then print("Cannot open meterfile " .. countfile) end
end
return commandArray
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Using GPIO to measure energy

Post by roblom »

sincze wrote:Yes the lua scripts goes into /domoticz/scripts/lua directory.
Pretty much the only change needs to go here: meteridx=<YOUR OWN ID HERE>
Thanks. Do you have a answer to my earlier question too?
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Using GPIO to measure energy

Post by roblom »

sincze wrote: "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
I probably do something wrong but can't find what.
When I do

Code: Select all

cd /etc/init.d
sudo isrcounter status
I get:

Code: Select all

sudo: isrcounter: command not found
User avatar
Conn-artist
Posts: 21
Joined: Saturday 12 July 2014 12:06
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8367
Location: 127.0.0.1
Contact:

Re: Using GPIO to measure energy

Post by Conn-artist »

roblom wrote:I probably do something wrong but can't find what.
When I do

Code: Select all

cd /etc/init.d
sudo isrcounter status
I get:

Code: Select all

sudo: isrcounter: command not found
/etc/init.d is not in the search path ($PATH environment variable).
Try sudo /etc/init.d/isrcounter status or (when you are in the /etc/init.d directory): sudo ./isrcounter status.
--
Martijn
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Using GPIO to measure energy

Post by roblom »

Conn-artist wrote:/etc/init.d is not in the search path ($PATH environment variable).
Try sudo /etc/init.d/isrcounter status or (when you are in the /etc/init.d directory): sudo ./isrcounter status.
When I use

Code: Select all

sudo /etc/init.d/isrcounter status
I get no message at all.
When I use

Code: Select all

cd /etc/init.d

Code: Select all

sudo ./isrcounter status
I get no message at all.
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Using GPIO to measure energy

Post by sincze »

roblom wrote:I was trying to use your script but i'm stuck at Step 3.

* I installed WiringPi (I missed that in your steps but I assumed is is necessary :-) )
* Added the isrcounter1.c to wiringPi/examples
Then I'm lost. Where did you added it and what should I do with it (how is the hardware connected, etc.?) Besides that I don't understand I think I can skip it and not using the LED. I also use a TCRT5000 so it already uses a LED but an extra LED can be nice. I left it for a later stage.
* I adjusted the pin numbers in the C program
Then I'm lost again, as I'm no programmer and I learnt all this by searching the internet I know what compiling is but I don't know how to do it. So please enlighten me :-)
Were you able to compile the code ??

Code: Select all

gcc -o isrcounter isrcounter1.c -lwiringPi
If nothing was compiled here succesfully your script in /etc/init.d will not do a thing either.

Hardware can be connected to your raspberry GPIO ports.
1 wire 3,3V,
1 wire GND,
1 wire D0,

Depending on the port chosen for D0 (digital output) on the PI you have to adjust the appropriate port in isrcounter1.c for INPIN.

Image
So Header Pin 12 = GPIO1 - WiringPi Pin 1.
The isrcountersc1.c should be adjusted like:

Code: Select all

// What GPIO input are we using?
//      This is a wiringPi pin number

#define IN_PIN                          1

Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Using GPIO to measure energy

Post by roblom »

I did the folowing:

Code: Select all

pi@RaspiBen / $ sudo apt-get install gcc
Pakketlijsten worden ingelezen... Klaar
Boom van vereisten wordt opgebouwd
De status informatie wordt gelezen... Klaar
gcc is reeds de nieuwste versie.
0 pakketten opgewaardeerd, 0 pakketten nieuw geïnstalleerd, 0 te verwijderen en 0 niet opgewaardeerd.

Code: Select all

pi@RaspiBen / $ cd home/pi/wiringPi/examples

Code: Select all

pi@RaspiBen ~/wiringPi/examples $ wget http://hansrune.net/domo/isrcounter1.c
--2015-02-24 21:57:50--  http://hansrune.net/domo/isrcounter1.c
Herleiden van hansrune.net (hansrune.net)... 80.91.36.18
Verbinding maken met hansrune.net (hansrune.net)|80.91.36.18|:80... verbonden.
HTTP-verzoek is verzonden; wachten op antwoord... 200 OK
Lengte: 4340 (4,2K) [text/x-c]
Wordt opgeslagen als: âisrcounter1.câ

100%[=========================================================================================================================>] 4.340       --.-K/s   in 0,03s

2015-02-24 21:57:50 (161 KB/s) - 'âisrcounter1.câ' opgeslagen [4340/4340]
Then i change the parameters of the downloaded file "isrcounter1.c" to

Code: Select all

#define	OUT_PIN		4
#define	IN_PIN		5
next

Code: Select all

pi@RaspiBen ~/wiringPi/examples $ gcc -o isrcounter isrcounter1.c -lwiringPi
pi@RaspiBen ~/wiringPi/examples $
Last edited by roblom on Tuesday 24 February 2015 22:27, edited 4 times in total.
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Using GPIO to measure energy

Post by roblom »

Then i get a file ïsrcounter". The instruction is then:
...Copy the compiled code to /usr/local/bin/isrcounter1....
Do I have to copy "isrcounter" the folder "/usr/local/bin/isrcounter1"
or
Do I have to copy "isrcounter" the folder "/usr/local/bin/"

For me it's quite confusing if you use "isrcounter" and "isrcounter1" in a random way. Also I think you use the same name for different files.
Like in the file "isrcounter" there is some parameter "NAME=isrcounter1". In the file "script_time_energy.lua" is a parameter "metername="isrcounter1".
The startup script has also the name "isrcounter".
It's a bit hard to be 100% sure if I use the correct file.
Last edited by roblom on Tuesday 24 February 2015 22:37, edited 1 time in total.
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Using GPIO to measure energy

Post by sincze »

roblom wrote:Then i get a file ïsrcounter". The instruction is then:
...Copy the compiled code to /usr/local/bin/isrcounter1....
Do I have to copy "isrcounter" the folder "/usr/local/bin/isrcounter1"
or
Do I have to copy "isrcounter" the folder "/usr/local/bin/"

For me it's quite confusing if you use "isrcounter" and "isrcounter1" in a random way. Also I think you use the same name for different files.
Like in the file "isrcounter" there is some parameter "NAME=isrcounter1". In the file "script_time_energy.lua" is a parameter "metername="isrcounter1". Should this all be the same file?
You can safely copy the compiled program with the name isrcounter to the directory /usr/local/bin

I can imagine the confusion of isrcounter / isrcounter1.c
Did take me some time to figure out as well.
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Using GPIO to measure energy

Post by roblom »

Ok but I did the above and still get no response when I do

Code: Select all

sudo /etc/init.d/isrcounter status
(i rebooted the RPi also)
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Using GPIO to measure energy

Post by sincze »

roblom wrote:Ok but I did the above and still get no response when I do

Code: Select all

sudo /etc/init.d/isrcounter status
(i rebooted the RPi also)
Does your iscounter program work if you execute it from within usr/local/bin ?
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Using GPIO to measure energy

Post by roblom »

Ok, I'm going somewhere. The thing that was wrong was what I already suspect, the unclear naming of isrcounter and isrcounter1 from within the scripts.
I'm able to directly start the script from within "usr/local/bin" and it reacts on my sensor.
But when I tried to start the isrcounter start/stop script I got an error of the creation of the log file. This is because the script is executed as pi and the pi user has no write access to the "var/log" folder. Is this correct?
So I created a isrcounter logfile by doing the following.

Code: Select all

sudo touch /var/log/isrcounter
sudo chmod 666 /var/log/isrcounter
But now again when I do

Code: Select all

sudo /etc/init.d/isrcounter start
I get no reaction.
But

Code: Select all

sudo /etc/init.d/isrcounter status
Gives

Code: Select all

[FAIL] isrcounter is not running ... failed!
I'm also missing some info about the following:
hansrune wrote:Step 5: Wire your input signal, and optionally the feedback output
What is what? I think
#define OUT_PIN 0 → "LED"
#define IN_PIN 1 → "Sensor"
hansrune wrote: Step 6: Under Domoticz application settings, adjust the number of pulses and costs per unit of consumption
I think this is not the number of pulses but the number of digits. For example if you want to display kW instead of W, the number of pulses is initiated by the sensor. But the script has to do something with these pulses, so there needs to be a factor that raises a number after a certain pulses. But I can't find this factor.
hansrune wrote: 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
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Using GPIO to measure energy

Post by roblom »

sincze wrote: 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.
When I use the TCRT5000 I also have the problem of too many interrupts even if I move my finger slowly pass the sensor, the LED indicator (controlled by the script) also stays on. If I move it really fast the indicator LED will somtimes go off. So the TCRT5000 gives to many interrupts. That's why the counting is that high...
You say that you could adjust "the script" but which script are you talking about because the example you provide I cannot recognize this piece in the "isrcounter" script.
The only dely I can find is the one in the isrcounter1.c script, in:

Code: Select all

  for (;;)
  {
    printf ("Waiting ... ") ; fflush (stdout) ;

    while (myCounter == globalCounter)
      delay (100) ;

    printf (" Done. counter: %lu: %lu\n",
		globalCounter, myCounter - lastCounter) ;
    lastCounter = myCounter ;
    myCounter   = globalCounter ;
    updateFile(argv[1], myCounter);
  }
Is this the same counter?
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Using GPIO to measure energy

Post by sincze »

It is a known issue for me that the tcrt 5000 generates lots of interrupts in a short time. Even adjusting the pod meter will not resolve all the falls ones.
The only solution we could find was not to use the interrupts in the end but poll the sensor every ms and see if the value of the pin is 0 or 1. And some additional logic. (only increment globalcounter if value goes from 0 to 1 don't count if it goes from 1 to zero).

Maybe I should create a wiki page.
With the whole custom script and all.
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Using GPIO to measure energy

Post by roblom »

Is there a better alternative for the TCRT5000? It must be an optical one because I'm not able to change something on the electrical meter.
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Using GPIO to measure energy

Post by roblom »

I still have the problem that the startup script won't start the isrcounter.
When I use

Code: Select all

pi@RaspiBen ~ $ sudo /etc/init.d/isrcounter status
[FAIL] isrcounter is not running ... failed!
pi@RaspiBen ~ $ sudo /etc/init.d/isrcounter start
pi@RaspiBen ~ $
pi@RaspiBen ~ $ sudo /usr/local/bin/isrcounter /var/run/shm/isrcounter
Waiting ...  Done. counter: 10: 0
Waiting ...
So the isrcounter does work, but it just won't start with the start/stop script. As I get no errors I don't know where to look for. Somebody an idea?
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests