I have a python script who is returning 2 values, humidity and temperature:
Temperature: 1.58050537109
Humidity: 89.2761230469
I call this srcipt from a PHP page but only one value is printed
<?php
echo exec("sudo python /home/pi/sht/sht31.py 2>&1");
?>
and only one humidity is printed. Could you please help me to print both value ? Thank you
-
without sudo you cannot read I2C and used my script...jimbolino– jimbolino2016年01月12日 21:15:05 +00:00Commented Jan 12, 2016 at 21:15
2 Answers 2
If you call exec() with only command string, then it will return only the last line of output. To capture everything call it with second parameter to get all output:
exec('command', $output);
var_dump($output);
answered Jan 12, 2016 at 20:45
Tomasz Kowalczyk
10.5k6 gold badges56 silver badges70 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
now output is :
array(5) { [0]=> string(5) "False" [1]=> string(4) "True" [2]=> string(5) "False" [3]=> string(26) "Temperature: 1.56982421875" [4]=> string(23) "Humidity: 89.3859863281" }
with this code :
exec('sudo python /home/pi/sht/sht31.py' , $output);
var_dump($output);
1 Comment
Steve E.
Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().
default