Close

Adding a webpage for quick viewing

A project log for Remote environmental logging

simple way to log temperature, humidity, etc. with the help of one or more ESP8266 boards

mcunerdmcu_nerd 04/25/2021 at 00:230 Comments

I created (or rather modified one I did for the DHT22 sensor readings) for seeing the latest an recent history of readings.  It's ment to be placed within the parent directory of the php and logfiles directories.  It still needs to be cleaned up a bit but it works:

<?php


$avg_temp=0;
$avg_humidity=0;

$avg_temp2=0;
$avg_humidity2=0;

$count = 0;
$csv_array=[];

$file1 = '/var/www/html/sensorlogging/logfiles/rsensor_ds18b20_d.csv';
$totallines =intval(exec('wc -l '.escapeshellarg($file1) . ' 2>/dev/null'));

$file2 ='/var/www/html/sensorlogging/logfiles/rsensor_dht22_e_1.csv';
$totallines2 = intval(exec('wc -l '.escapeshellarg($file2) . ' 2>/dev/null'));

if ($totallines >= 40){
if (($csv = new SplFileObject('logfiles/rsensor_ds18b20_d.csv', 'r')) !== FALSE){

$csv->seek(PHP_INT_MAX);
$last_line=$csv->key();

$lines = new LimitIterator($csv,$last_line - 40,$last_line);
$csv = null;
$count = 0;
//print_r(iterator_to_array($lines));
$lines_array = iterator_to_array($lines);
$lines_array = array_values($lines_array);

$csv_lines_parsed=[];
while($count < 40){
$scv_lines_parsed[]=str_getcsv($lines_array[$count],',');
$count++;

}

//echo "<pre>";
//var_dump($scv_lines_parsed);
//echo "</pre>";


$count =20;
echo '<!DOCTYPE html>' . "\r\n";
echo '<html>' . "\r\n";
echo '<head>' . "\r\n";
echo '<link href="main.css" rel="stylesheet" type="text/css">' . "\r\n";
echo '</head>' . "\r\n";
echo '<body>' . "\r\n";

echo '<table><caption>DS18b20 remote sensors last 20 readings</caption>' . "\r\n";
echo '<tr><th>date</th><th>time</th><th>Attic temp</th><th>Indoor temp</th></tr>' . "\r\n";

while($count < 40){
echo '<tr>';
echo '<td>' . print_r($scv_lines_parsed[$count][0], true) . '</td>';
echo '<td>' . print_r($scv_lines_parsed[$count][1], true) . '</td>';
echo '<td>' . print_r($scv_lines_parsed[$count][2], true) . '</td>';
echo '<td>' . print_r($scv_lines_parsed[$count][3], true) . '</td></tr>' . "\r\n";
$count++;
}

echo '</table>' . '<br>' ."\r\n";

$count = 0;
while($count < 40){
$avg_temp=$avg_temp+floatval($scv_lines_parsed[$count][3]);
$avg_humidity=$avg_humidity+floatval($scv_lines_parsed[$count][2]);
$count++;
}
$avg_temp=$avg_temp/40;
$avg_humidity=$avg_humidity/40;


echo '<table><caption>10 minute average</caption>' . "\r\n";
echo '<tr><th>Attic temp</th><th>Indoor temp</th></tr>' . "\r\n";
echo '<tr><td>' . $avg_humidity . '</td><td>' . $avg_temp . '</td></tr></table>';


//echo $avg_humidity . ',' . $avg_temp;

echo '</body>' . "\r\n";
echo '</html>' . "\r\n";

}
}

else {
echo "Not enough data yet to generate stats, please try again later.";

}

?>

Discussions