作者共發了35篇帖子。 內容轉換:不轉換▼
 
點擊 回復
1849 34
【單片機實際應用】開機時在MySQL數據庫中自動記錄開機時間和室內溫度(由DS18B20通過串口提供)
一派護法 十九級
31樓 發表于:2015-6-30 10:04
PHP連續測量300組溫度:
<?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;
}

$filename = "/dev/ttyUSB0";
$fp = fopen($filename, "a+");
for ($i = 1; $i <= 300; $i++) {
    fwrite($fp, "\x83");
    $flag = fread($fp, 1);
    $data = fread($fp, 3);
    $temperature = decodeTemperature(ord($data{0}), ord($data{1}), ord($data{2}));
    echo "<b>Temperature $i:</b> $temperature&deg;C<br>\n";
}
fclose($fp);
?>
一派護法 十九級
32樓 發表于:2015-6-30 10:04
回復:31樓
輸出內容:
Temperature 1: 27.37°C
Temperature 2: 27.37°C
Temperature 3: 27.37°C
Temperature 4: 27.37°C
Temperature 5: 27.37°C
Temperature 6: 27.37°C
Temperature 7: 27.37°C
Temperature 8: 27.37°C
Temperature 9: 27.37°C
Temperature 10: 27.37°C
Temperature 11: 27.37°C
Temperature 12: 27.37°C
Temperature 13: 27.37°C
Temperature 14: 27.37°C
Temperature 15: 27.37°C
Temperature 16: 27.37°C
Temperature 17: 27.37°C
Temperature 18: 27.37°C
Temperature 19: 27.37°C
Temperature 20: 27.37°C
Temperature 21: 27.37°C
Temperature 22: 27.37°C
Temperature 23: 27.37°C
Temperature 24: 27.37°C
Temperature 25: 27.37°C
Temperature 26: 27.37°C
Temperature 27: 27.37°C
Temperature 28: 27.37°C
Temperature 29: 27.37°C
Temperature 30: 27.37°C
Temperature 31: 27.37°C
Temperature 32: 27.37°C
Temperature 33: 27.37°C
Temperature 34: 27.37°C
Temperature 35: 27.37°C
Temperature 36: 27.37°C
Temperature 37: 27.37°C
Temperature 38: 27.37°C
Temperature 39: 27.37°C
Temperature 40: 27.37°C
Temperature 41: 27.37°C
Temperature 42: 27.37°C
Temperature 43: 27.37°C
Temperature 44: 27.37°C
Temperature 45: 27.37°C
Temperature 46: 27.37°C
Temperature 47: 27.37°C
Temperature 48: 27.37°C
Temperature 49: 27.37°C
Temperature 50: 27.37°C
Temperature 51: 27.37°C
Temperature 52: 27.37°C
Temperature 53: 27.37°C
Temperature 54: 27.37°C
Temperature 55: 27.37°C
Temperature 56: 27.37°C
Temperature 57: 27.37°C
Temperature 58: 27.37°C
Temperature 59: 27.37°C
Temperature 60: 27.37°C
Temperature 61: 27.37°C
Temperature 62: 27.37°C
Temperature 63: 27.37°C
Temperature 64: 27.37°C
Temperature 65: 27.37°C
Temperature 66: 27.37°C
Temperature 67: 27.37°C
Temperature 68: 27.37°C
Temperature 69: 27.37°C
Temperature 70: 27.37°C
Temperature 71: 27.37°C
Temperature 72: 27.37°C
Temperature 73: 27.37°C
Temperature 74: 27.37°C
Temperature 75: 27.37°C
Temperature 76: 27.37°C
Temperature 77: 27.37°C
Temperature 78: 27.37°C
Temperature 79: 27.37°C
Temperature 80: 27.37°C
Temperature 81: 27.37°C
Temperature 82: 27.37°C
Temperature 83: 27.37°C
Temperature 84: 27.37°C
Temperature 85: 27.37°C
Temperature 86: 27.37°C
Temperature 87: 27.37°C
Temperature 88: 27.37°C
Temperature 89: 27.37°C
Temperature 90: 27.37°C
Temperature 91: 27.37°C
Temperature 92: 27.37°C
Temperature 93: 27.37°C
Temperature 94: 27.37°C
Temperature 95: 27.37°C
Temperature 96: 27.37°C
Temperature 97: 27.37°C
Temperature 98: 27.37°C
Temperature 99: 27.37°C
Temperature 100: 27.37°C
Temperature 101: 27.43°C
Temperature 102: 27.43°C
Temperature 103: 27.43°C
Temperature 104: 27.43°C
Temperature 105: 27.43°C
Temperature 106: 27.43°C
Temperature 107: 27.43°C
Temperature 108: 27.43°C
Temperature 109: 27.43°C
Temperature 110: 27.43°C
Temperature 111: 27.43°C
Temperature 112: 27.43°C
Temperature 113: 27.43°C
Temperature 114: 27.43°C
Temperature 115: 27.43°C
Temperature 116: 27.43°C
Temperature 117: 27.43°C
Temperature 118: 27.43°C
Temperature 119: 27.43°C
Temperature 120: 27.43°C
Temperature 121: 27.43°C
Temperature 122: 27.43°C
Temperature 123: 27.43°C
Temperature 124: 27.43°C
Temperature 125: 27.43°C
Temperature 126: 27.43°C
Temperature 127: 27.43°C
Temperature 128: 27.43°C
Temperature 129: 27.43°C
Temperature 130: 27.43°C
Temperature 131: 27.43°C
Temperature 132: 27.43°C
Temperature 133: 27.43°C
Temperature 134: 27.43°C
Temperature 135: 27.43°C
Temperature 136: 27.43°C
Temperature 137: 27.43°C
Temperature 138: 27.43°C
Temperature 139: 27.43°C
Temperature 140: 27.43°C
Temperature 141: 27.43°C
Temperature 142: 27.43°C
Temperature 143: 27.43°C
Temperature 144: 27.43°C
Temperature 145: 27.43°C
Temperature 146: 27.43°C
Temperature 147: 27.43°C
Temperature 148: 27.43°C
Temperature 149: 27.43°C
Temperature 150: 27.43°C
Temperature 151: 27.5°C
Temperature 152: 27.5°C
Temperature 153: 27.5°C
Temperature 154: 27.5°C
Temperature 155: 27.5°C
Temperature 156: 27.5°C
Temperature 157: 27.5°C
Temperature 158: 27.5°C
Temperature 159: 27.5°C
Temperature 160: 27.5°C
Temperature 161: 27.5°C
Temperature 162: 27.5°C
Temperature 163: 27.5°C
Temperature 164: 27.5°C
Temperature 165: 27.5°C
Temperature 166: 27.5°C
Temperature 167: 27.5°C
Temperature 168: 27.5°C
Temperature 169: 27.5°C
Temperature 170: 27.5°C
Temperature 171: 27.5°C
Temperature 172: 27.5°C
Temperature 173: 27.5°C
Temperature 174: 27.5°C
Temperature 175: 27.5°C
Temperature 176: 27.5°C
Temperature 177: 27.5°C
Temperature 178: 27.5°C
Temperature 179: 27.5°C
Temperature 180: 27.5°C
Temperature 181: 27.5°C
Temperature 182: 27.5°C
Temperature 183: 27.5°C
Temperature 184: 27.5°C
Temperature 185: 27.5°C
Temperature 186: 27.5°C
Temperature 187: 27.5°C
Temperature 188: 27.5°C
Temperature 189: 27.5°C
Temperature 190: 27.5°C
Temperature 191: 27.5°C
Temperature 192: 27.5°C
Temperature 193: 27.5°C
Temperature 194: 27.5°C
Temperature 195: 27.5°C
Temperature 196: 27.5°C
Temperature 197: 27.5°C
Temperature 198: 27.5°C
Temperature 199: 27.5°C
Temperature 200: 27.5°C
Temperature 201: 27.5°C
Temperature 202: 27.5°C
Temperature 203: 27.5°C
Temperature 204: 27.5°C
Temperature 205: 27.5°C
Temperature 206: 27.5°C
Temperature 207: 27.5°C
Temperature 208: 27.5°C
Temperature 209: 27.5°C
Temperature 210: 27.5°C
Temperature 211: 27.5°C
Temperature 212: 27.5°C
Temperature 213: 27.5°C
Temperature 214: 27.5°C
Temperature 215: 27.5°C
Temperature 216: 27.5°C
Temperature 217: 27.5°C
Temperature 218: 27.5°C
Temperature 219: 27.5°C
Temperature 220: 27.5°C
Temperature 221: 27.5°C
Temperature 222: 27.5°C
Temperature 223: 27.5°C
Temperature 224: 27.5°C
Temperature 225: 27.5°C
Temperature 226: 27.5°C
Temperature 227: 27.5°C
Temperature 228: 27.5°C
Temperature 229: 27.5°C
Temperature 230: 27.5°C
Temperature 231: 27.5°C
Temperature 232: 27.5°C
Temperature 233: 27.5°C
Temperature 234: 27.5°C
Temperature 235: 27.5°C
Temperature 236: 27.5°C
Temperature 237: 27.5°C
Temperature 238: 27.5°C
Temperature 239: 27.5°C
Temperature 240: 27.5°C
Temperature 241: 27.5°C
Temperature 242: 27.5°C
Temperature 243: 27.5°C
Temperature 244: 27.5°C
Temperature 245: 27.5°C
Temperature 246: 27.5°C
Temperature 247: 27.5°C
Temperature 248: 27.5°C
Temperature 249: 27.5°C
Temperature 250: 27.5°C
Temperature 251: 27.56°C
Temperature 252: 27.56°C
Temperature 253: 27.56°C
Temperature 254: 27.56°C
Temperature 255: 27.56°C
Temperature 256: 27.56°C
Temperature 257: 27.56°C
Temperature 258: 27.56°C
Temperature 259: 27.56°C
Temperature 260: 27.56°C
Temperature 261: 27.56°C
Temperature 262: 27.56°C
Temperature 263: 27.56°C
Temperature 264: 27.56°C
Temperature 265: 27.56°C
Temperature 266: 27.56°C
Temperature 267: 27.56°C
Temperature 268: 27.56°C
Temperature 269: 27.56°C
Temperature 270: 27.56°C
Temperature 271: 27.56°C
Temperature 272: 27.56°C
Temperature 273: 27.56°C
Temperature 274: 27.56°C
Temperature 275: 27.56°C
Temperature 276: 27.56°C
Temperature 277: 27.56°C
Temperature 278: 27.56°C
Temperature 279: 27.56°C
Temperature 280: 27.56°C
Temperature 281: 27.56°C
Temperature 282: 27.56°C
Temperature 283: 27.56°C
Temperature 284: 27.56°C
Temperature 285: 27.56°C
Temperature 286: 27.56°C
Temperature 287: 27.56°C
Temperature 288: 27.56°C
Temperature 289: 27.56°C
Temperature 290: 27.56°C
Temperature 291: 27.56°C
Temperature 292: 27.56°C
Temperature 293: 27.56°C
Temperature 294: 27.56°C
Temperature 295: 27.56°C
Temperature 296: 27.56°C
Temperature 297: 27.56°C
Temperature 298: 27.56°C
Temperature 299: 27.56°C
Temperature 300: 27.56°C


一派護法 十九級
33樓 發表于:2015-6-30 10:06
PHP每隔1秒測定一次溫度值,並測量10組溫度:
<?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;
}

$filename = "/dev/ttyUSB0";
$fp = fopen($filename, "a+");
for ($i = 1; $i <= 10; $i++) {
    fwrite($fp, "\x83");
    $flag = fread($fp, 1);
    $data = fread($fp, 3);
    $temperature = decodeTemperature(ord($data{0}), ord($data{1}), ord($data{2}));
    echo "<b>Temperature $i:</b> $temperature&deg;C<br>\n";
    sleep(1);
}
fclose($fp);
?>

輸出:
Temperature 1: 27.56°C
Temperature 2: 27.31°C
Temperature 3: 27.31°C
Temperature 4: 27.37°C
Temperature 5: 27.37°C
Temperature 6: 27.37°C
Temperature 7: 27.37°C
Temperature 8: 27.43°C
Temperature 9: 27.37°C
Temperature 10: 27.43°C
一派護法 十九級
34樓 發表于:2015-7-1 08:30
<?php
$fp = fopen("/dev/ttyUSB0", "a+");
$temperature = getTemperature($fp);
echo "<b>Temperature:</b> $temperature&deg;C<br>\n";
$sql = "INSERT INTO test.Temperatures (TempValue) VALUES ($temperature)";
$rs = mysql_query($sql);
fclose($fp);
?>
存入資料庫
一派護法 十九級
35樓 發表于:2015-11-17 19:39
外界的温度信息先被DS18B20温度传感器采集到,然后经由AVR单片机读取后,通过串口发送给电脑,PHP再通过串口读取温度数据,最后显示在网页上,同时存入MySQL数据库。一个完整的物联网环节!

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:1849 回複數:34
評論數: ?
作者:巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2015-11-17 19:39
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。