I did some improvements to the code for generating the last 20 readings and a running 10 minute average on a webpage. Of course work on improving things is never done. I"m probably now going to work on improving reliability of the connection between the ESP8266 D1 board and the DHT22 sensor. Occasionally Micropython throws an error when trying to run the routine to read data off of the sensor. The problem appears to be with the jumpers that connect the two devices. Maybe I need something above the really cheapo jumpers.
Anyways he's a screenshot of the web page and the code for index.php (and the main.css page)
index.php
<?php
$avg_temp=0;
$avg_humidity=0;
$count = 0;
$csv_array=[];
$file2 = '/var/www/html/sensorlogging/logfiles/rsensor_dht22.csv';
$totallines =intval(exec('wc -l '.escapeshellarg($file2) . ' 2>/dev/null'));
if ($totallines >= 40){
if (($csv = new SplFileObject('logfiles/rsensor_dht22.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>DHT22 remote sensor last 20 readings</caption>' . "\r\n";
echo '<tr><th>date</th><th>time</th><th>humidity</th><th>temperature</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>humidity</th><th>temperature</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.";
}
?>
main.css
table {
border-collapse: collapse;
background-color: lightgray;
margin-left: auto;
margin-right: auto;
}
td {
text-align: center;
border: 1px solid black;
}
th {
border:none;
background-color: white;
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.