單片機端程序:
https://zh.arslanbar.net/post.php?t=22504PHP程序:
<?php
define("DSNEG", 4);
define("DSERR", 6);
function BIT($n) {
return 1 << $n;
}
function decodeTemperature($flags, $TN, $TD) {
if ($flags & BIT(DSERR)) {
$value = "Error";
} else {
$value = $TN * 1.00 + $TD * 0.01;
if ($flags & BIT(DSNEG)) {
$value = -$value;
}
}
return $value;
}
function getTemperature($fp) {
fwrite($fp, "\x83");
$flag = fread($fp, 1);
$data = fread($fp, 3);
$temperature = decodeTemperature(ord($data{0}), ord($data{1}), ord($data{2}));
return $temperature;
}
?>
<!doctype html>
<html>
<head>
<title>Temperatures</title>
</head>
<body>
<?php
$fp = fopen("/dev/ttyUSB0", "a+");
for ($i = 1; $i <= 30; $i++) {
$temperature = getTemperature($fp);
echo "<b>Temperature $i:</b> $temperature°C<br>\n";
usleep(1E5);
}
fclose($fp);
?>
</body>
</html>