espeasy sonoff pulse counter Metern

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

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Thu Nov 28, 2019 7:00 pm

hi JM, thanks for help, but i'm not so good with php and systemd.


i create the ping_metern.php in my /var/www/MyScripts/

Code: Select all

#!/usr/bin/php
<?php
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}

$domoticzIP='192.168.0.105'
$domoticzPort=8085
$signalSwitch=152
$domoticzURL="http://$domoticzIP:$domoticzPort/json.htm?type=command&param=switchlight&idx=$signalSwitch&switchcmd="
$log       = '/tmp/ping_metern.log';

if (file_exists('/dev/shm/mN_LIVEMEMORY.json')) {
	$data     = file_get_contents('/dev/shm/mN_LIVEMEMORY.json');
	$memarray = json_decode($data, true);
	$nowUTC   = strtotime(date("Ymd H:i:s"));
		if ($nowUTC - $memarray["UTC"] > 300) { // too old
		echo("switch on dummy device on domoticz");
		$remotedata = file_get_contents($domoticzURL);
		
		$stamp = date('d/m/Y H:i:s');
	    $data = "$stamp : $nowUTC - $memarray['UTC']\n";
	    file_put_contents($log, $data, FILE_APPEND);
		}
} else { // ain't running
	die("Aborting: mN not running \n");
}
?>
it has owner root and it is 755
and i create the symlink
sudo ln -s /var/www/MyScripts/ping_metern.php /usr/bin/ping_metern
it's all ok till hear?

i have some dubt in php code:
1) to turn on the dummy devices in domoticz the url should be:
http://$domoticzIP:$domoticzPort/json.htm?type=command&param=switchlight&idx=$signalSwitch&switchcmd=On

so at line:
$remotedata = file_get_contents($domoticzURL);
is possible to add "On" ?

2) is not better to check two or three times before sendin to domoticz the triggering of the dummy switch?
i see that you use:
if ($nowUTC - $memarray["UTC"] > 300)
so maybe this control is enougt for you to discover clearly if metern is stuck or not?


then i move to systemd. (i'm using raspberry with raspbian stretch)
i havo not clear the passages to implement this instead of use crontab.

sudo nano /etc/systemd/system/ping_metern.service

Code: Select all

[Unit]
Description=ping_metern
After=metern.target

[Service]
Type=oneshot
ExecStart=ping_metern

[Install]
WantedBy=multi-user.target
and after that how i have to create the timer?

if i try to:

systempi@raspberrypi:/var/www/MyScripts $ systemctl enable ping_metern.timer & systemctl start ping_metern.timer

Code: Select all

[1] 24154
Failed to enable unit: The name org.freedesktop.PolicyKit1 was not provided by any .service files
Failed to start ping_metern.timer: The name org.freedesktop.PolicyKit1 was not provided by any .service files
See system logs and 'systemctl status ping_metern.timer' for details.
[1]+  Uscita 1                systemctl enable ping_metern.timer

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Thu Nov 28, 2019 7:37 pm

1) sure
2) yes that's check time. It always increase sadly ;)
3) Maybe remove After=metern.target in .service

try first the ping_metern script to see if all ok

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

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Thu Nov 28, 2019 9:07 pm

jeanmarc wrote:
Thu Nov 28, 2019 7:37 pm
1) sure
ok, but how? :D
$remotedata = file_get_contents($domoticzURLOn);
because
$domoticzURL=http://$domoticzIP:$domoticzPort/json.htm?type=command&param=switchlight&idx=$signalSwitch&switchcmd=
and i need to put "On" at the end
jeanmarc wrote:
Thu Nov 28, 2019 7:37 pm
3) Maybe remove After=metern.target in .service

try first the ping_metern script to see if all ok
can you plese explain more in detail what file i need to create to enable systemd ?

Code: Select all

[Unit]
Description=ping_metern
After=metern.target

[Service]
Type=oneshot
ExecStart=ping_metern

[Install]
WantedBy=multi-user.target
and

Code: Select all

[Unit]
Description=ping_metern
ping_metern.timer : 
[Timer]
OnBootSec=1min
#OnCalendar=5min
OnUnitActiveSec=5min
Unit=ping_metern.service

[Install]
WantedBy=multi-user.target
are two different type of files, what names i should give and where i need to save them?



jeanmarc wrote:
Thu Nov 28, 2019 7:37 pm
try first the ping_metern script to see if all ok

Code: Select all

pi@raspberrypi:/var/www/MyScripts $ php ping_metern.php
PHP Parse error:  syntax error, unexpected '$domoticzPort' (T_VARIABLE) in /var/www/MyScripts/ping_metern.php on line 8

so i change with this:

Code: Select all

#!/usr/bin/php
<?php
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}

$domoticzIP='192.168.0.105';
$domoticzPort=8085;
$signalSwitch=152;
$domoticzURL="http://$domoticzIP:$domoticzPort/json.htm?type=command&param=switchlight&idx=$signalSwitch&switchcmd=";
$log       = '/tmp/ping_metern.log';

if (file_exists('/dev/shm/mN_LIVEMEMORY.json')) {
	$data     = file_get_contents('/dev/shm/mN_LIVEMEMORY.json');
	$memarray = json_decode($data, true);
	$nowUTC   = strtotime(date("Ymd H:i:s"));
		if ($nowUTC - $memarray["UTC"] > 300) { // too old
		echo("switch on dummy device on domoticz");
		$remotedata = file_get_contents($domoticzURL);
		
		$stamp = date('d/m/Y H:i:s');
	    $data = "$stamp : $nowUTC - $memarray['UTC']\n";
	    file_put_contents($log, $data, FILE_APPEND);
		}
} else { // ain't running
	die("Aborting: mN not running \n");
}
?>
add ; at the end of:
$domoticzIP='192.168.0.105';
$domoticzPort=8085;
$signalSwitch=152;
$domoticzURL="http://$domoticzIP:$domoticzPort/json.htm?type=command&param=switchlight&idx=$signalSwitch&switchcmd=";

but now i have this errors:

Code: Select all

pi@raspberrypi:/var/www/MyScripts $ php ping_metern.php
PHP Parse error:  syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /var/www/MyScripts/ping_metern.php on line 22
thanks

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Fri Nov 29, 2019 8:23 am

Oh try that

Code: Select all

#!/usr/bin/php
<?php
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}

$domoticzIP='192.168.0.105';
$domoticzPort=8085;
$signalSwitch=152;
$domoticzURL="http://$domoticzIP:$domoticzPort/json.htm?type=command&param=switchlight&idx=$signalSwitch&switchcmd=On";
$log       = '/tmp/ping_metern.log';

if (file_exists('/dev/shm/mN_LIVEMEMORY.json')) {
	$data     = file_get_contents('/dev/shm/mN_LIVEMEMORY.json');
	$memarray = json_decode($data, true);
	$nowUTC   = strtotime(date("Ymd H:i:s"));
		if ($nowUTC - $memarray["UTC"] > 300) { // too old
		echo("switch on dummy device on domoticz");
		$remotedata = file_get_contents($domoticzURL);
		
		$memt = $memarray["UTC"];
		$stamp = date('d/m/Y H:i:s');
	    $data = "$stamp : $nowUTC - $memt\n";
	    file_put_contents($log, $data, FILE_APPEND);
		}
} else { // ain't running
	die("Aborting: mN not running \n");
}
?>
From systemd timer, you should have as /etc/systemd/system/ping_metern.service

Code: Select all

[Unit]
Description=ping_metern

[Service]
Type=oneshot
ExecStart=ping_metern

[Install]
WantedBy=multi-user.target
as /etc/systemd/system/ping_metern.timer

Code: Select all

[Unit]
Description=ping_metern
ping_metern.timer : 
[Timer]
OnBootSec=1min
#OnCalendar=5min
OnUnitActiveSec=5min
Unit=ping_metern.service

[Install]
WantedBy=multi-user.target
Then do systemctl daemon-reload after modification.

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

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Fri Nov 29, 2019 9:54 pm

hi JM, i tested your php and it seams that now is without errors:

Code: Select all

pi@raspberrypi:~ $ cd /var/www/MyScripts
pi@raspberrypi:/var/www/MyScripts $ php ping_metern.php
pi@raspberrypi:/var/www/MyScripts$ 
but i still have some trubles with systemd:
the passages that i used:

Code: Select all

pi@raspberrypi:/var/www/MyScripts $ sudo ln -s /var/www/MyScripts/ping_metern.php /usr/bin/ping_metern

Code: Select all

pi@raspberrypi:/var/www/MyScripts $ sudo nano /etc/systemd/system/ping_metern.timer

Code: Select all

[Unit]
Description=ping_metern
ping_metern.timer :
[Timer]
OnBootSec=1min
#OnCalendar=5min
OnUnitActiveSec=5min
Unit=ping_metern.service

[Install]
WantedBy=multi-user.target

Code: Select all

pi@raspberrypi:/var/www/MyScripts $ sudo nano /etc/systemd/system/ping_metern.service

Code: Select all

[Unit]
Description=ping_metern
After=metern.target

[Service]
Type=oneshot
ExecStart=ping_metern

[Install]
WantedBy=multi-user.target

and then:

Code: Select all

pi@raspberrypi:/var/www/MyScripts $ sudo systemctl enable ping_metern.timer & sudo systemctl start ping_metern.timer                                         [1] 30528
Failed to enable unit: File ping_metern.timer: Invalid argument
Failed to start ping_metern.timer: Unit ping_metern.timer is not loaded properly: Invalid argument.
See system logs and 'systemctl status ping_metern.timer' for details.
[1]+  Uscita 1                sudo systemctl enable ping_metern.timer
pi@raspberrypi:/var/www/MyScripts $
i try also to reload:

Code: Select all

pi@raspberrypi:/var/www/MyScripts $ sudo systemctl daemon-reload
pi@raspberrypi:/var/www/MyScripts $

but it seams that if i try to find if ping_metern is runnin on raspberry, it's not lunched:

Code: Select all

pi@raspberrypi:/var/www/MyScripts $ pgrep -u root,pi  ping_metern.php
pi@raspberrypi:/var/www/MyScripts $
what is wrong?
thanks

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Fri Nov 29, 2019 10:12 pm

I see no error with the script, there is just no output when all is ok. If you wish feedback :

Code: Select all

#!/usr/bin/php
<?php
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}

$domoticzIP='192.168.0.105';
$domoticzPort=8085;
$signalSwitch=152;
$domoticzURL="http://$domoticzIP:$domoticzPort/json.htm?type=command&param=switchlight&idx=$signalSwitch&switchcmd=On";
$log       = '/tmp/ping_metern.log';

if (file_exists('/dev/shm/mN_LIVEMEMORY.json')) {
	$data     = file_get_contents('/dev/shm/mN_LIVEMEMORY.json');
	$memarray = json_decode($data, true);
	$nowUTC   = strtotime(date("Ymd H:i:s"));
		if ($nowUTC - $memarray["UTC"] > 300) { // too old
		echo "switch on dummy device on domoticz \n";
		$remotedata = file_get_contents($domoticzURL);
		$memt = $memarray["UTC"];
		$stamp = date('d/m/Y H:i:s');
	   	$data = "$stamp : $nowUTC - $memt\n";
		file_put_contents($log, $data, FILE_APPEND);
		} else {
		echo "mN is running \n";
		}
} else { // ain't running
	die("Aborting: mN not running \n");
}
?>
Sorry ping_metern.timer is

Code: Select all

[Unit]
Description=ping_metern

[Timer]
OnBootSec=1min
#OnCalendar=5min
OnUnitActiveSec=5min
Unit=ping_metern.service

[Install]
WantedBy=multi-user.target

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

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Fri Nov 29, 2019 10:40 pm

hi,
this is the output with new timer, is it correct?

Code: Select all

pi@raspberrypi:/var/www/MyScripts $ sudo systemctl enable ping_metern.timer & sudo systemctl start ping_metern.timer
[1] 32035
Created symlink /etc/systemd/system/multi-user.target.wants/ping_metern.timer → /etc/systemd/system/ping_metern.timer.
Job for ping_metern.timer failed.
See "systemctl status ping_metern.timer" and "journalctl -xe" for details.
[1]+  Completato              sudo systemctl enable ping_metern.timer
pi@raspberrypi:/var/www/MyScripts $ sudo systemctl daemon-reload
pi@raspberrypi:/var/www/MyScripts $ systemctl status ping_metern.timer
● ping_metern.timer - ping_metern
   Loaded: loaded (/etc/systemd/system/ping_metern.timer; enabled; vendor preset: enabled)
   Active: inactive (dead)
how to ckeck if php is running?

Code: Select all

pi@raspberrypi:/var/www/MyScripts $ pgrep -u root,pi  ping_metern.php
pi@raspberrypi:/var/www/MyScripts $
with pgrep i should not see the pid about the process of ping_metern.php if it's running?


last question, i see the log file:
29/11/2019 19:26:48 : 1575052008 -
29/11/2019 21:50:04 : 1575060604 - 1575051893
here what will write the ping_metern.php?


thanks

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Sat Nov 30, 2019 8:23 am

The script will run for a second each 5 minute. Please read about systemd to understand what you're doing :roll:
In /tmp/ping_metern.log you get the errors, you already get 2.

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

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Sat Nov 30, 2019 8:20 pm

jeanmarc wrote:
Sat Nov 30, 2019 8:23 am
The script will run for a second each 5 minute.
hi JM, thank, but how to check if the script are running?
i try with:
pi@raspberrypi:~ $ pgrep -u root,pi ping_metern.php
pi@raspberrypi:~ $

but i do not see the process, thanks

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Sun Dec 01, 2019 7:05 am

There is a problem, check your timer : systemctl status ping_metern.timer
You should see : Active: active (waiting) ...
Please read about timers and systemd.
If you're too lazy do a daemon with the script:

Code: Select all

<?php
while (true) {
// Do things ..
sleep(600);
}
?>
then ping_metern &

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests