Close

A bit more progress on the web server side of things

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 01/19/2020 at 04:320 Comments

I decided to work on a web front-end for displaying summary information and the last few readings.  I wanted to make a running 10 min or so average of the temperature and humidity readings.  It ended up a little bit involved but I got it functional, the php code still need a few tweaks.  It needs a bit more prettying up with some proper html and some css thrown in. Below is a sample of the output (again not very pretty yet.):

And of course the underlying php code:

<?php


$avg_temp=0;
$avg_humidity=0;

$count = 0;
$csv_array=[];

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;

while($count < 40){
echo print_r($scv_lines_parsed[$count][0], true) . ',';
echo print_r($scv_lines_parsed[$count][1], true) . ',';
echo print_r($scv_lines_parsed[$count][2], true) . ',';
echo print_r($scv_lines_parsed[$count][3], true) .'<br />';
$count++;
}


$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++;
}
echo '10 min avg' . '<br />';
echo 'humid,temp <br />';
$avg_temp=$avg_temp/40;
$avg_humidity=$avg_humidity/40;

echo $avg_humidity . ',' . $avg_temp;
}
?>

On the back-end of things I also modified  rsensor_dht22.php to create a new temp/humidity log file when the current one gets around 2MB as the file can get incredibly long over time.

<?php
if($_POST){


$statefile = fopen("../logfiles/rsensor_dht22.txt", "r");
if (flock($statefile,LOCK_SH)){
	$edstatus = fgets($statefile);
	flock($statefile,LOCK_UN);
}
fclose($statefile);
if($edstatus =="enable"){

//date_default_timezone_set("UTC");
date_default_timezone_set("America/New_York");
$data = $_POST['data'];
$data2 = $_POST['data2'];
$datfile = fopen("../logfiles/rsensor_dht22.csv", "a+");
if (flock($datfile,LOCK_EX)){
	//fwrite($datfile, $data . "," . $data2 . "," . date("H:i:s") ."\n");
	//fwrite($datfile, $data . "," . $data2 . "," . date("H:i:s") . "," . date("Y-m-d") ."\n");
	fwrite($datfile, date("Y-m-d") . "," . date("H:i:s") . "," . $data . "," . $data2 . "\n");
	flock($datfile,LOCK_UN);
	}
fclose($datfile);
}
$statusfile = '../logfiles/rsensor_dht22.csv';
$filesize = filesize($statusfile);
if($filesize >= 2097152){
rename($statusfile,'../logfiles/rsensor_dht22_' . date("Y-m-d-H-i-s") . '.csv');


}


}

?>

Discussions