jeudi 13 juin 2013

Monitor Raspberry Pi with Mango

Now we have a aspberry Pi working and able to sense at least it's own temperature, we can monitor it with a little Scada  software.

Mango is what I use  to control my home, so I'll try to show how to deal with it.


First we need to know how a Unix system (Mango, on an Alix Board) can poll another Unix system (The Pi) to retreive a value ...
Differents solutions exists but the simplest one is probably an HTTP Request. Scp should be adapted too, with use of a SSH private key.

First let's Add lighthttpd server on Pi to minimize the footprint on this little machine. There is abslutly no difficulty with this :

  • Installation : sudo apt-get install lighttpd
  • Configuration, Adding suport to cgi : vi /etc/lighttpd/lighttpd.conf and add those lines :

server.modules += ( "mod_cgi" )

$HTTP["host"]  =~ "" {
  server.document-root = "/var/www/pi2"
  accesslog.filename         = "/var/log/lighttpd/access.log"
  $HTTP["url"] =~ "/cgi-bin/" {
      cgi.assign = ( ".pl" => "/usr/bin/perl" )

As you can see :
  •  my server respond to request on
  • I have made a directory /var/www/pi2 to store general web pages (not used here)
  • I have also made a subdirectory /var/www/pi2/cgi-bin where the server will accept to consider any .pl file as a Perl doc, and execute it.
  • You can't see it actualy, but it's better to change ownership of this directory : sudo chown www-data:www-data /var/www/pi2/cgi-bin
Now just restart Lighttpd by entering the following command : /etc/init.d/lighttpd restart


Now the server is ready, let's add a little Perl Cgi file to handle the temp requests :
  • vi /var/www/pi2/cgi-bin/
Add those lines :

print "Content-Type: text/plain", "\n\n";
open (TMP,"</sys/bus/w1/devices/28-00000459aa0d/w1_slave") || die "Pb lecture /sys/bus/w1/devices/28-00000459aa0d/w1_slave !\n";
while ($l = <TMP>) {
        chop $l;
        if ($l !~ /t=/) { next;}
        @chaine = split (/ /,$l);
        ($n, $temp) = split (/=/, $chaine[9]);
        print "TEMP=$temp";
close TMP;

For those how had read my previous post, You'll recognize the file where the DS18B20 stores the temperature : /sys/bus/w1/devices/28-00000459aa0d/w1_slave

You'll have to tune it to your conf.

So now when the following URL is called :
It will respond the following text to your navigator : TEMP=32525 ...  for 32.525°C


Now let's configure Mango :
Open It, go to DataSource and add an HTTP retreiver data source. (Sorry, but explain Mango here is much too long, please report to Serotonine Forum for that ...).

Below a print screen of the DS configuration :

I have surrounded the parameters needed to make it operate.

  • The Name of the DataSource : 5 - HTTP or whatever you want
  • The URL called :
  • The Name of the DataPoint : TEMP or whatvever you want
  • The type of the DataPoint : Numeric
  • The regex used to extract the Temperature Value from the Web response, we have to transform TEMP=32525 to 32525, so let write : TEMP=(\d{2,5}), which stand for : I have to extract between 2 and 5 ({2,5}) digits (\d)  situated after the string "TEMP=" ... the value extracted must be surrounded by parenthesis.
As the temperature returned is in millidegrees, we have to multiply it by ... 1000.

We could have done this with perl but let's do it with Mango and Point Links :

Below is a screen whowing the Point Link configuration. I have added a relationship between :

  • my new DataPoint (5 - HTTP - TEMP), where you recognise the DataSource name followed by the DataPoint name.
  • Another DataPoint created for that : 3 - VIRTUAL - TEMP PI2
Virtuals DS are very convenient as they can be modified by something that does'nt need to exists in real life. In Scada software, altering a DataPoint value often means altering a real register in a equipement, and this is not what I want to do here.

What is intersting here is that you can modify the value returned by the first DP before copying it the the second one. This is done by the expression : return source.value / 1000;

Now I can follow the Temp of my Pi at real time, having graphs for the history and of course I can be warned if the value go over a fixed limit. Use the "Event handler" Tab of Mango for that.

in this special case, I have configured my watchdog on Pi to Switch On the Fan when the heatsink reaches 34°C, and switching it OFF when the temp goes below 29°C ... to respect an hysterisis cycle.

Indeed, we actualy can see it throught the following chart :

But if my Watchdog script fails, I will still have a monitoring through Mango and I will be advised if the temperature reaches, for example 45°C, wich is far more than the upper limit of the script, and then I can shut it down.

raspberry pi mango serotonine m2m2 ds18b20 perl monitoring home automation script

Aucun commentaire:

Enregistrer un commentaire