Add CPU temperature to PiAware/SkyAware local web page?

I was thinking about modding the PiAware/SkyAware web page to include Pi CPU temperature as reported by “/opt/vc/bin/vcgencmd measure_temp“.

Is that already available somehow? I tried searches to see but haven’t found anything.

I can ssh in and run the command myself, but to have CPU temperature available and updating like the aircraft stats on the live data web page makes it immediately available and maybe helpful to many as the northern hemisphere heats up for summer.

3 Likes

This is a good idea and should actually be easy to implement as part of the sdcard image’s status page; I created a ticket to look at it.

3 Likes

Did this already on my own since I’ve installed PHP 7.2 for some other stuff that’s running on my Pi. This way the vcgencmd is executed via a simple php script file, included in the renamed index.php and displayed on load of the page. I don’t need frequent checks on that - the environmental conditions are stable.

There are other ways to implement such a feature (you won’t need PHP for that).

If you’re interested, I can put a How-To together quickly.

4 Likes

A how-to would be most excellent, @Joschi73!

That’s exactly what I was asking for. If it isn’t too much load on the system, I might adapt to where it updates each map and data update, but refreshing the page is fine too.

Thanks!

I’m using a Pi3 B+ 1.2 with a mostly regular PHP7.2 install running in CGI. Only changed web port for lighttpd. Rest is pretty much standard.

I’d like to point out that all of the stuff could be done as shell script (or using Python). I’ve seen some solutions on the web where a JSON file is created using crontab which is then simply read through JavaScript.

Update/Upgrade system, install PHP, enable the module in lighttpd and reload the web server:
sudo apt update
sudo apt upgrade
sudo apt install php7.2-cgi
sudo lighty-enable-mod fastcgi-php
sudo /etc/init.d/lighttpd force-reload

Head over to:
/usr/share/dump1090-fa/html/

Create PHP file:
sudo touch pistats.php

Paste the following contents:
https://pastebin.com/cESaqRpA

Rename index.html to index.php

Add on top of index.php:

<?php
include('pistats.php');
?>

Search for ‘buttonContainer’ (below infoHeading). Add the following line above ‘clock_div’:

<div class="piStats">Pi Stats: <?=$temp;?>&deg;C | <?=$freq;?> MHz | Throttled: <?=$thro;?></div>

Open style.css and search for ‘.dateTime’. Change to:

.dateTime, .piStats

Result:

Notes:

  • The *exec() commands are disabled in php.ini by default. You need to remove the command from the disable_functions list. The php.ini is located at /etc/php/7.2/cgi/php.ini but may differ on your system.
  • The footprint of PHP 7.2 is small, but you can change memory size via php.ini. Im running node.js and angular.js aswell on the system for other purposes and it doesn’t affect tracking/feeding at all.
  • If throttling status returns something, it may bust the space. Just put it into an alt/title tag or leave it out. Didn’t test this yet since I don’t have any issues with my Pi (Hot summer + attic might change that) :kissing_smiling_eyes:
  • I’m running buster with PiAware package.
  • Uncomment ini_set in pistats.php if you’re having issues (especially with exec() commands).
  • You need to edit the files using sudo nano.
  • If there’s no output, you might need to take an additional step which I intentionally left out.
  • For the imperial lads: add $temp = intval($temp) * 9/5 + 32; after line 6 in pistats.php to get Fahrenheit.

Best regards!

6 Likes

Oddly enough, *exec was not listed anywhere near the disabled_functions string in the php.ini file. I can only assume it’s disabled somewhere else given the output I’m seeing? Then again, by default it’s picking up the CPU Freq but nothing else.

Edit/Add:
Notice : Undefined offset: 1 in /usr/share/dump1090-fa/html/pistats.php on line 29
Notice : Undefined offset: 1 in /usr/share/dump1090-fa/html/pistats.php on line 32

Meaning…it’s picking up null for both vcgencmd polls. vcgencmd works fine through cmdline.

$temp = shell_exec('cat /sys/class/thermal/thermal_zone0/temp');
$temp = substr($temp, 0, 2);

Works for temp

OK, sorted for me at least:

$temp = exec('vcgencmd measure_temp');
...
$output = exec("vcgencmd get_throttled");

Had some issues running shell through php for some reason…I suspect permissions issue or something along those lines.

Thanks, @Joschi73! I‘ll go through this later today and see how it goes.

Thanks for your feedback. The offset notice might be thrown due to different value from the get_throttled command. It’s always a good thing to go through the output of the exec command itself.

As for the exec() commands I fixed my typo above. It’s disable_functions in php.ini. You can check with a info.php file (contents: <?php phpinfo(); ?>) and a search for that directive. Here’s my output, I removed exec from that list:

You might need to try if exec() or shell_exec() works on your system. Same with or without sudo.

I recommend leaving out the //Throttle part of pistats.php (just remove it) and remove the variable in index.php. It might throw notices/errors on your personal system.

Anyways I put that stuff together quickly. I will create a better version using a shell script executed by crontab writing to a JSON file that can be parsed with JavaScript. Lesser footprint, no PHP and easier to incorporate.

1 Like

That was exactly it on my machine (Buster package build, not pre-canned image) - changing the vcgencmd polls to exec() from shell_exec() did the trick. Shell was polling NULL for the vcgencmd polls, I can only assume due to permissions (I did add www_data to the video group, but that didn’t help).

Also, not that it effects most, but non raspbian users would require something other than relying on vcgencmd, I pasted an alternative to pick up temperature that should work for other OS flavors, but polling throttle is only for Pi users AFAIK.

From above, an alternative to vcgencmd for polling temp:

$temp = shell_exec('cat /sys/class/thermal/thermal_zone0/temp');
$temp = substr($temp, 0, 2);
1 Like

I worked out a different solution without PHP as mentioned above. And as mentioned, this only works for PiAware. You’ll only need to install jq as a requirement:

sudo apt update
sudo apt install jq

Create the file pistats-fa.sh in your home directory (/home/pi):

touch pistats-fa.sh

and make it executable

chmod +x pistats-fa.sh

Open the file with

nano pistats-fa.sh

and paste the contents from pistats-fa.sh - Pastebin.com

Please check the WEBROOT variable matching the root directory of the lighttpd webserver. It is set to the default value where PiAware is run. Change to dump978-fa if neccessary (or your personal location).

Next we add the shellscript to crontab and set it to be executed every minute. Open the root user crontab with

sudo crontab -e

If you’re asked which editor you might want to use, go for nano. Next, add the following line to the end of the file:

* * * * * /home/pi/pistats-fa.sh

Save and close the file and you should see a confirmation message.

Move to /usr/share/dump1090-fa/html (or dump978-fa) and execute

sudo touch pistats.js

Open the file with

`sudo nano pistats.js’

and paste the contents from pistats.js - Pastebin.com

Important:
Please review the $( ".buttonContainer" ).prepend line. Open up the console on your webpage by hitting F12 and perform a search for that classname. You might need to change it to a different value depending on your installation. Either .classname or #idname.

Open index.html with

sudo nano index.html

and add the following line below line 25 (use Alt-C to enable line numbers in nano):

<script type="text/javascript" src="pistats.js"></script>

That’s all. Meanwhile the pistats.json file should already be in your web server folder. Refresh your website and it should look like this (PiAware 3.8.1 on Raspian Buster):

If you hover over “Pi:” your Pi model with version number will be shown. If you hover over the throttle value, I’ve put some basic information together. I’ll do a hex-to-byte conversion maybe later and make it more fancy. Or remove it. :grin:

Have fun!

2 Likes

I’m not getting prepend or anything from the pistats.js. I checked the script, it is creating and populating the json file, but seems to stop at the pistats.js and I’m horrible with js, not sure how to debug on my own. I did add the script-type, etc per the above and double-checked everything else. cron is doing it’s thing, etc.

EDIT:
OK, it does show up if I follow the FA link to :8080, but from package install, my map defaults to dump1090.php

Ok. The jQuery prepend function is attached to the class .buttonContainer, which is a wrapping element. Prepend injects on top of all other elements inside the given element. If .buttonContainer doesn’t exist, nothing will happen.

Debug: open your browsers console by hitting F12. Search for an wrapping element end get its class or id attribute. Change .buttonContainer in pistats.js to either .classname or #idname.

With a little HTML/CSS knowledge you can move and format the information to your likings.

1 Like

Tried to set this up. I get the new text in my browser but it doesn’t show the new values in the json file unless I explicitly hit reload on the browser but I do see the timestamp updating every second. I know the cron is working because I can see the file pistats.json update. I didn’t grok the part about modifying the file to either .classname or #idname but I was able to modify pistats.js so it also prints the temp in F as follows:

Pi: 22.2°C 72.2°F | 1222.2 MHz | 2.2220V | 0x0 6/27/2020, 1:20:04 AM

What part am I missing?