Temperatur und Luftfeuchtigkeit messen mit dem Arduino Teil III – Auswertung
Nachdem jetzt die Daten erfolgreich in der Datenbank gespeichert werden können, wird es Zeit, die Daten zu visualisieren.
Dazu nutze ich pChart, eine für nicht-kommerzielle Zwecke freie PHP Bibliothek. Die Verbindung zur Datenbank war schnell erstellt, das folgende Beispiel ist nur eine Anpassung des Codes von http://wiki.pchart.net/doc.mysql.integration.html
Hinweis: Der Code ist noch nicht aufgeräumt, dies folgt in den nächsten Tagen.
<?php /* pChart library inclusions */ include("../pChart/class/pData.class.php"); include("../pChart/class/pDraw.class.php"); include("../pChart/class/pImage.class.php"); /* Create and populate the pData object */ $MyData = new pData(); /* Connect to the MySQL database */ $db = mysql_connect("<server>", "<user>", "<password>"); mysql_select_db("uwezie_db7",$db); /* Build the query that will returns the data to graph */ $Requete = "SELECT temperature, humidity, timestamp FROM ( SELECT @row := @row +1 AS rownum, temperature, humidity, timestamp FROM ( SELECT @row :=0) r, data ) ranked WHERE rownum % 5 = 1 and timestamp >= now() - INTERVAL 24 hour"; $Result = mysql_query($Requete,$db); $timestamp=""; $temperature=""; $humidity=""; if($Result == FALSE) { die(mysql_error()); // TODO: better error handling } while($row = mysql_fetch_array($Result)) { /* Get the data from the query result */ $timestamp = $row["timestamp"]; $temperature = $row["temperature"]; $humidity = $row["humidity"]; /* Save the data in the pData array */ //$myData->addPoints($timestamp,"Timestamp"); $MyData->addPoints($temperature,"Temperature"); //$myData->addPoints($humidity,"Humidity"); } /* Create the pChart object */ $myPicture = new pImage(1600,800,$MyData); /* Turn of Antialiasing */ $myPicture->Antialias = FALSE; /* Add a border to the picture */ // $myPicture->drawRectangle(0,0,1599,799,array("R"=>0,"G"=>0,"B"=>0)); /* Write the chart title */ $myPicture->setFontProperties(array("FontName"=>"../pChart/fonts/Forgotte.ttf","FontSize"=>11)); $myPicture->drawText(150,35,"Temperature",array("FontSize"=>30,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); /* Set the default font */ $myPicture->setFontProperties(array("FontName"=>"../pChart/fonts/pf_arma_five.ttf","FontSize"=>8)); /* Define the chart area */ $myPicture->setGraphArea(60,40,1500,700); /* Draw the scale */ $labelskip = 30; // 1000/24; // values = 1000, hours = 24 $AxisBoundaries = array(0=>array("Min"=>15,"Max"=>25)); $scaleSettings = array("GridR"=>200,"GridG"=>200,"GridB"=>200,"DrawSubTicks"=>FALSE,"CycleBackground"=>FALSE,"Mode"=>SCALE_MODE_MANUAL, "ManualScale"=>$AxisBoundaries,"LabelSkip"=>$labelskip); $serieSettings = array("R"=>229,"G"=>11,"B"=>11,"Alpha"=>100); $MyData->setPalette("Temperature",$serieSettings); $myPicture->drawScale($scaleSettings); /* Turn on Antialiasing */ $myPicture->Antialias = TRUE; /* Draw the line chart */ $myPicture->drawLineChart(); /* Write the chart legend */ $myPicture->drawLegend(540,20,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL)); /* Render the picture (choose the best way) */ $myPicture->autoOutput("drawobjects2.png"); ?> <img src="drawobjects2.png"/> |