nateshnp
Posts: 4
Joined: Thu Nov 24, 2016 7:39 am

returning value from python to php

Thu Nov 24, 2016 1:22 pm

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.
Last edited by nateshnp on Fri Nov 25, 2016 3:34 pm, edited 1 time in total.

richrarobi
Posts: 271
Joined: Sun Feb 08, 2015 1:13 pm

Re: returning value from python to php

Thu Nov 24, 2016 9:22 pm

may not be relevant, but fom a quick google.....
Make sure PHP has permission to excecute that file.

nateshnp
Posts: 4
Joined: Thu Nov 24, 2016 7:39 am

Re: returning value from python to php

Fri Nov 25, 2016 2:35 am

I have given chmod +x for the python file.

User avatar
Mettauk
Posts: 238
Joined: Mon Dec 10, 2012 12:40 pm
Location: Zarg

Re: returning value from python to php

Sun Dec 04, 2016 8:35 am

Have a look at this thread, it covers permissions and DHT output to web pages viewtopic.php?f=32&t=92629&p=655851#p655851
As humans we have been the same for a very very long time, technology changes how we do... not who we are as people.

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

Re: returning value from python to php

Sun Dec 04, 2016 9:43 am

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.
Criticising any questions is banned on this forum.

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

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
Mettauk
Posts: 238
Joined: Mon Dec 10, 2012 12:40 pm
Location: Zarg

Re: returning value from python to php

Sun Dec 04, 2016 9:58 am

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?
As humans we have been the same for a very very long time, technology changes how we do... not who we are as people.

Return to “Python”