
將程式碼放至Device頁框中。(I2C的速度不要選400KHz,可能會讀不到氣壓計的值)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 本程式來源為: boffin @ http://forums.electricimp.com/discussion/2234/htu21d-null-read-out-values/p1 | |
// | |
class HTU21D { | |
_i2c = null; | |
static ALSADDR = 0x40; | |
static READTEMP = "\xe3"; | |
static READHUMID = "\xe5"; | |
constructor(i2c) { | |
_i2c = i2c; | |
_i2c.configure(CLOCK_SPEED_100_KHZ); | |
} | |
function GetTemp() { | |
_i2c.write(ALSADDR<<1, READTEMP); | |
imp.sleep(0.05); | |
local a = _i2c.read(ALSADDR<<1, "", 3); | |
//server.log("A"+a); | |
//return 100; | |
return (((a[0] << 8) | (a[1] & 0xfc)) * 175.72 / 65536.0) - 46.85 | |
} | |
function GetHumid() { | |
_i2c.write(ALSADDR<<1, READHUMID); | |
imp.sleep(0.05); | |
local b = _i2c.read(ALSADDR<<1, "", 3); | |
//local rh = (((b[0] << 8) | (b[1] & 0xf0)) * 120.00 / 65536.0) - 6.00 ; | |
return (((b[0] << 8) | (b[1] & 0xf0)) * 125.00 / 65536.0) - 6.00; | |
} | |
} | |
tempSensor <- HTU21D(hardware.i2c89); // 初始化HTU21D | |
server.log(tempSensor.GetTemp()); | |
server.log(tempSensor.GetHumid()); |
按下

應該可以在 Device logs 頁框中看到溫濕度的訊息。
沒有留言:
張貼留言