Aneeshnit
Posts: 9
Joined: Sun May 24, 2015 6:48 am

Python from PHP Button

Sun May 24, 2015 1:43 pm

Hi,
i have this bit of code in python.. i wanted to run it from a php page runnin on pi... its been like weeks i've read all d related post nothin seems to work... can som one help me.. :| i have all the codes saved at /var/www


Python code: Leaf.py
from time import sleep
import RPi.GPIO as G
G.setmode(G.BCM)
G.setwarnings(False)
G.setup(20, G.OUT)
G.setup(21, G.OUT)
G.setup(16, G.OUT)
G.output(20, True)
sleep(1)
G.output(20, False)
G.output(21, True)
sleep(1)
G.output(21, False)
G.output(16, True)
sleep(1)
G.output(16, False)
sleep(1)

Leafpy.php
<?php
exec("sudo python3 /var/www/leaf.py");

?>


Control.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$('#clickTEST').click(function()
{
var a=new XMLHttpRequest();
a.open("GET","leafpy.php");
a.onreadystatechange=function()
{
if(a.readyState==4)
{
if(a.status==200)
{}
else alert("HTTP ERROR");
}
}
a.send();
});
});
</script>

<title>Pi Controller </title>
</head>
<body>
<button type ="button" style="width:200px;height:100px" id="clickleaf">TEST</button><b$
</body>
</html>

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Python from PHP Button

Sun May 24, 2015 2:43 pm

Aneeshnit wrote:its been like weeks i've read all d related post nothin seems to work... can som one help me..
lol, ok, so tell us what you did..
what does "nothin seems to work" means? what's the error?

Aneeshnit
Posts: 9
Joined: Sun May 24, 2015 6:48 am

Re: Python from PHP Button

Sun May 24, 2015 3:22 pm

Massi wrote:
Aneeshnit wrote:its been like weeks i've read all d related post nothin seems to work... can som one help me..
lol, ok, so tell us what you did..
what does "nothin seems to work" means? what's the error?

the button!!!!...it does'nt work... the python prgrm alone works... but d php file to execute it does not..
i've tried everythin.. :cry:

tried adding #! /usr/bin/python3
tried adding all des files to sudoers

bt still nothin..

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Python from PHP Button

Sun May 24, 2015 4:02 pm

Aneeshnit wrote:the button!!!!...it does'nt work... the python prgrm alone works... but d php file to execute it does not..
What
error
do
you
get?
tried adding all des files to sudoers
what?

Aneeshnit
Posts: 9
Joined: Sun May 24, 2015 6:48 am

Re: Python from PHP Button

Sun May 24, 2015 4:06 pm

No error as such...it simply does not run the python prgrm...
Der is a "favicon" not found..wen i chk d php log..

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Python from PHP Button

Sun May 24, 2015 4:51 pm

a proper answer to "doesn't work" can be "mine works"

you don't see errors simply because you are not looking at them.
Your php code has no error checks at all
start from there. Write proper php code and you'll see what's the output of your command.

I also asked you about what you added to sudoers, but even that was too much.

So my code works, good luck with yours.

Aneeshnit
Posts: 9
Joined: Sun May 24, 2015 6:48 am

Re: Python from PHP Button

Sun May 24, 2015 4:58 pm

hey hey...sorry ..sorry... am travelling..


i added www-data ALL=(ALL) NOPASSWD: ALL to sudoers.

am not an expert in php... i found dat
<?php
exec("sudo python /var/www/leaf.py")
?>
should run s python file... but it does nothin..

User avatar
DougieLawson
Posts: 37686
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Python from PHP Button

Sun May 24, 2015 9:08 pm

Aneeshnit wrote: i added www-data ALL=(ALL) NOPASSWD: ALL to sudoers.
That is an enormous security hole.

Giving www-data sudo is a bad idea.
Giving www-data sudo for any possible command or program is an incredibly bad idea.

You need to find any other way to do that stuff. Clue: look at Joan's pigpiod.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Aneeshnit
Posts: 9
Joined: Sun May 24, 2015 6:48 am

Re: Python from PHP Button

Sun May 24, 2015 11:57 pm

DougieLawson wrote:
Aneeshnit wrote: i added www-data ALL=(ALL) NOPASSWD: ALL to sudoers.
That is an enormous security hole.

Giving www-data sudo is a bad idea.
Giving www-data sudo for any possible command or program is an incredibly bad idea

I've read abt dis..bt jst tryin to get d code to wrk frm php/html button... Is it even possible?

User avatar
mattrix
Posts: 133
Joined: Mon Mar 02, 2015 6:20 am
Location: Christchurch, New Zealand
Contact: Website

Re: Python from PHP Button

Mon May 25, 2015 4:49 am

What I would do is run a small server in the python code.
Look at bottle.py (http://bottlepy.org/docs/dev/index.html)

Code: Select all

pip install bottle
GPIO-API.py:

Code: Select all

from bottle import route, run
import RPi.GPIO as GPIO

PIN = 12

GPIO.setmode(G.BCM)
GPIO.setwarnings(False)
GPIO.setup(PIN, GPIO.OUT)

@route('/on')
def on():
    GPIO.output(PIN,True)
    return("ON")

@route('/off')
def off():
    GPIO.output(PIN,False)
    return("OFF")

@route('/status')
def status():
    return GPIO.input(PIN)

run(host='localhost', port=8080)
This will run a small server on port 8080.
So, if you called http://localhost:8080/on it would run your GPIO ON function.

Run this python code on system boot so it's ready to accept requests.
You can usually do this by adding the command to /etc/rc.local
eg: python /var/www/GPIO-API.py

It will run when the Pi boots, setup the GPIO's and then wait for requests.

Then all you need to do is call those URLs from your PHP code or even via Javascript (AJAX request)

index.php

Code: Select all

<?php
$result = file_get_contents('http://localhost:8080/on');
echo($result);

$status = file_get_contents('http://localhost:8080/status');
echo($status);
?>
This way, you don't need to worry about permissions etc for Apache / NGINX.

Essentially you have just setup a nice little API wrapper to do your GPIO functions :)

You could easily expand it to do many other things.
Last edited by mattrix on Sat May 27, 2017 2:41 am, edited 15 times in total.
www.matthuisman.nz

User avatar
DougieLawson
Posts: 37686
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Python from PHP Button

Mon May 25, 2015 7:55 am

mattrix wrote:What I would do, is run a small web server in the python code.
Look at bottle.py (http://bottlepy.org/docs/dev/index.html)
That's one option and certainly a lot less of a security risk than adding www-data to sudoers.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Aneeshnit
Posts: 9
Joined: Sun May 24, 2015 6:48 am

Re: Python from PHP Button

Mon May 25, 2015 6:16 pm

thnks a ton.... DougieLawson and mattrix... i was looking for d solutin for weeks..thnks again..

Aneeshnit
Posts: 9
Joined: Sun May 24, 2015 6:48 am

Re: Python from PHP Button

Sat May 30, 2015 3:03 pm

mattrix wrote:What I would do is run a small server in the python code.
Look at bottle.py (http://bottlepy.org/docs/dev/index.html)

Code: Select all

pip install bottle

hii... :? i ran this code...the php code that contains the url of leaf python executes well from any pc in d network... but wen i call this php from another file nothin happens.. is der any special permsions?

User avatar
Lombra
Posts: 67
Joined: Tue Sep 16, 2014 12:41 am
Location: Sweden

Re: Python from PHP Button

Mon Jun 01, 2015 11:41 am

Not strictly on topic, but thanks for that great introduction to using Bottle, mattrix! Everything I've read about it (and similar methods) before now have been or seemed so needlessly complex.

I would be interested in a simple solution to executing it remotely, too. I don't want to go through any other ports over the internet, so to speak, but have it handled locally on the server. PHP and file_get_contents would do that, but that seems a bit clunky. I've read all these guides on how to setup nginx to do similar things, but again, have seemed needlessly complex. I've not understood how it really worked and given up eventually.

Edit: Now I'm thinking, even something as simple as having nginx locally redirecting requests to a certain folder to localhost:8080 instead. Would that be possible, would that be a suboptimal solution for this purpose somehow?

Code: Select all

example.com/app/light -> localhost:8080/light
Edit2: Well I've just added this to my server config and it works! So unless there are serious flaws with it I'm gonna stick with it.

Code: Select all

    location /light {
        proxy_pass http://localhost:8080;
    }

User avatar
mattrix
Posts: 133
Joined: Mon Mar 02, 2015 6:20 am
Location: Christchurch, New Zealand
Contact: Website

Re: Python from PHP Button

Thu Sep 03, 2015 1:09 am

That's how I would do it :)

I was going to say, just run bottle.py on port 80, but you obviously have another site running already.

I use proxy-pass a lot for services that run on different ports.
www.matthuisman.nz

Return to “Python”