NULL, "pressure" => NULL, "humidity" => NULL, "gas resistance" => NULL, "iaq (accuracy)" => NULL ); // do a quick sanity check on data usually 224 bytes, 200 is probably good enough if(200 > strlen(implode("\n", $output))) { return false; } // 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"] . " Pa"; $arr["humidity"] = $arr["humidity"] . " %"; $arr["gas resistance"] = $arr["gas resistance"] . " Ohm"; $arr["iaq (accuracy)"] = $iaq; // one last sanity check, everything should be set foreach($arr as $v) if(NULL === $v) return false; return $arr; } $last=""; // monitor the sensor, running mkr680-reader takes about a quarter second, while(true) { // read data, retry in ten seconds on failure if(false === ($now = readSensor())) { sleep(10 /*seconds*/); continue; } // only send data if there is a change if($last === $now) continue; $last = $now; // send data to mmcgo $now["sensor"] = 'H'; file_get_contents("http://mmcgo.com/radon/environmental.php", false, stream_context_create(array("http"=>array( "method" => "POST", "header" => "Content-Type: application/x-www-form-urlencoded", "content" => http_build_query($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*/); }