cazz
Posts: 205
Joined: Wed Nov 14, 2012 9:01 pm
Location: Sweden

show information on a webpage

Wed Nov 14, 2012 9:15 pm

Hi
I looking to read information of my Raspberry from a webpage
I have found how I can read the temp but not how much CPU is working (Only find how to see how fast it can do)
I also looking how to see how much space I have on my SD card.

And some other information :)

I have see some project about this but I just looking to keep it simple, basic textinformation.
/Cazz

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: show information on a webpage

Thu Nov 15, 2012 6:48 am

@cazz: I would use Python and the `psutil` module for that. There is RaspberryPi specific information which can't be determined with `psutil` like temperature, model type, or the overvolting flag, but for those you'll find code snippets here in the forums. For the web part I would use Bottle. Both mentioned modules can be installed from the package management of most Linux distributions. On Debian Wheezy the packages are called `python-psutil` and `python-bottle`.

Code: Select all

while not self.asleep():
    sheep += 1

cazz
Posts: 205
Joined: Wed Nov 14, 2012 9:01 pm
Location: Sweden

Re: show information on a webpage

Thu Nov 15, 2012 7:07 am

Thanks for the fast replay

Do you have any information?
I have never work in python

I have some PHP code that read temp and I have work alot with PHP but never python or CGI :)
/Cazz

User avatar
Dweeber
Posts: 606
Joined: Fri Aug 17, 2012 3:35 am
Location: Mesa, AZ
Contact: Website

Re: show information on a webpage

Thu Nov 15, 2012 7:35 am

Displaying info depends on what you are attempting to do and the permissions your web server gives the web site to do.

Example: http://webrpi.tnet.com/status

Displays a number of live info about the Raspberry Pi sitting next to me. Note that this is done in the context that the website is using a PHP wiki system, but it is calling a php module that obtains most of the data from various locations on the unit.

1) Current Temp
Had issues using the normal command from the website, so I just setup a simple cron to obtain the info once a minute and store it in a file. The status.php script outputs the info along with other system info.
The cron script btw gets the current data from the command: /opt/vc/bin/vcgencmd measure_temp but does some parsing to obtain both the Centigrade info (which is default) and Fahrenheit which is calculated from that data (Live in the US more use to F than C)

Code: Select all

* * * * * /usr/local/bin/GetRPiTemp > /www/vhosts/webrpi/currenttemp
2) Uptime Info
Just output from the uptime system call: /usr/bin/uptime

3) Memory
Info from free -mt

4) Disk Space
Info from doing df -h

5) Rest of the text is just text on the wiki page that I created. Doesn't look like a typical wiki because I don't want it to be but it gives me content management that lets me edit stuff without having to FTP/SFTP or whatever to the page.

This could be simply a normal PHP web page.

The page calls a php script which gathers the info from the Raspberry Pi...
status.php script (never intended to be public so pretty raw):

Code: Select all

<?php
// Collect the Data first
# Get current temp from cron updated file
$temp = file("/www/vhosts/webrpi/currenttemp");
$tmp = substr($temp[0],7);
$temp = preg_replace("/'/",'&deg;',$tmp);

# Get uptime info from system call to uptime
$uptime = exec("/usr/bin/uptime");

# Read the current memory output of free -mt
$raw = array();
$handle = popen('free -mt 2>&1', 'r');
#echo "'$handle'; " . gettype($handle) . "\n";
while (!feof($handle)) {
    $raw[] = fgets($handle);
}
pclose($handle);
foreach($raw as $key => $val) {
  if (strpos($val,"Mem:") !== FALSE) {
    list($junk,$trmem,$tumem, $tfmem) = preg_split('/ +/',$val);
  }
  if (strpos($val,"Swap:") !== FALSE) {
    list($junk,$trswap,$tuswap, $tfswap) = preg_split('/ +/',$val);
  }
  if (strpos($val,"Total:") !== FALSE) {
    list($junk,$tmem,$umem, $fmem) = preg_split('/ +/',$val);
  }
}

// Okay got all the data... lets display it
echo "<ul>";
echo "<li>Uptime:" . $uptime . "</li>";
echo "<li>RPi Internal Temp: " . $temp . "</li>";
echo "<li>Memory Split: 240/16</li>";
echo "</ul>";
echo '<h2>Memory</h2><table class="status" style="width: 60%;">
<tr><th>Type</th><th>Total</th><th>Used</th><th>Free</th></tr>
<tr><td>Real</td><td>' . $trmem . 'mb</td><td>' . $tumem . 'mb</td><td>' . $tfmem . 'mb</td></tr>
<tr><td>Swap</td><td>' . $trswap . 'mb</td><td>' . $tuswap . 'mb</td><td>' . $tfswap . 'mb</td></tr>
<tr><td>Total</td><td>' . $tmem . 'mb</td><td>' . $umem . 'mb</td><td>' . $fmem . 'mb</td></tr>
</table>';
There are a lot of different ways you can do this... this is just an example.

Another page on the same site obtains data from a weather station that is sitted on site. Information from it is gathered in a similar fashion. http://webrpi.tnet.com/weather The data is updated once a minute from the Davis Vantage Pro+ Weather station's raw data.
Dweeber A.K.A. Kevin...
My RPI Info Pages including Current Setup - http://rpi.tnet.com

cazz
Posts: 205
Joined: Wed Nov 14, 2012 9:01 pm
Location: Sweden

Re: show information on a webpage

Thu Nov 15, 2012 8:09 am

ohh thanks alot :)

have to look at it later when I'm home :)
/Cazz

Return to “General programming discussion”