1

So I'm wondering, I found a script online that outputs the temperature of the raspberry pi every 2 seconds:

import os
import time

def measure_temp():
        temp = os.popen("vcgencmd measure_temp").readline()
        return (temp.replace("temp=",""))

while True:
        print(measure_temp())
        time.sleep(1)

I've also created a server and site using the standard apache method. In /var/www/html I have a file called 'phptest.php' that will makeup the site and I have a python file called 'monitor-temp.py' which contains the above code for outputting the temp.

My question is, how do I successfully add the python code to the php file and hence display the temperature on the site?

Can I literally just type out the python code into the php file using nano phptest.php? Or do I the php file to somehow access the python file.

I see a lot of guides saying to add this to the php file to execute the python code:

<?php
    $command = escapeshellcmd('/usr/custom/test.py');
    $output = shell_exec($command);
    echo $output;
?>

but that doesn't do anything for me. Thank you in advance!

Varattican
  • 11
  • 1

2 Answers2

0

The Python script you provided is using vcgencmd CLI utility, nothing more. You can run it directly from PHP (no Python needed):

<?php
// escape shell metacharacters
$command = \escapeshellcmd('vcgencmd measure_temp');

// execute vcgencmd CLI utility directly; write it's output to a variable
$output = \shell_exec($command);

// remove redundant prefix 'temp='
$output = \str_replace('temp=', '', $output);

echo $output;

If you still want to do it via Python script, you either need to specify Python interpreter:

<?php
$command = \escapeshellcmd('python3 /path/to/script.py');
$output = \shell_exec($command);
echo $output;

or follow instructions from this answer.

krlv
  • 2,310
  • 1
  • 12
  • 15
  • Wow thank you so much! You're an angel. I have no doubts that this is exactly the code I need, however on my site i get an error: VCIH initialisation fail". Searching for a fix online tells me I need to "add the correct video group to the user". I then run 'sudo usermod -aG video ' but it doesn't seem to fix it. Do you know anything about this? Thanks again – Varattican May 23 '20 at 11:16
  • As Apache is running under the "www-data" user and group you need to to add the video group to this user: `sudo usermod -aG video www-data`. Apache restart might be required after that `sudo service apache2 restart` @Varattican Let me know if it helps. – krlv May 24 '20 at 07:57
0

If you are just getting started and are looking for simplicity and something that just works, you may not be aware that PHP includes a simple web server that you can run without needing to set up and configure Apache at all.

This script will get the Raspberry Pi temperature and display it. Save it as server.php:

<?php
   echo '<pre>';
   $output = system("/usr/bin/vcgencmd measure_temp");
   echo '</pre>';
?>

And you can run it as a web server from the Terminal without Apache or anything else like this:

php -S 0.0.0.0:8000 server.php

Find it in your web browser at:

http://RASPI_IP_ADDRESS:8000
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432