Skip to content

Instantly share code, notes, and snippets.

@analogic
Created March 12, 2015 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save analogic/597d6511b20c501b6346 to your computer and use it in GitHub Desktop.
Save analogic/597d6511b20c501b6346 to your computer and use it in GitHub Desktop.
<?php
// some settings first
$rrdtool = "/usr/bin/rrdtool";
$rrd = "temps.rrd";
$png = "temps.png";
$pngUptime = "temps.uptime.png";
$last = "temps.last.txt";
// parse data from request
$temp = isset($_GET['temp']) ? floatval($_GET['temp']) : null;
$uptime = isset($_GET['uptime']) ? intval($_GET['uptime']) : null;
// log data to text file for debug
if(!empty($temp)) {
file_put_contents($last, date('Y-m-d H:i:s').' '."$temp $uptime\n", FILE_APPEND);
echo "OK";
}
// if there is no rrd database yet we create it
$start = time();
if (!is_file($rrd)) {
$cmd = "$rrdtool create $rrd --start ".($start-1)." --step 60 ".
"DS:temperature:GAUGE:120:-50:80 ".
"DS:uptime:GAUGE:120:0:U ".
"RRA:AVERAGE:0.5:1:600 ".
"RRA:AVERAGE:0.5:6:700 ".
"RRA:AVERAGE:0.5:24:775 ".
"RRA:AVERAGE:0.5:288:797 ".
"RRA:MIN:0.5:1:600 ".
"RRA:MIN:0.5:6:700 ".
"RRA:MIN:0.5:24:775 ".
"RRA:MIN:0.5:288:797 ".
"RRA:MAX:0.5:1:600 ".
"RRA:MAX:0.5:6:700 ".
"RRA:MAX:0.5:24:775 ".
"RRA:MAX:0.5:288:797 ".
"RRA:AVERAGE:0.5:1:200";
echo shell_exec($cmd);
}
// fill with data if we have them
if(!empty($temp)) {
$cmd = "$rrdtool update $rrd $start:$temp:$uptime";
echo shell_exec($cmd);
}
// if client is not Arduino but some browser we generate and serve two images
if(!empty($_SERVER['HTTP_USER_AGENT'])) {
$cmd = "$rrdtool graph $png ".
"--title 'Temperature' ".
"--start 'now-1d' ".
"--end now ".
"--imgformat PNG ".
"--width=600 ".
"--height=300 ".
"--lower-limit 0 ".
"DEF:a=$rrd:temperature:AVERAGE ".
"'LINE2:a#00b6e4:Temperature in kitchen' ";
//echo $cmd."<br />";
shell_exec($cmd);
echo "<br /><img src='$png'><br /><br />";
$cmd = "$rrdtool graph $pngUptime ".
"--title 'Uptime' ".
"--start 'now-1d' ".
"--end now ".
"--imgformat PNG ".
"--width=600 ".
"--height=300 ".
"--lower-limit 0 ".
"DEF:a=$rrd:uptime:AVERAGE ".
"'LINE2:a#FE6E3A:Uptime' ";
//echo $cmd."<br />";
shell_exec($cmd);
echo "<br /><br /><img src='$pngUptime'><br /><br />";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment