1

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

1
  • without sudo you cannot read I2C and used my script... Commented Jan 12, 2016 at 21:15

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

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().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.