espeasy sonoff pulse counter Metern

giulio
Posts: 36
Joined: Tue Aug 28, 2018 1:37 pm

Re: espeasy sonoff pulse counter Metern

Post by giulio » Thu Aug 30, 2018 3:59 pm

jeanmarc thank you very much for your help.

but I do not know in which folder the script req_espeasy.php should be copied and if something needs to be modified and some folders or files have to be created.

in Metern
which "command" should be entered in Main 5min pooling
which "command" should be entered in Dashboard live pooling

Thanks again

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Thu Aug 30, 2018 5:20 pm

please invest a bit of yourself :roll: It's really more usefull to know what you're doing..
1) put the scripts in your com app folder in the webserver directory
2) chmod +x req_espeasy.php
3) create a sym link ln -s /srv/http/comapps/req_espeasy.php /usr/bin/req_espeasy
4) adjust it to your need (config in script)
5) test as "req_espeasy -total" in meterN

ps: i've updted the script

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Fri Aug 31, 2018 12:48 pm

Here's the script, i've test it. It 100% work for a counter that doesn't always increase :

Code: Select all

removed

giulio
Posts: 36
Joined: Tue Aug 28, 2018 1:37 pm

Re: espeasy sonoff pulse counter Metern

Post by giulio » Sat Sep 01, 2018 6:18 pm

jeanmarc wrote:
Thu Aug 30, 2018 5:20 pm
please invest a bit of yourself :roll: It's really more usefull to know what you're doing..
1) put the scripts in your com app folder in the webserver directory
2) chmod +x req_espeasy.php
3) create a sym link ln -s /srv/http/comapps/req_espeasy.php /usr/bin/req_espeasy
4) adjust it to your need (config in script)
5) test as "req_espeasy -total" in meterN

ps: i've updted the script
This is what I did and what happend:
1) I copied the script to /var/www/comapps (which is my webserver directory I guess)
2) I modified the file's permissions
3) I created the symbolic link
4) I've set $pathtomn as /var/www/metern and $metnum
5) the test fails because apparently there is not a value. Also all my meters/sensors' values turn to 0 when I create this new one.

I noticed some files in the script (like /dev/shm/req_espeasy.espjson or /dev/shm/req_espeasy_log.txt) which do not exist on my Raspberry; I tried to create them, but the test fails anyway.

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Sat Sep 01, 2018 6:38 pm

There was some typo (i've updted the script). Don't create req_espeasy.json.
What does it return ?

giulio
Posts: 36
Joined: Tue Aug 28, 2018 1:37 pm

Re: espeasy sonoff pulse counter Metern

Post by giulio » Sat Sep 01, 2018 8:07 pm

The test still returns:

Code: Select all

Abording: Cannot get last value
is not valid, the correct format is 6(1234.5*l) 
And all the meters/sensors' values are 0

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Sun Sep 02, 2018 6:17 am

It mean it cannot get the last value from your esp. Check the url first.
If still have a problem, add a print_r($espjson) around line 88 to see more detail :

Code: Select all

...
    if (!isset($espjson['Total'])) {
	print_r($espjson);
        die("Abording: Cannot get last value\n");
        // $espjson['Total'] = 3; // testing
    }

giulio
Posts: 36
Joined: Tue Aug 28, 2018 1:37 pm

Re: espeasy sonoff pulse counter Metern

Post by giulio » Sun Sep 02, 2018 10:07 am

the URL is correct
I added to print_r ($ espjson) around line 88

now The test still returns:

URI: / espjsonMethod: GETArguments: 1 NAME: tasknr VALUE: 1Abording: Can not get last value
is not valid, the correct format is 6 (1234.5 * l)



I do not know if it can servel:

from browser

http://192.168.1.140/espjson?tasknr=1

URI: / espjson
Method: GET
Arguments: 1
NAME: tasknr
VALUE: 1

http://192.168.1.140/json

{"System": {
"Build": 20000,
"Unit": 1,
"Uptime": 2087,
"Free RAM": 22200
},
"Sensors": [
{
"TaskName": "counter",
"Count": 0.00,
"Total": 14.25,
"Time": 1487.00
}
]}

http://192.168.1.140/json?tasknr=1

{
"TaskName": "counter",
"Count": 0.00,
"Total": 14.25,
"Time": 1487.00
}

what is the correct url?

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Sun Sep 02, 2018 11:16 am

Ok the url is ok. Maybe try this one :

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.
// chmod +x then ln -s /srv/http/comapps/req_espeasy.php /usr/bin/req_espeasy

// config
$pathtomn = '/srv/http/metern'; // without / at the end
$metnum   = 1; // vitual meter number
$url      = 'http://192.168.1.140/espjson?tasknr=1'; // espeasy url

// No edit should be needed bellow
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}
if (!isset($argv[1])) {
    die("Abording: no valid argument given.\n");
}
if (isset($argv[2])) {
    die("Abording: Too many arguments\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, $datareturn) // Retrieve last know value in csv
{
    $datareturn = null;
    $contalines = count($csvarray);
    $j          = 0;
    while (!isset($datareturn)) {
        $j++;
        $array      = preg_split('/,/', $csvarray[$contalines - $j]);
        $datareturn = (int) trim($array[$meternum]);
        if ($datareturn == '') {
            $datareturn = null;
        }
        if ($j == $contalines) {
            $datareturn = 0;
        }
    }
    if ($datareturn > $passo) {
        $datareturn -= $passo;
    }
    return $datareturn;
}

if ($argv[1] == '-total') {
    // 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
            $contalines           = count($lines);
            $datareturn           = null;
            $previous['virt_tot'] = retrievecsv($metnum, $lines, ${'PASSO' . $metnum}, $datareturn);
        } else { // restarting from scratch !
            $previous['virt_tot'] = 0;
        }
        $previous['esp_count'] = 0;
    }
    
    // Get last value
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 5000); // error
    $espjson = array();
    if (($espjson = curl_exec($ch)) === false) {
        die(curl_error($ch) . "\n");
    }
    /*
    $espjson ='{
    "TaskName": "counter",
    "Count": 0.00,
    "Total": 14.25,
    "Time": 1487.00
    }';
    */
    $espjson = json_decode($espjson, true);
    if (!isset($espjson['Total'])) {
        //print_r($espjson);
        die("Abording: Cannot get last value\n");
        // $espjson['Total'] = 3; // testing
    }
    $last = floatval($espjson['Total']);
    $prev = (float) $previous['esp_count'];
    if ($last > $prev) {
        $diff = $last - $prev; // Difference
        $previous['virt_tot'] += $diff; // increment
        if ($previous['virt_tot'] >= ${'PASSO' . $metnum}) { // passed over
            $previous['virt_tot'] = 0;
        }
        $previous['esp_count'] = $espjson['Total'];
    } else { // 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] == '-prev') {
    if (file_exists($prevfile)) {
        echo "\n$prevfile :\n\n";
        $data     = file_get_contents($prevfile);
        $previous = json_decode($data, true);
        print_r($previous);
    }
} else {
    echo "Usage: req_espeasy { total | prev }\n
	-total : Virtual total counter
	-prev : Show previous file
	\n";
}
?>

giulio
Posts: 36
Joined: Tue Aug 28, 2018 1:37 pm

Re: espeasy sonoff pulse counter Metern

Post by giulio » Sun Sep 02, 2018 3:33 pm

The test still returns: (command req_espeasy -total)
6 (0 * l)
is a valid entry!

but the water value is always at 0, even Immisions and Self-consumption value at 0

Image
https://drive.google.com/file/d/1aaXYnB ... CTTPf/view

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests