Linux command via PHP

Moderator: Telldus

Post Reply
GMawston
Posts: 10
Joined: Fri Mar 17, 2023 9:45 am

Linux command via PHP

Post by GMawston »

Well I have the Home Easy HE803s running now with no problems via the commandline and I have created a very simple index.php page for local and remote control when I am away from home but no matter what I try I cannot get the button to work.

To rule out some parts:

1. The php works ok for a Python command - controlling the Pi Face interface
2. The command lines work

I know a lot of you might be thinking why not use the Telldus Centre but I want to build this all from scratch as a learning curve.

The code below is my index.php file

Code: Select all

<?php


?>
<html>
<head>
<?php 
if (isset($_POST['RedON']))
{
exec("python /var/www/pifaceon.py");
}

if (isset($_POST['RedOFF']))
{
exec("python /var/www/pifaceoff.py");
}

if (isset($_POST['plugon']))
{
exec("tdtool -n 1");
}

if (isset($_POST['plugoff']))
{
exec("tdtool -f 1");
}



?>

  <title>Turn the lights on</title>
</head>
<body>
<form method="post">
  <table
 style="width: 75%; text-align: left; margin-left: auto; margin-right: auto;"
 border="0" cellpadding="2" cellspacing="2">
    <body>
      <tr>
        <td style="text-align: center;">Turn Lamp On</td>
        <td style="text-align: center;">Turn Lamp Off</td>
	<td style="text-align: center;">Turn Plug1 On</td>
	<td style="text-align: center;">Turn Plug1 Off</td>
      </tr>
      <tr>
        <td style="text-align: center;"><button name="RedON">Lamp On</button></td>
        <td style="text-align: center;"><button name="RedOFF">Lamp Off</button></td>
        <td style="text-align: center;"><button name="plugon">Plug On</button></td>
        <td style="text-align: center;"><button name="plugoff">Plug Off</button></td>
      </tr>

    </body>
  </table>
</form>

<p>
Hello there!
<?

?>
</p>


</body>
</html>
If anyone else can help on it would be appreciated. I hazard a guess there is a very simple thing I am missing so if anyone has done this basic step before please let me know.

All the best

Gavin
Zapper
Posts: 212
Joined: Fri Mar 17, 2023 9:45 am
Location: Där det är kallt

Re: Linux command via PHP

Post by Zapper »

You could probably look at the simple script I made for XBMC (And a simple web based remote)
http://www.telldus.com/forum/viewtopic.php?f=8&t=19679
davka003
Posts: 187
Joined: Fri Mar 17, 2023 9:45 am
Location: Sweden
Contact:

Re: Linux command via PHP

Post by davka003 »

Without any knowledge about php I belive your problem is that the user running the webserver doesn't have permissions to use the tellstick.
If I remember correctly it should be added to the plugdev group or something like that.
My home automation software: Automagically @ Raspberry Pi http://automagically.weebly.com
GMawston
Posts: 10
Joined: Fri Mar 17, 2023 9:45 am

Re: Linux command via PHP

Post by GMawston »

Zapper, the link does not go to the post. Would love to see the code.

Davka, that sounds very plausable because I think the php code is pretty sound. If there is any example code or reference posts let me know.
Zapper
Posts: 212
Joined: Fri Mar 17, 2023 9:45 am
Location: Där det är kallt

Re: Linux command via PHP

Post by Zapper »

http://www.telldus.com/forum/viewtopic.php?f=8&t=3233 Hope this link works, the post is at the end.
GMawston
Posts: 10
Joined: Fri Mar 17, 2023 9:45 am

Re: Linux command via PHP

Post by GMawston »

Hello Zapper

Yes it did work and having read your code I managed to pin the problem down very quickly.

I had not explictly stated where the Tdtool was residing so changing it did the trick.

Essentially this:

Code: Select all

exec("tdtool -n 1");
Became this:

Code: Select all

exec("/usr/local/bin/tdtool -n 1");
Such a small change made all the difference.

Thanks again Zapper.
Zapper
Posts: 212
Joined: Fri Mar 17, 2023 9:45 am
Location: Där det är kallt

Re: Linux command via PHP

Post by Zapper »

No problem, I'm not sure where the default cwd of exec in php is. It might be in the current dir you are running the script from. And exec has no environment so it doesn't know where to look for commands.
Post Reply