espeasy sonoff pulse counter Metern

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Sun Nov 10, 2019 7:59 am

Hi,
You should run as

Code: Select all

pi@raspberrypi:/var/www/comapps $ sudo pingtester -start &
[1] 13243
13243 is then the process id, please read some tutorials to understand Linux basis (no offence there) :angel:
The pingertester is a infinity loop, it check each 5 min if the device respond, you may see the process with ps -ef | grep pingertester

For the timeout thing, you can forget about it now. If mN is stuck, leave it running and request each com app in terminal to see which one is broken. (req_espeasy -total, -live and -Humidity and the other ones)

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

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Mon Nov 11, 2019 11:43 am

jeanmarc wrote:
Sun Nov 10, 2019 7:59 am
Hi,
You should run as

Code: Select all

pi@raspberrypi:/var/www/comapps $ sudo pingtester -start &
[1] 13243
13243 is then the process id, please read some tutorials to understand Linux basis (no offence there) :angel:
The pingertester is a infinity loop, it check each 5 min if the device respond, you may see the process with ps -ef | grep pingertester

For the timeout thing, you can forget about it now. If mN is stuck, leave it running and request each com app in terminal to see which one is broken. (req_espeasy -total, -live and -Humidity and the other ones)
Hi JM today i find metern that was stuck. so i lunch this command with this response:

Code: Select all

pi@raspberrypi:~ $ sudo pingtester -start &
[1] 29195
pi@raspberrypi:~ $ sudo: pingtester: comando non trovato

Code: Select all

ps -ef | grep pingertester
pi       30610 29014  0 11:30 pts/0    00:00:00 grep --color=auto pingertester
[1]+  Uscita 1                sudo pingtester -start
pi@raspberrypi:~ $ ps -ef | grep pingertester
pi       30764 29014  0 11:30 pts/0    00:00:00 grep --color=auto pingertester
pi@raspberrypi:~ $ sudo pingtester -start &
[1] 30972
pi@raspberrypi:~ $ sudo: pingtester: comando non trovato
^C
[1]+  Uscita 1                sudo pingtester -start

then i try to call the comapps:

Code: Select all

pi@raspberrypi:~ $ req_espeasy -total
^C pi@raspberrypi:~ $
pi@raspberrypi:~ $ req_espeasy -live
^C pi@raspberrypi:~ $
pi@raspberrypi:~ $ poolerconsumi 1 energy
PHP Warning:  file_put_contents(/dev/shm/consumi1.txt): failed to open stream: P                                                                             ermission denied in /var/www/comapps/poolerconsumi.php on line 77
1(41389609*Wh)
pi@raspberrypi:~ $
pi@raspberrypi:~ $ pool123s energy
PHP Warning:  file_put_contents(/dev/shm/produzione2.txt): failed to open stream                                                                             : Permission denied in /var/www/comapps/pool123s.php on line 66
2(31643780*Wh)
pi@raspberrypi:~ $ eflow whin
3(2536356*Wh)
pi@raspberrypi:~ $ reqsdm DStemp11
11(42.00*C)pi@raspberrypi:~ $ reqsdm DStemp14
14(28.81*C)pi@raspberrypi:~ $ reqsdm nodemcu_204_temp_ritorno_riscald
23(26.4*°C)
pi@raspberrypi:~ $ req_espeasy -total
^C pi@raspberrypi:~ $ req_espeasy -total
^C 
lookin at the response it seams that comapp with problem should be req_espeasy -total right? because i have to press CTRL + C to quit
what i should do to:
1) avoid the stuck of metern when i use pulse counter (req_espeasy -total)
2) pin metern to be notify if it's stuck? i mean that i would like to ping metern and receve two type of response: "response A" if it's not stuck, "response B" it it's stuck, then with domoticz in the same way that i ping all my esp8266 device, i will ping meterN and i will judge the response, if it's "response A" i will not send the notify, if it will be "response B" i will send notify with domoticz.
thanks so much!
regards

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Mon Nov 11, 2019 3:59 pm

Hi,
You have sudo: command not found ? Did you do a ln -s /srv/http/pathto/pingtester.php /usr/bin/pingtester ?

So ok, req_espeasy is stuck and mN hold and waiting a return. :idea: I believe the solution is to add CURLOPT_TIMEOUT in the script, do change twice :

Code: Select all

..
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3000); // error
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
..
You can also put set_time_limit(20); in top of the script. If all work, you can forget about pingtester.

For the notification, you will get warned if you enable "Warn connection lost" and config Pushover and/or RpiNotify.

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

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Mon Nov 11, 2019 6:49 pm

jeanmarc wrote:
Mon Nov 11, 2019 3:59 pm
Hi,
You have sudo: command not found ? Did you do a ln -s /srv/http/pathto/pingtester.php /usr/bin/pingtester ?
i'm logged in putty with "pi"
so i used:
sudo ln -s /var/www/pathto/pingtester.php /usr/bin/pingtester
jeanmarc wrote:
Mon Nov 11, 2019 3:59 pm
So ok, req_espeasy is stuck and mN hold and waiting a return. :idea: I believe the solution is to add CURLOPT_TIMEOUT in the script, do change twice :

Code: Select all

..
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3000); // error
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
..
You can also put set_time_limit(20); in top of the script. If all work, you can forget about pingtester.
i added lines: 16, 75 and 91
can you please have a look if i modify it correctly?

Code: Select all

#!/usr/bin/php
<?php
// A simple script to request a espeasy counter that doesn't always increase (after a surge) and make virtual meter.
// You should set your meter ID in meterN and define a non null pass over value (like 100000)
// chmod +x then ln -s /var/www/comapps/req_espeasy.php /usr/bin/req_espeasy
// Use req_espeasy -total as main command -live as live command

// Config
$pathtomn  = '/var/www/metern'; // without / at the end
$metnum    = 20; // Vitual meter number

$url       = 'http://192.168.0.204/json?tasknr=1'; // espeasy url  CHANGE LINE 138  -> $val_count /=100; // 1 impulse = 0.01m3 gas 
$initial   = 630.0; // initial virtual meter value  (x gas era 1214.95)
$urlreboot = 'http://192.168.0.204/?cmd=reboot'; // restart counting on reboot   ---> line 73 mettere sleep 

set_time_limit(20); //added in 11/11/2019 to avoid stuck of metern

// No edit should be needed bellow
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}
if (isset($argv[2])) {
    die("Abording: Too many arguments\n");
}
if (!isset($argv[1])) {
    die("\nUsage: req_espeasy { total | live | Humidity | prev }\n
    -total : Virtual total counter
    -live : Counter live value
    -Humidity : Show humidity value
    -prev : Show previous file
    \n");
}
// Save previous values in a file, make sure the http user can write there
$prevfile = '/dev/shm/req_espeasy.json';
if (file_exists($prevfile) && !is_writable($prevfile)) {
    die("$prevfile not writable");
}

define('checkaccess', TRUE);
include("$pathtomn/config/config_main.php");
include("$pathtomn/config/memory.php");
include("$pathtomn/config/config_met$metnum.php");

function retrievecsv($meternum, $csvarray, $passo) // Retrieve last know value in csv
{
    $datareturn = null;
    $contalines = count($csvarray);
    $j          = 0;
    while (!isset($datareturn)) {
        $j++;
        $array      = preg_split('/,/', $csvarray[$contalines - $j]);
        $datareturn = (float) trim($array[$meternum]);
        if ($datareturn == '') {
            $datareturn = null;
        }
        if ($j == $contalines) {
            $datareturn = 0;
        }
    }
    if ($datareturn > $passo) {
        $datareturn -= $passo;
    }
    return $datareturn;
}

if ($argv[1] == '-total' && !file_exists($prevfile)) {
	// Reboot
	$ch = curl_init($urlreboot);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3000); // error
	curl_setopt($ch, CURLOPT_TIMEOUT, 10); // added in 11/11/2019 to avoid stuck of metern
	if ((curl_exec($ch)) === false) {
	    die(curl_error($ch) . "\n");
	}
	curl_close($ch);
	sleep(11);  //was 14
}

if ($argv[1] == '-live' || $argv[1] == '-total' || $argv[1] == '-Humidity') { // Get last values












    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3000); // error
	curl_setopt($ch, CURLOPT_TIMEOUT, 10); // added in 11/11/2019 to avoid stuck of metern
    $espjson = array();
    if (($espjson = curl_exec($ch)) === false) {
        die(curl_error($ch) . "\n");
    }
    curl_close($ch);
    
    // Testing
    /*
    $espjson = '
    {  
    "TaskValues":[  
    {  
    "ValueNumber":1,
    "Name":"Count",
    "NrDecimals":2,
    "Value":10000.00
    },
    {  
    "ValueNumber":2,
    "Name":"Total",
    "NrDecimals":2,
    "Value":1000.00
    },
    {  
    "ValueNumber":3,
    "Name":"Time",
    "NrDecimals":2,
    "Value":0.00
    }
    ],
    "TTL":1000,
    "DataAcquisition":[  
    {  
    "Controller":1,
    "IDX":0,
    "Enabled":"false"
    },
    {  
    "Controller":2,
    "IDX":0,
    "Enabled":"false"
    },
    {  
    "Controller":3,
    "IDX":0,
    "Enabled":"false"
    }
    ],
    "TaskInterval":1,
    "Type":"Generic - Pulse counter",
    "TaskName":"gas",
    "TaskEnabled":"true",
    "TaskNumber":3
    }';
    */
    $espjson   = json_decode($espjson, true);
    // Get values from json
    $val_live  = $espjson['TaskValues'][0]['Value'];
    $val_count = $espjson['TaskValues'][1]['Value'];
    $val_count /= 4000; // 1 impulse = 0.01m3 gas scrivere: $val_count /= 100      se 1 impulse = 0.25m3 h20 scrivere: $val_count /= 4000
    $val_humid = $espjson['TaskValues'][1]['Value'];
    //print_r($espjson);
}

if ($argv[1] == '-live') {
    if (!isset($val_live)) {
        die("Abording: Cannot get live Count value\n");
    }
    $val    = (float) $val_live;
    $outstr = utf8_decode("${'LID' . $metnum}($val*${'LIVEUNIT' . $metnum})\n");
    echo "$outstr";
} elseif ($argv[1] == '-total') {
    if (!isset($val_count)) {
        die("Abording: Cannot get last Total value\n");
    }
    // Retrieve previous virtual meter value
    if (file_exists($prevfile)) {
        $data     = file_get_contents($prevfile);
        $previous = json_decode($data, true);
    } else {









        $tdfile  = date('Ymd');
        $tdfile  = $pathtomn . '/data/csv/' . $tdfile . '.csv';
        $ytdfile = date('Ymd', strtotime("-1 day"));
        $ytdfile = $pathtomn . '/data/csv/' . $ytdfile . '.csv';
        
        if (file_exists($tdfile)) {
            $lines = file($tdfile);
        } else if (file_exists($ytdfile)) {
            $lines = file($ytdfile);
        }
        if (file_exists($tdfile) || file_exists($ytdfile)) { // today or yesterday
            $previous['virt_tot'] = retrievecsv($metnum, $lines, ${'PASSO' . $metnum});
        } else { // restarting from scratch !
            $previous['virt_tot'] = $initial;
        }
        $previous['esp_count'] = 0;
    }
    $last = (float) $val_count;
    $prev = (float) $previous['esp_count'];
    if ($last > $prev) {
        $diff = $last - $prev; // Increment vitural meter
        $previous['virt_tot'] += $diff;
        if ($previous['virt_tot'] >= ${'PASSO' . $metnum}) { // passed over
            $previous['virt_tot'] -= ${'PASSO' . $metnum};
        }
        $previous['esp_count'] = $val_count;
    } elseif ($last < $prev) { // surge or esp counter restart
        $previous['esp_count'] = 0;
    }
    
    // Saving previous values
    $data = json_encode($previous);
    file_put_contents($prevfile, $data);
    $val    = $previous['virt_tot'];
    // Output
    $outstr = utf8_decode("${'ID' . $metnum}($val*${'UNIT' . $metnum})\n");
    echo "$outstr";
} elseif ($argv[1] == '-Humidity') {
    $val    = $val_humid;
    $outstr = utf8_decode("Humidity($val*hum)\n");
    echo "$outstr";
} elseif ($argv[1] == '-prev') {
    if (file_exists($prevfile)) {
        echo "\n$prevfile :\n\n";
        $data     = file_get_contents($prevfile);
        $previous = json_decode($data, true);
        print_r($previous);
    }
}
?>
but with this code modification, what will be the new response if during a reboot or whatever the req_espeasy will not respond?

jeanmarc wrote:
Mon Nov 11, 2019 3:59 pm
For the notification, you will get warned if you enable "Warn connection lost" and config Pushover and/or RpiNotify.
ok, you are right, but ripnotify is not anymore supported for free and pushover is not working for my mobile, that's the reason why i implement by myself with domoticz (dzvent, lua script) a notification sistem that is based on ping my espeasy devices.
if i would like to extend to metern, how i can recognize if meterN is stuck or not?
thanks


---P.S
last question, i find this differences in putty using req_espeasy using "sudo" or without using "sudo":
of course i create the symbolic link with: sudo ln -s /var/www/comapps/req_espeasy.php /usr/bin/req_espeasy and the owner of /usr/bin/req_espeasy is "root"

Code: Select all

pi@raspberrypi:~ $ sudo req_espeasy -total
20(703.5675*m3)
pi@raspberrypi:~ $ req_espeasy -total
/dev/shm/req_espeasy.json not writablepi@raspberrypi:~ $

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Mon Nov 11, 2019 7:10 pm

Ok one problem at the time ;)
Your script is correct but that better to put 'set_time_limit' on top

Code: Select all

<?php
set_time_limit(20);
...
If req_espeasy don't respond it should end up with an error instead of waiting.

RpiNotify is still free AFAIK. I believe instant notification are more wise with such app rather than email and there is no problem to buy something useful :roll: Linux is not all about free for nothing...

The issue about req_espeasy is that you cannot launch that as root, it's the HTTP that should run that. If you do so do chmod 666 /dev/shm/req_espeasy.json

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

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Mon Nov 11, 2019 7:42 pm

jeanmarc wrote:
Mon Nov 11, 2019 7:10 pm
Ok one problem at the time ;)
Your script is correct but that better to put 'set_time_limit' on top

Code: Select all

<?php
set_time_limit(20);
...
ok, i correct the script, thanks for suggestion!
jeanmarc wrote:
Mon Nov 11, 2019 7:10 pm
If req_espeasy don't respond it should end up with an error instead of waiting.
ok, i will test it from now on....

jeanmarc wrote:
Mon Nov 11, 2019 7:10 pm
RpiNotify is still free AFAIK. I believe instant notification are more wise with such app rather than email and there is no problem to buy something useful :roll: Linux is not all about free for nothing...
uhmm....if i'm not wrong, i tested it now, but i receive a message that is not for free anymore.
anyway, i agree with you, an app can be bought, not a problem and i also agree that a push message is better than email, but it's the reason why i use domoticz, and i write an app in dzvent (lua language of domoticz) to ping my device. in this case i create my personal bot in telegram, add my family to this bot, and with domoticz ping my espeasy device, if they not respond i send i push notification with telegram!
so now i would try to extend my app in domoticz and try to ping meterN.
that's why i asked if there is a way to ping meterN and have two types of answer: one for correct running, and a different one if meterN is stuck.
is it possible?
jeanmarc wrote:
Mon Nov 11, 2019 7:10 pm
The issue about req_espeasy is that you cannot launch that as root, it's the HTTP that should run that. If you do so do chmod 666 /dev/shm/req_espeasy.json
ok, i think that i understand.
my chmod of /dev/shm/req_espeasy.json are 644 and inside metern the command field i write directly:

Code: Select all

req_espeasy -total
the symbolic link was created as:
sudo chmod +x /var/www/comapps/req_espeasy.php
then
sudo ln -s /var/www/comapps/req_espeasy.php /usr/bin/req_espeasy
so both with "sudo". I did correctly all things?
thanks

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Mon Nov 11, 2019 8:19 pm

Hard to tell if mN is stuck with a ping, the solution would be to look if time stamp is late in /dev/shm/mN_LIVEMEMORY.json.
That say : mN is stable, if a problem arise it should be a com app.

About 644, it mean that the user who run it (root) can write, the other can only reads. So please chmod and don't run it in terminal (with sudo) with -total as it write in a file. Other options are fine (live | Humidity | prev).

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

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Mon Nov 11, 2019 8:42 pm

jeanmarc wrote:
Mon Nov 11, 2019 8:19 pm
Hard to tell if mN is stuck with a ping, the solution would be to look if time stamp is late in /dev/shm/mN_LIVEMEMORY.json.
That say : mN is stable, if a problem arise it should be a com app.

About 644, it mean that the user who run it (root) can write, the other can only reads. So please chmod and don't run it in terminal (with sudo) with -total as it write in a file. Other options are fine (live | Humidity | prev).
Yes, metern is very stable, the confernation is that if not use the comapp req_espeasy i never find metern stuck.
So i agree with you that the problem is the app.

Related to ping metern, could be a solution reading the json in /run/shm and see the last update?
Which one i need to monitor and after what time of not updating i can say that metern is stuck?

So it's correct to have 644 permissions? Because i never run req_espeasy from putty or shell, but always inside metern.

The option -prev what function has?

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Mon Nov 11, 2019 9:14 pm

I'll look later on to avoid getting stuck if a com app don't respond. First time it happen to me..

Code: Select all

more /dev/shm/mN_LIVEMEMORY.json
{"UTC":1573502999,"Conso1":"228","Gas2":"<font color='#8B0000'>Off<\/font>","Water3":"0","Prod4":"0","EIMP5":"226.9","EE
XP6":"0","eselfc7":"0"}
UTC is time stamp in epoch format. Looking at pool123s.php there is some sort part of the solution.
644 might be good, depend on the owner of the file ;)
-prev simply show previous 5' value for your esp.

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

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Mon Nov 11, 2019 9:33 pm

jeanmarc wrote:
Mon Nov 11, 2019 9:14 pm
I'll look later on to avoid getting stuck if a com app don't respond. First time it happen to me..

Code: Select all

more /dev/shm/mN_LIVEMEMORY.json
{"UTC":1573502999,"Conso1":"228","Gas2":"<font color='#8B0000'>Off<\/font>","Water3":"0","Prod4":"0","EIMP5":"226.9","EE
XP6":"0","eselfc7":"0"}
UTC is time stamp in epoch format. Looking at pool123s.php there is some sort part of the solution.
644 might be good, depend on the owner of the file ;)
-prev simply show previous 5' value for your esp.
hi, so i can see inside the file: /dev/shm/mN_LIVEMEMORY.json the UTC value, i run my script every minute, if it's ok i will find a diffent value compared to the minute before, if i will find the same value metern is stuck. could be a solution?

the file in /dev/shm/req_espeasy.json is 644 and the owner is www-data.
the file /var/www/comapps/req_espeasy.php is 755 and the owner is www-data.
the symbolic link file /usr/bin/req_espeasy is 777 and the owner is root
that's correct?

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest