Page 1 of 1

returning value from python to php

Posted: Thu Nov 24, 2016 1:22 pm
by nateshnp
Hi,

I have a python code which gives me the temperature and humidity using DHT11. I have attached the code of the python script below

Code: Select all

import Adafruit_DHT as dht
import sys
h,t = dht.read_retry(dht.DHT11,4)
print 'Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(t, h)
temp = 'Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(t, h)
print temp
I have the PHP code as

Code: Select all

<?php
        //$command = escapeshellcmd('python nattemp.py');
        //$output = shell_exec($command);
        exec('sudo  python /home/pi/natsample/nattemp.py',$output,$retval);
        echo "<prev>";
        var_dump($output);
        print_r("My Farm");
        print_r(" out  ".$output);
        print_r(" ret  ".$retval);
        echo "</prev>";
?>
when i run the PHP i get the below out put as

array(0) { } My Farm out Array ret 1

Can you please help me what i have done wrong.

Re: returning value from python to php

Posted: Thu Nov 24, 2016 9:22 pm
by richrarobi
may not be relevant, but fom a quick google.....
Make sure PHP has permission to excecute that file.

Re: returning value from python to php

Posted: Fri Nov 25, 2016 2:35 am
by nateshnp
I have given chmod +x for the python file.

Re: returning value from python to php

Posted: Sun Dec 04, 2016 8:35 am
by Mettauk
Have a look at this thread, it covers permissions and DHT output to web pages viewtopic.php?f=32&t=92629&p=655851#p655851

Re: returning value from python to php

Posted: Sun Dec 04, 2016 9:43 am
by DougieLawson
Why do you need sudo to run that program? It's an exceptionally bad idea to give www-data (which is the user ID your webserver runs with) sudo authority.

See if you can find a way to run the sensor reading program without needing root privileges.

Re: returning value from python to php

Posted: Sun Dec 04, 2016 9:58 am
by Mettauk
My limited understanding from way back when, is it only gives permission for www-data to access that file to be read. But as I write this reply I am a little concerned!? How should the python script output be displayed on a php page?