123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- function readSensor() {
- $res;
- $output;
- exec("./mkr680-reader", $output, $res);
- $arr = array(
- "temperature" => -1,
- "pressure" => -1,
- "humidity" => -1,
- "gas resistance" => -1,
- "iaq (accuracy)" => -1
- );
- // prepare the output by converting to lowercase and splitting
- foreach($output as $k=>$v) {
- $t = strtok(strtolower($v), ':');
- $output[$k] = array($t, strtok(''));
- }
- // perform the search
- foreach($arr as $k=>$v) {
- foreach($output as $j=>$w) {
- if(0 === strpos($w[0], $k)) {
- $arr[$k] = $w[1];
- unset($output[$j]);
- }
- }
- }
- // cleanup by removing excess information
- $iaq = trim($arr["iaq (accuracy)"]);
- foreach($arr as $k=>$v) {
- // get first "word", and split on every character
- $v = str_split(strtok(trim($v), ' '), 1);
- $s = "";
- foreach($v as $t) {
- if('.' === $t) { $s .= '.'; continue; }
- //var_dump($t, 0x32, ord($t), chr($t));
- if((0x29 < ord($t)) && (0x40 > ord($t))) $s .= $t;
- else break;
- }
- $arr[$k] = $s;
- }
- // final formatting, unit conversions, precision, etc
- $arr["temperature"] = $arr["temperature"] . " C";
- $arr["pressure"] = $arr["pressure"] . " hPa";
- $arr["humidity"] = $arr["humidity"] . " %";
- $arr["gas resistance"] = $arr["gas resistance"] . " Ohm";
- $arr["iaq (accuracy)"] = $iaq;
- return $arr;
- }
- $last="";
- // monitor the sensor, running mkr680-reader takes about a quarter second,
- while(true) {
- $now = readSensor();
- if($last === $now) { echo "same" . PHP_EOL; continue; }
- $last = $now;
- var_dump($last);
- // the sensor takes at least a second before new samples are available
- //usleep(1000 /*uS*/ * 1750 /* and ~250ms for mkr680-reader gives 2 seconds */);
- // thinking about it differently, reading every 15 minutes gives about 100 readings per day
- sleep(15 /*minutes*/ * 60 /*seconds*/);
- }
|