SVG Dashboard

luca 22/01/2012 13

PHP and Pachube

To get data from Pachube, you need an api key, related to your account. From Pachube website, you can generate keys with different privileges. If you only want to read data from a public feed, you need to choose READ as permitted methods:

At the top of PHP source, some constants are defined about Pachube connection: the api key, the feed id, the datastream id and the element position in the received row (normally, a data row from Pachube has the format <datastream_id>,<timestamp>,<value>):

define("API_KEY", "myapikey");
define("FEED_ID", 29689);
define("DATASTREAM_ID", 0);
define("ELEMENT_POSITION", 2);

To keep things simple, choose to receive data in CSV format, using an URL as explaned in the guide:

$feed_url = "http://api.pachube.com/v2/feeds/" . FEED_ID . ".csv?datastreams=" . DATASTREAM_ID;

To connect to Pachube website, we’re going to use cURL library: with its curl_init method, you get an handle related to Pachube URL. Before you can connect, you need to set some propertiesRETURNTRANSFER to get response body from cURL and HTTPHEADER to specify your api key:

$curl_handle = curl_init($feed_url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('X-PachubeApiKey: ' . API_KEY));
$out = curl_exec($curl_handle);

When the connection is done, check its HTTP_CODE: if it’s 200, it means the connection was successful and Pachube returned a correct value. Split the response body using comma (CSV) as a separator and get the value in the position specified:

$http_code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
if($http_code == "200") {
  $elements = explode(",", $out);
  $element = $elements[ELEMENT_POSITION];
}

At last create an iframe to display thermometer page passing the read value as parameter:

echo "<iframe src="thermometer.php?value=$element" frameborder="0" width="320" height="240"></iframe>;

The result (with debug mode on):

Pages: 1 2 3 4

13 Comments »

  1. Lucio 30/06/2012 at 16:51 - Reply

    Gentile Luca Dentella,
    ho trovato interessantissimo il suo tutorial che riguarda il dashboard svg. Le chiederei gentilmente, se possibile, di fornirmi un esempio per visualizzare in forma grafica sul browser i valore di tensione letti sugli ingressi analogici dell’arduino uno in forma vu meter lineare (tipo il suo termometro) oppure classico a lancetta.
    Ringraziando anticipatamente, Le porgo distinti saluti.
    lucio.ra@libero.it

  2. A MacLeod 06/05/2013 at 22:24 - Reply

    Thanks for taking the time to document this, I used it as a basis for my own arduino project (a web-connected heating oil level monitor.)

    Getting a nice SVG-based level indication turned out to be easier than I had feared… strangely, getting it to send warning emails was trickier!

    http://s230.photobucket.com/user/macleodaj/media/Junk/oil_level_gauge.jpg.html

    • luca 07/05/2013 at 08:19 - Reply

      Hi! Thanks for your feedback and great SVG web interface!

Leave A Response »

This website uses cookies to ensure you get the best experience on our website maggiori informazioni

Questo sito utilizza i cookie per fonire la migliore esperienza di navigazione possibile. Continuando a utilizzare questo sito senza modificare le impostazioni dei cookie o clicchi su "Accetta" permetti al loro utilizzo.

Chiudi