Contents:

Part1: Environment Setup

Part2: Monitering Temperature with Xtrinsic Sensor

Part3: Capture sensor data to database 

Part4: Remote datalogger   <-- You are here

 

Okay most of the core activity related to this blog series has been done .

 

Lets try some php + mysql stuff to display our data collected earlier.

 

In blog 1 we have setup the apache,mysql,php.

so we got a server, database and webpage development tool ready .

 

We will create file to store currently read temperature:

# touch /var/www/sensor.txt

 

Now something should update this file with current temperature.

 

modifying xtrinsic_mysql.c by adding below code.

 

xtrinsic_mysql.c  -> xtrinsic_current_temp.c

//define a char buffer

char buf1[200];

 

//capture current temp to a file

sprintf(buf1, "echo %d > /var/www/sensor.txt",t_m);

system(buf1);

Complete code available in attachment : xtrinsic_current_temp.c.zip

 

Compiling and execute the above code :

# gcc xtrinsic_current_temp.c -o  xtrinsic_current_temp  -std=c99 $(mysql_config --cflags --libs)

# ./xtrinsic_current_temp


Next we will try to pull data from sensor.txt to our webpage

will use php to display current temperature.


current_temper.php

<HEAD>
<meta http-equiv='refresh' content='1;url='current_temper.php'>
</HEAD>

<?php
if (array_key_exists("sensor",$_GET )) {
    $temper = $_GET["sensor"];
    $file=fopen("sensor.txt","w");
    fwrite($file, $temper);
    fclose($file);
    echo $temper;
} else {
    $temper = file_get_contents("sensor.txt");
    echo $temper;
}
?>










 

In Line2 we have added a 1s refresh to get automatic  temperature updates.

 

we will run in browser.

http://localhost/current_temper.php

 

24cwk08.jpg

 

so the current temperature is getting updated.

 

You need to download the "current_temper.php" and "line_graph" and "pChart.tar" to "/var/www" directory.

all available in the attachments.

 

pChart is a php library used to create graphs,charts and pictures and other cool stuff.

 

Let plot a line graph with temperature and time in Vertical & Horizontal axis.

 

line_graph.php

<?php
//ADD AUTO REFRESH HERE

define("PCHART_PATH", "/var/www/pChart");
set_include_path(get_include_path() . PATH_SEPARATOR . PCHART_PATH);
require_once "class/pDraw.class.php";
require_once "class/pImage.class.php";
require_once "class/pData.class.php";

////////////////////////////////////////
// Connect to MySQL
$db = mysql_connect( 'localhost', 'root', 'root' );
if ( !$db ) {
  die( 'Could not connect: ' . mysql_error() );
}

// Select the data base
$db = mysql_select_db( 'db1', $db );
if ( !$db ) {
  die ( 'Error selecting database \'test\' : ' . mysql_error() );
}

// Fetch the data
$query = "SELECT temperature,time FROM sensordata";
#$query = SELECT * FROM sensordata WHERE time1 >= NOW() - INTERVAL 20 MINUTE;

$result = mysql_query( $query );

// All good?
if ( !$result ) {
  // Nope
  $message  = 'Invalid query: ' . mysql_error() . "\n";
  $message .= 'Whole query: ' . $query;
  die( $message );
}

$myData = new pData();

// Print out rows
while ( $row = mysql_fetch_array( $result ) ) {
$temperature = $row["temperature"];
$time        = $row["time"];

$myData->addPoints($temperature,"temperature");
$myData->addPoints($time,"time");

}

$myData->setAbscissa("time");
#$myData->setXAxisDisplay(AXIS_FORMAT_DATE,"H:i:s");
#$myData->setSerieOnAxis("temperature", 1);
$myData->setAxisName(0,"temperature");
$myData->setAxisUnit(0," C");


// specify colors
$myData->setPalette("temperature",array("R" => 240, "G" => 16, "B" => 16, "Alpha" => 100));

// define image object
$myGraph = new pImage(900,400, $myData);

//create a rectangle box
$box_Settings = array("R"=>225, "G"=>227, "B"=>226, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107);
$myGraph->drawFilledRectangle(100,15,600,60,$box_Settings);


$myGraph->setFontProperties(array(
"FontName" => PCHART_PATH . "/fonts/verdana.ttf",
"FontSize" => 5));
$myGraph->setGraphArea(80,100,800,260);
$myGraph->drawScale();

$myGraph->drawText(350,47," Riotboard Temperature Graph",array("FontSize"=>20,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE));




// output line chart
$myGraph->drawLineChart();
header("Content-Type: image/png");
$myGraph->Render(null);

// Close the connection
mysql_close($db);

?>







 

 

lets try in browser to get some updates:

http://localhost/line_graph.php.

 

you will something as below.

25jjhw0.jpg


So we got a nice graph .

The temperature can be accessed remotely from any browser, for this Riotboard has to be given a public IP and accessed over internet.


Incase you get some thing like this

mq83r.jpg

 

we need a php gd library (graphics library for php)

# sudo apt-get install php5-gd

 

Restart apache to load newly installed modules.

# /etc/init.d/apache2 restart          

or

# apache2ctl restart

 

 

with this we got a simple temperature monitoring system.