Reflective IR sensor

Post Reply
jeanmarc
Posts: 1832
Joined: Thu Aug 29, 2013 7:16 am

Reflective IR sensor

Post by jeanmarc » Tue Dec 17, 2013 8:12 am

Hi,
I sometimes have troubles with my IR barrier reflective sensor of my gas meter.
Image
It happens when the reflective digit is stop in front of the sensor, even though it is not suppose to give "fallings edges" as this point, the electric signal seem to be a bit noisy and not continuous. :?
That's enough for the Arduino to count. I am looking for a trick to make sure the pulse is real, anyone have any idea ?

thanks

megamarco83
Posts: 77
Joined: Tue Apr 14, 2015 9:25 am

Re: Reflective IR sensor

Post by megamarco83 » Tue Feb 21, 2017 9:46 am

Hi jeanmarc did you discover something?
i would like to implement the gas meter to have my consumption....
thanks
Marco

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

Re: Reflective IR sensor

Post by jeanmarc » Tue Feb 21, 2017 10:10 am

Hi Marco,
Not 100% accurate but since i measure pulse lenght to avoid those under 300ms, I have way less issues.
The biggest problem for me now is that the sensor is peeling off sometimes (when it's hot) and it move from the good position. Sadly i can't definitely fix it or i may have trouble with my gas provider :?

megamarco83
Posts: 77
Joined: Tue Apr 14, 2015 9:25 am

Re: Reflective IR sensor

Post by megamarco83 » Tue Feb 21, 2017 10:20 am

hey Jeanmarc thank for feedback...i understand what you mean....and also my doubt is related to my gas provider...if i attach with a glue a reflective IR on my gas meter, with powering (batteries) i do not know if "they are happy".
anyway...do you use batteries or you directly powering the IR?
how many days during the batteries if you use this type of powering?
i looking for some sensor/counter modbus for gas (reading the flow, so i could install after the gas meter of supplyer) like sdm120 (but for gas) but I didn't find anything cheep, do you know something?
thanks

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

Re: Reflective IR sensor

Post by jeanmarc » Tue Feb 21, 2017 11:06 am

Well, i believe that in few years we will have smart meters here in Belgium. I hope it will be some sort of P1 like in Nedherlands..

Meanwhile, we need to tweak a bit ;) I use "repositionable pads", it doesn't make any trace when removing. The bad thing: it melt and move when the temperature is above 25°
I am not worried by powering the sensor, there is not direct contact and the voltage is only 5V (powered by arduino). I don't believe a provider wouldn't make problem, they also sell counting sensors that use electricity...

I prefer not to be worried with battery, i don't know how long that could last.

megamarco83
Posts: 77
Joined: Tue Apr 14, 2015 9:25 am

Re: Reflective IR sensor

Post by megamarco83 » Tue Nov 07, 2017 4:39 pm

Hi Jeanmarc i come back to this topic beause i used a sonoff modded using espmega as firmware
the i connected to GPIO14 of sonoff a reeder with a glass relay magnetic switch
https://it.aliexpress.com/item/Smart-El ... 0.0.lhgkm0
i set IP of sonoff 192.168.0.198
and the configuration is:
device = pulse counter (p003)
GPIO used = 14
debounce time = 1000 msec
counter type = delta
model type = rising
and delay = 1

if i use the shell command:

Code: Select all

pi@raspberrypi ~ $ curl -s http://192.168.0.198/json?tasknr=1
{
"TaskName": "GAS",
"Count": 0.00,
"Total": 55.00,
"Time": 1778.00
}
total = 55 the value of the pulse that is registered by the sensot (i put a magnet 55times close to the switch to test it and it works well)
so i get the value of pulse counting with:

Code: Select all

pi@raspberrypi ~ $ curl -s http://192.168.0.198/json?tasknr=1 | egrep '"Total":' | egrep -o '[0-9]*(\.)?[0-9]*'
55.00
now i modify the php poolerpulse.php
like this:

Code: Select all

#!/usr/bin/php
<?php
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}
// This script will output a meterN compatible format for the main command
// You'll need to setup the path to meterN ($pathtomn). 
// ln -s /var/www/comapps/poolerpulse.php /usr/bin/poolerpulse
// Usage:  poolerpulse [MeterID] [gas | water]

// No edit is needed below
if (!isset($argv[1],$argv[2])) {
$argv[1]=null;
}

if ($argv[1]!= null && ($argv[2]== 'gas' || $argv[2]== 'water')) {
    
	$pathtomn = '/var/www/metern';
	$costgas = 0.01; 	// costante di conversione da impulsi a mc per gas
	$costwater = 1;  	// costante di conversione da impulsi a mc per acqua
	$prevgas = 338500; 		// Inserire qui il totale riportato dal contatore gas
	$prevwater = 0; 	// Inserire qui il totale riportato dal contatore water
	$metnum = $argv[1]; // Meter ID 

	//$cmd = "more /dev/shm/metern$metnum.txt | egrep \"^$metnum\(\" | grep \"*Wh)\""; // Request pulse gas
	//$cmd1 = "more /dev/shm/metern$metnum.txt | egrep \"^$metnum\(\" | grep \"*W)\""; // Request pulse water
	$cmd = `curl -s http://192.168.0.198/json?tasknr=1 | egrep '"Total":' | egrep -o '[0-9]*(\.)?[0-9]*'`;	// Request pulse gas
	$cmd1 = "curl http://192.168.1.XX"; // Request pulse water
    #echo "$cmd\n";
    #echo "$cmd1\n";

	// End of setup

	define('checkaccess', TRUE);
	include("$pathtomn/config/config_main.php");
	include("$pathtomn/config/config_met$metnum.php");
    
    if ($argv[2]== 'gas') {
        // acquisisce il valore precedente dal csv
        $dir    = '/var/www/metern/data/csv';
        $output = array();
        $output = glob($dir . '/*.csv');
        sort($output);
        $cnt = count($output);
            
        if (file_exists($output[$cnt - 1])) {
            $file       = file($output[$cnt - 1]); // today
            $contalines = count($file);

            if ($contalines > 1) {
                $prevarray = preg_split("/,/", $file[$contalines - 1]);
                
            } elseif ($contalines == 1 && file_exists($output[$cnt - 2])) { // yesterday, only header
                $file       = file($output[$cnt - 2]);
                $contalines = count($file);
                $prevarray = preg_split("/,/", $file[$contalines - 1]);
            }
            $cons_val_first = trim($prevarray[$metnum]);
        } else {
            $cons_val_first = null;
        }      

        #sleep(1); // oh why ?
        // Now retrieve the current value
        $datareturn = shell_exec($cmd);
		//echo "$datareturn\n";
        $datareturn = trim($datareturn);
        $datareturn = preg_replace("/^${'ID'.$metnum}\(/i", '', $datareturn); // VALUE*UNIT)
        $lastval    = preg_replace("/\*[a-z0-9]+\)$/i", '', $datareturn); // VALUE
        //echo "$lastval\n";

        settype($lastval, 'float');
        settype($prevgas, 'float');
        settype($cons_val_first, 'float');

        $lastval += $prevgas; // aggiunge il correttore del totale

        if ($lastval < $cons_val_first) { // controlla se il contatore segna meno del valore precedente
            $lastval = $cons_val_first;
        }
        $lastval = round($lastval, ${'PRECI' . $metnum});
		$lastval = $lastval*$costgas;
        $str     = utf8_decode("${'ID'.$metnum}($lastval*${'UNIT'.$metnum})\n");
        file_put_contents("/dev/shm/consumi$metnum.txt", $str);
        echo "$str";
    
    }elseif ($argv[2]== 'water') {
              // acquisisce il valore precedente dal csv
        $dir    = '/var/www/metern/data/csv';
        $output = array();
        $output = glob($dir . '/*.csv');
        sort($output);
        $cnt = count($output);
            
        if (file_exists($output[$cnt - 1])) {
            $file       = file($output[$cnt - 1]); // today
            $contalines = count($file);

            if ($contalines > 1) {
                $prevarray = preg_split("/,/", $file[$contalines - 1]);
                
            } elseif ($contalines == 1 && file_exists($output[$cnt - 2])) { // yesterday, only header
                $file       = file($output[$cnt - 2]);
                $contalines = count($file);
                $prevarray = preg_split("/,/", $file[$contalines - 1]);
            }
            $cons_val_first = trim($prevarray[$metnum]);
        } else {
            $cons_val_first = null;
        }      
		#sleep(1); // oh why ?
        // Now retrieve the current value
        $datareturn = shell_exec($cmd1);
        $datareturn = trim($datareturn);
        $datareturn = preg_replace("/^${'ID'.$metnum}\(/i", '', $datareturn); // VALUE*UNIT)
        $lastval    = preg_replace("/\*[a-z0-9]+\)$/i", '', $datareturn); // VALUE

        settype($lastval, 'float');
        settype($prevgas, 'float');
        settype($cons_val_first, 'float');

        $lastval += $prevgas; 			// aggiunge il correttore del totale
		$lastval = $lastval*$costgas;	// converte gli impulsi secondo la costante
		$lastval += $cons_val_first;	// aggiunge la lettura precedente
		
		/*
        if ($lastval < $cons_val_first) { // controlla se il contatore segna meno del valore precedente
            $lastval = $cons_val_first;
        }
		*/
        $lastval = round($lastval, ${'PRECI' . $metnum});
        $str     = utf8_decode("${'ID'.$metnum}($lastval*${'UNIT'.$metnum})\n");
        file_put_contents("/dev/shm/consumi$metnum.txt", $str);
        echo "$str";
    }
    
} else {
    die("Usage: poolerpulse {MeterID} {gas | water}\n");
}
?>
i create a meter on metern, number 22 and i put the type as "gas" and the command:
poolerpulse 22 gas
i give the permission to file:

Code: Select all

cd /var/www/metern/comapps 
sudo chmod a+x poolerpulse.php 
sudo ln -s /var/www/metern/comapps/poolerpulse.php /usr/bin/poolerpulse
sudo chown -R www-data:www-data /var/www/metern/comapps/poolerpulse.php
the problem is that i try to use the pooler i receive:

Code: Select all

pi@raspberrypi ~ $ poolerpulse 22 gas
sh: 1: 55.00: not found
22(3385*m^3)
3385*m^3 is the number that i insert here on php $prevgas
55.00 is the number that i extract from espeasy counter

but why is not working and if i use a magnet to increment the counting i read correct the number using:

Code: Select all

pi@raspberrypi ~ $ curl -s http://192.168.0.198/json?tasknr=1 | egrep '"Total":' | egrep -o '[0-9]*(\.)?[0-9]*'
55.00
but the poolerpulse.php is not working and my gas counter is not incrementing on metern
could you help me?
thanks so mutch

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

Re: Reflective IR sensor

Post by jeanmarc » Tue Nov 07, 2017 5:49 pm

Hi,
Maybe use json_decode instead of more | egrep

Code: Select all

<?php
$somedata = file_get_contents(/dev/shm/metern22.txt);
$dataarray = json_decode($somedata, true);
//print_r($dataarray);
echo "you want $dataarray['Total'] right";
?>

megamarco83
Posts: 77
Joined: Tue Apr 14, 2015 9:25 am

Re: Reflective IR sensor

Post by megamarco83 » Wed Nov 08, 2017 12:09 am

Hi jeanmarc
Thanks for your replay...could you explain to me better how to use the suggested php that you wrote upper?
Thanks
Marco

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

Re: Reflective IR sensor

Post by jeanmarc » Wed Nov 08, 2017 3:59 pm

Sorry but i don't have much free time right now.. you need to change the shell_exec($cmd) with the exemple

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest