Some home automation

Talks about metering
giulio
Posts: 36
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: 1858
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: 36
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: 1858
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: 36
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: 1858
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
..

lueila
Posts: 16
Joined: Wed May 15, 2019 9:40 am

Re: Some home automation

Post by lueila » Tue May 21, 2019 11:24 am

Hi Jeanmarc good day
thank you very much to share your work with all (and me :D :D )
I'm interested to have a control on power and give command to gpio when it exceed , I read this thread and
If I understood the first thing is to prepare file over_script.php with my details and
for me it is not clear if I must put it in metern or in comapps (I dont have any folder under srv)

1. ln -s /var/www/comapps/over_script.php /usr/bin/over_script
2. ln -s /var/www/metern/over_script.php /usr/bin/over_script

In metern I have 6 meter sensors (1 energy-2 current-3 frequency-4 voltage-5 cospi-6 power) and I thought it (not sure for MAXCMD & UNDCMD)

Code: Select all

#!/usr/bin/php
?php
// A simple script to command something if a meter or a sensor exceeded a live value.

$MNDIR  = '/var/www/metern'; // Path to meterN
$METNUM = 6; // meter/sensor number
$MAXVAL = 2500; // eg 2500 W
$MAXCMD = 'http://192.168.1.150/control?cmd=GPIO,12,1'; // Do something
$UNDCMD = 'http://192.168.1.150/control?cmd=GPIO,12,0'; 


// No edit should be needed bellow
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}
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);

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);
    }
?>

After this step I should check if it works and if it's working go ahead with other points, is it correct?

Thank you very much for your time
lueila

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

Re: Some home automation

Post by jeanmarc » Tue May 21, 2019 1:14 pm

Hi,
I never used rpi GPIO myself so i can't tell much about the commands ;)

You should only make one link to your com app. It should not be located into mN directory.
That's should then be ln -s /var/www/comapps/over_script.php /usr/bin/over_script

Depending on your need, you could also take a look at 'prod_over_cons_script'.

lueila
Posts: 16
Joined: Wed May 15, 2019 9:40 am

Re: Some home automation

Post by lueila » Tue May 21, 2019 11:52 pm

Tks and understood, my msg was only to know what I can do step by step and if the code is correct i.e. if meter sensor 6 is correct for my configuration.
It could be interesting if Giulio solved it and found the right way.

lueila
Posts: 16
Joined: Wed May 15, 2019 9:40 am

Re: Some home automation

Post by lueila » Thu May 23, 2019 6:11 am

Hi jeanmarc
maybe I found the way to control GPIO, I need to have your confirm if:
1. script over_deamon.php should be in comapps (with over_script.php) and create the ln-s to usr/bin
2. script over_demon_loop.php should be also in comapps?

are those the right location? tks a lot
lueila

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests