Some home automation

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

Re: Some home automation

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

lueila wrote:
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
I can answer myself, ;)
I found it in the code in daemon.php --- $command = 'php /srv/http/comapps/over_daemon_loop.php' :lol:

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

Re: Some home automation

Post by lueila » Thu May 23, 2019 6:02 pm

Hi jeanmarc

Followed all instructions and I found problem on over.timer.
systemctl start over.timer and received ---> job for over.timer failed. See Systemctl status over.time dgt it and received

over.timer - over_script timer
loaded: loaded (etc/systemd/system/over.timer; enable; vendor present: enable)
Active: Inactive (dead)

(colour red) > over.timer: refusing to start, unit to trigger not loaded
Failed to start over_script timer.

Checked systemctl enable over.timer and it created the symlink to etc/systemd/system/over.target.wants/over_service->etc/systemd/system/over.service

is this msg complicate to solve? :|
tks a lot

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

Re: Some home automation

Post by jeanmarc » Thu May 23, 2019 7:17 pm

You need to create systemd service and timer, depending on your distros the path may vary. On Arch as root it's in /etc/systemd/system/

over_script.timer

Code: Select all

[Unit]
Description=over_script

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

[Install]
WantedBy=multi-user.target
over_script.service

Code: Select all

[Unit]
Description=over_script
Requires=network.target
After=network.target php-fpm.service

[Service]
Type=oneshot
ExecStart=over_script.php

[Install]
WantedBy=default.target
Try then the service alone: systemctl start over_script
Check systemctl status over_script
If it's ok enable and start the timer : systemctl enable over_script.timer , systemctl start over_script.timer

Read out the Arch wiki, it's pretty explicative.

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

Re: Some home automation

Post by lueila » Thu May 23, 2019 9:57 pm

Hi jeanmarc
of course I did them as read on the first page on this thread and modify them as per instruction read in wiki.
Timer worked only with enable and some errors if dgt start.
Anyway I used the last two but the same errors and after some tests I found the mistake, stupid mistake :evil:
now it's ok :D
thks a lot

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

Re: Some home automation

Post by lueila » Sat May 25, 2019 10:18 pm

Hi jeanmarc
Today I did some tests with GPIO and connected a led just to see if it switch on when the power exceed the limit in $MAXVAL and switch off when the power return below the level $MAXVAL.
I did two script sh one to close and one to open the contact.
I noted strage conditions.

Led blinking one times (switch on and switch off) per minute.

I deleted the command in $UNDCMD (I left the action only in $MAXCMD) and in this case the led switch on both the power is higher and power is lower the value in $MAXVAL.

Could be an erron in the over_script code? what do u think?

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

Re: Some home automation

Post by jeanmarc » Sun May 26, 2019 1:26 pm

Hi Lueila,

Did you edit the script like so :

Code: Select all

...
    if ($nowutc - $array['UTC'] < 15 && isset($array['UTC']) && $array["${'METNAME'.$METNUM}$METNUM"] > $MAXVAL) {
	    $ch = curl_init($MAXCMD);
	    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3000); // error
	    curl_exec($ch);
	    curl_close($ch);
        //exec("$MAXCMD", $output);
    } else {
 	    $ch = curl_init($UNDCMD);
	    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3000); // error
	    curl_exec($ch);
	    curl_close($ch);
        //exec("$UNDCMD", $output);
    }

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

Re: Some home automation

Post by lueila » Sun May 26, 2019 4:38 pm

Thks for reply
yes, I did it, following over_script complete:

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/comapps'; // Path to meterN
$METNUM = 6; // meter/sensor number
$MAXVAL = 100; // eg 100 W.    Test 100W
$MAXCMD = shell_exec(' /var/www/comapps/ch12.sh'); // Close contact GPIO12
$UNDCMD = shell_exec(' /var/www/comapps/op12.sh'); // Open contact  GPIO12

// No edit should be needed bellow
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}
define('checkaccess', TRUE);
include("/var/www/metern/config/config_main.php");
include("/var/www/metern/config/config_met$METNUM.php");
include("/var/www/metern//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, 3000); // error
	    curl_exec($ch);
	    curl_close($ch);
        //exec("$MAXCMD", $output);
    } else {
	    $ch = curl_init($UNDCMD );
	    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3000); // error
	    curl_exec($ch);
	    curl_close($ch);
        //exec("$UNDCMD", $output);
    }
?>
could be daemon_script_loop conflict with over_script?
Tks a lot

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

Re: Some home automation

Post by jeanmarc » Sun May 26, 2019 6:10 pm

Well if you use com_daemon you should not use a systemd timer or cron tab job.
You should choose one method: com_daemon run as fast as possible a systemd timer/cron tab job should be slower.

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

Re: Some home automation

Post by lueila » Mon May 27, 2019 9:50 pm

Hi jeanmarc
now it's working :) at moment I need to find the best setting about the loop, now I have delay about 35 sec (when the power exceed and GPIO close) I tried with daemon, systemd timer and also rs.local but the delay still remain abut 27/40 sec.
Maybe that it's depend on the system used for loop, I'll do other test.
tks a lot :D

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

Re: Some home automation

Post by jeanmarc » Tue May 28, 2019 4:58 am

you may play with sleep or usleep.. I belive a daemon is cleaner for a script that run continuously.
what did you command btw ? ;)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest