Use PHP Script to Monitor Temperature and Uptime for Raspberry PI in the Browser


The Raspberry PI (so far, three models, A, B and B Plus) is a credit-card size tiny full functional PC that runs Linux (e.g. Raspbian). I have introduced the script to measure the temperature of the board. Here is an article to show you different ways to reduce the temperature of Raspberry PI by using coolers, fans. Under linux, you can use uptime to see how long the server has been running. Suppose, you have installed the apache2server, you can have a PHP page that displays these information by using shell_exec() or exec() function that executes these commands.

8e31a8f226eab9ebe747c85cc7feb45e Use PHP Script to Monitor Temperature and Uptime for Raspberry PI in the Browser HTML php PHP Raspberry PI Technology web programming

The shell_exec runs the linux command and returns all output as a string. However, this might be disabled for security purposes on some webhosting companies, e.g. Fasthosts disables this but Godaddy does not.

1
2
3
<php
    echo shell_exec("uptime");
?>
<php
    echo shell_exec("uptime");
?>

Refresh Every Few Seconds

The above script produces one-snapshot result, meaning that the result is not updated in real time unless you press F5 manually. You could use HTML refresh technique:

1
<meta http-equiv="refresh" content="5" />
<meta http-equiv="refresh" content="5" />

The above should be placed within head of HTML code and will reload the same page every 5 seconds. This can also be done using Javascript:

1
setTimeout(function () { location.reload(1); }, 5000);
setTimeout(function () { location.reload(1); }, 5000);

However, if you don’t want the whole page refreshing, you can use a better solution that is Ajax. Ajax allows you to get content asynchronous, which is considered efficient. The following Javascript is based on JQuery and it updates the HTML element with id status every 5 seconds. The cache=false is recommended because in IE browsers, if you don’t add this, most likely the content will be cached and you will not notice any updates. The ajax calls won’t work if the URL is on some other domain (cross-domain). So you have to ensure that both URLs are at the same domain.

1
2
3
4
5
6
7
8
9
10
11
12
13
   <div style='width:300px' id='status'>
  
  </div>
  
  <script>
    function update() {
       $.ajax({url: "http://192.168.0.100/status.php", cache:false, success: function (result) {
                $('#status').html(result);
                setTimeout(function(){update()}, 5000);
                }});
   }
    update();
  </script>  
   <div style='width:300px' id='status'>
  
  </div>
  
  <script>
    function update() {
       $.ajax({url: "http://192.168.0.100/status.php", cache:false, success: function (result) {
                $('#status').html(result);
                setTimeout(function(){update()}, 5000);
                }});
   }
    update();
  </script>  

The other alternative as to setTimeout is setInterval. Both differ slightly. The setTimeout usually invokes (fires) the function at the given delay (second parameter) once and that is why you put another call inside the called-function to ensure repeatedly function calls at interval. The setInterval will set up the repeatedly function calls at the interval (second parameter) anyway (no need to explicitly call the function again). Both timers can be cleared by clearTimeout or clearInterval respectively.

You can also use Python script to retrieve the CPU temperature of the Raspberry PI: How to Monitor the CPU Temperature of Raspberry PI using Python Script?

Raspberry Pi

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
726 words
Last Post: Block 426 Low CPC Advertiser URLs in Adsense - Using VBScript to Remove Duplicate Domains
Next Post: Add a Next Random Post in WordPress Page Template

The Permanent URL is: Use PHP Script to Monitor Temperature and Uptime for Raspberry PI in the Browser

4 Comments

  1. Steve Nicklen
  2. Atik

Leave a Reply