Some home automation

Talks about metering
giulio
Posts: 22
Joined: Tue Aug 28, 2018 1:37 pm

Re: Some home automation

Post by giulio » Sat Nov 24, 2018 3:31 pm

Yes, I need to continuously monitor the meter
which measures the kw taken from the external supplier and activate something when it exceeds a threshold and deactivate when below the threshold

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

Re: Some home automation

Post by jeanmarc » Sat Nov 24, 2018 5:34 pm

You may adapt it to your needs (/srv/http/comapps/ path) :
over_daemon.php :

Code: Select all

<?php
/*
php /srv/http/comapps/over_daemon.php { start | stop }
*/

if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}

if (file_exists('/dev/shm/over_daemon.pid')) {
    $cdpid = (int) file_get_contents('/dev/shm/over_daemon.pid');
    exec("ps -ef | grep $cdpid | grep over_daemon", $ret);
    if (!isset($ret[1])) {
        $cdpid = null;
        unlink('/dev/shm/over_daemon.pid');
    }
} else {
    $cdpid = null;
}

if (isset($argv[1])) {
    if ($argv[1] == 'start') {
        if (is_null($cdpid)) {
            $command = 'php /srv/http/comapps/over_daemon_loop.php' . ' > /dev/null 2>&1 & echo $!;';
            $cdpid     = exec($command);
            file_put_contents('/dev/shm/over_daemon.pid', $cdpid);
        } else {
            echo "over_daemon seem to be running as $cdpid";
        }
    } else if ($argv[1] == 'stop') {
        if (!is_null($cdpid)) {
            $command = exec("kill $cdpid > /dev/null 2>&1 &");
        }
        unlink('/dev/shm/over_daemon.pid');
    } else {
        echo "Usage : over_daemon {start | stop}\n";
    }
} else {
    echo "Usage : over_daemon {start | stop}\n";
}
?>
over_daemon_loop.php :

Code: Select all

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

$MNDIR  = '/srv/http/metern'; // Path to meterN
$METNUM = 1; // meter/sensor number
$MAXVAL = 150; // eg 150 W
$MAXCMD = 'http://192.168.1.139/control?cmd=GPIO,12,1'; // Do something
$UNDCMD = '';

define('checkaccess', TRUE);
include("$MNDIR/config/config_main.php");
include("$MNDIR/config/config_met$METNUM.php");
include("$MNDIR/config/memory.php");
date_default_timezone_set($DTZ);

while (true) {
    if (file_exists($LIVEMEMORY)) {
        $data   = file_get_contents($LIVEMEMORY);
        $array  = json_decode($data, true);
        $nowutc = strtotime(date('Ymd H:i:s'));
        
        if ($nowutc - $array['UTC'] < 15 && isset($array['UTC']) && $array["${'METNAME'.$METNUM}$METNUM"] > $MAXVAL) {
            $ch = curl_init($MAXCMD);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 5000); // error
            curl_exec($ch);
            curl_close($ch);
            //exec("$MAXCMD", $output);
        } else {
            $ch = curl_init($UNDCMD);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 5000); // error
            curl_exec($ch);
            curl_close($ch);
            //exec("$UNDCMD", $output);
        }
    }
    usleep(500000);
}
?>

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

Re: Some home automation

Post by giulio » Sun Dec 02, 2018 4:51 pm

sorry jeanmarc, explain to me how to make the scripts work,
can I give the stop and start command?

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

Re: Some home automation

Post by jeanmarc » Sun Dec 02, 2018 7:14 pm

Code: Select all

ln -s  /path to/over_daemon.php /usr/bin/over_daemon
over_daemon start

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

Re: Some home automation

Post by giulio » Tue Dec 04, 2018 4:16 pm

with command
over_daemon start

I get this
JARVIS: MyScripts -> over_daemon start
/ usr / bin / over_daemon: line 1:? php: No such file or directory
/ usr / bin / over_daemon: line 2: / bin: Is a directory
/ usr / bin / over_daemon: line 3: stop: command not found
/ usr / bin / over_daemon: line 4: ups /: Is a directory
/ usr / bin / over_daemon: line 6: syntax error near unexpected token `$ _SERVER ['REMO TE_ADDR'] '
/ usr / bin / over_daemon: line 6: `if (isset ($ _ SERVER ['REMOTE_ADDR'])) {'

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

Re: Some home automation

Post by jeanmarc » Tue Dec 04, 2018 4:43 pm

You might change the top line of the scripts and install php :lol:

Code: Select all

#!/usr/bin/php
<?php
..

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest