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: 234
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: 35517
Joined: Sun Jun 16, 2013 11:19 pm
Location: 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.
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
Mettauk
Posts: 234
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”