Last active
August 29, 2015 14:13
-
-
Save mokjpn/50fbd1c2cd40a3a9e21e to your computer and use it in GitHub Desktop.
Read Barometer from GROVE I2C Barometer Module.
This file contains hidden or 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
# GROVEシステムのI2C気圧センサモジュール(BMP085)から温度を読み込んでkonashi経由で表示 | |
- [GROVEシステムのI2C気圧センサモジュール](http://www.seeedstudio.com/wiki/Grove_-_Barometer_Sensor) (BMP085)から温度と気圧を読み込んで表示します. | |
[GROVE拡張ボード](http://yukaishop.base.ec/items/219896) のI2Cバスに温度センサモジュールを接続して実行してください. | |
- ac1-mdの部分はセンサ内部のEEPROMに書かれている較正データで,個体ごとに違うようです.ここが0とか,同じ値の連続になっている場合は読み込みに失敗しているので,KonashiボードとiOSアプリ両方を再起動してやり直したほうがいいです. | |
- Arduinoのサンプルスケッチを参考につくりはじめたけど,JavaScriptではウェイトができないので非同期で動くように書くのに苦労しました... | |
## わかったこと | |
- I2Cバスへのコマンド書き込みを1バイトずつ2度に分けてk.i2cWrite()で書くとうまく動かなくて,2バイト一度に書くとうまくいったりとか,けっこう微妙. | |
- 読み込みはcompleteReadI2cを経由して行うこと,そして呼び出されるハンドラの中では必ず k.off(k.KONASHI_EVENT_I2C_READ_COMPLETE); をすること.そうしないとハンドラが残ってしまって大変なことに. | |
- HTMLのほうに<span id="konashi-debug-console"><span> をいれておくこと,そしてk.log()をうまく入れること. | |
- データシート通りに,書き込みのときはi2cStartCondition->i2cWrite->i2cRestartCondition->i2cReadRequest という順序を守ること.とくに途中のRestartをStartやStopで代用しないこと. | |
- unsignedな値を作りたいときは >>> 演算子を使うこと.Javascriptの数値は基本的に32bit signed. | |
## 動作環境 | |
- [konashi 1.0](http://yukaishop.base.ec/items/219844) + [GROVE拡張ボード](http://yukaishop.base.ec/items/219896) + [GROVEシステムのI2C気圧センサモジュール](http://www.seeedstudio.com/wiki/Grove_-_Barometer_Sensor) で開発しましたが, | |
- [konashi 2.0](http://www.macnicaonline.com/SHOP/YEWPC002.html) + [Uzukiシールド](http://www.macnicaonline.com/SHOP/MPUZK001B.html) + [GROVEシステムのI2C気圧センサモジュール](http://www.seeedstudio.com/wiki/Grove_-_Barometer_Sensor) でもまったく変更なく動きました. | |
- [SBBLE](http://sbble.micutil.com/) + [Adafruit BMP180 Breakout board](http://www.adafruit.com/product/1603) でもそのまま動きました. | |
This file contains hidden or 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
.content { | |
margin: 10px; | |
} | |
#values { | |
display: none; | |
} |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="viewport" content="width=device-width, initial-scale=2, minimum-scale=1, maximum-scale=1, user-scalable=no"> | |
<!-- ratchet css --> | |
<link rel="stylesheet" href="http://jsrun.it/assets/h/F/P/P/hFPPa"> | |
</head> | |
<body> | |
<header class="bar-title"> | |
<h1 class="title">konashi.js: Read Barometer</h1> | |
</header> | |
<div class="content"> | |
<ul id="values" class="list inset"> | |
<li>uncompensated temperature: <span id="ut"></span></li> | |
<li>Temperature: <span id="temperature"></span> ℃</li> | |
<li>uncompensated pressure: <span id="up"></span></li> | |
<li>Barometer: <span id="barometer"></span>hPa</li> | |
<li>ac1: <span id="ac1"></span></li> | |
<li>ac2: <span id="ac2"></span></li> | |
<li>ac3: <span id="ac3"></span></li> | |
<li>ac4: <span id="ac4"></span></li> | |
<li>ac5: <span id="ac5"></span></li> | |
<li>ac6: <span id="ac6"></span></li> | |
<li>b1: <span id="b1"></span></li> | |
<li>b2: <span id="b2"></span></li> | |
<li>mb: <span id="mb"></span></li> | |
<li>mc: <span id="mc"></span></li> | |
<li>md: <span id="md"></span></li> | |
<li>log: <span id="konashi-debug-console"></span></li> | |
</ul> | |
</div> | |
<!-- for konashijs --> | |
<script src="http://konashi.ux-xu.com/kjs/konashi-bridge.min.js"></script> | |
<!-- zepto --> | |
<script src="http://jsrun.it/assets/1/M/0/f/1M0fl"></script> | |
<!-- touch.js --> | |
<script src="http://jsrun.it/assets/g/s/1/M/gs1MI"></script> | |
<!-- ratchet --> | |
<link rel="stylesheet" href="http://jsrun.it/assets/h/F/P/P/hFPPa"> | |
<script src="http://jsrun.it/assets/g/3/W/u/g3WuF"></script> | |
</body> | |
</html> |
This file contains hidden or 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
// forked from mokjpn's "Read Temperature from GROVE analog Temperature Module." http://jsdo.it/mokjpn/vY5W | |
// forked from kotobuki's "Analog IO" http://jsdo.it/kotobuki/mCc7 | |
// forked from kotobuki's "Digital IO" http://jsdo.it/kotobuki/lbRn | |
// forked from monakaz's "LEDをコントロールしちゃおう☆" http://jsdo.it/monakaz/w1gz | |
$(function() { | |
var intervalId; | |
var BMP085_ADDRESS = 0x77; | |
var OSS = 0; | |
var ac1, ac2, ac3, ac4, ac5, ac6, b1, b2, mb, mc, md; | |
var ut, PressureCompensate; | |
var readdelay = 400; | |
// konashiボードの検索を開始 | |
k.find(); | |
// k.showDebugLog(); | |
k.ready(function() { | |
$("#values").show(); | |
k.log("Enable I2C"); | |
k.i2cMode(k.KONASHI_I2C_ENABLE); | |
bmp085_init(); | |
setTimeout(function() { | |
intervalId = window.setInterval(function() { | |
bmp085ReadUT(); | |
setTimeout(function() { | |
bmp085ReadUP(); | |
}, readdelay); | |
}, 1500); | |
}, readdelay * 11); | |
}); | |
function bmp085ReadInt(address, handler) { | |
k.log("bmp085ReadInt entered: " + address); | |
k.i2cStartCondition(); | |
k.i2cWrite(1, address, BMP085_ADDRESS); | |
k.i2cRestartCondition(); | |
k.i2cReadRequest(2, BMP085_ADDRESS); | |
k.log("bmp085ReadInt i2cReadRequest called"); | |
k.completeReadI2c(handler); | |
} | |
function bmp085Read3bytes(address, handler) { | |
k.log("bmp085Read entered: " + address); | |
k.i2cStartCondition(); | |
k.i2cWrite(1, address, BMP085_ADDRESS); | |
k.i2cRestartCondition(); | |
k.i2cReadRequest(3, BMP085_ADDRESS); | |
k.log("bmp085Read i2cReadRequest called"); | |
k.completeReadI2c(handler); | |
} | |
function bmp085ReadUT() { | |
k.log("bmp085ReadUT invoked"); | |
k.i2cStartCondition(); | |
k.i2cWrite(2, [0xF4, 0x2E], BMP085_ADDRESS); | |
k.i2cStopCondition(); | |
setTimeout(function() { | |
bmp085ReadInt(0xF6, function() { | |
k.i2cStopCondition(); | |
k.off(k.KONASHI_EVENT_I2C_READ_COMPLETE); | |
k.i2cRead(2, function(data) { | |
ut = data[0] * 256 + data[1]; | |
k.log("ut got:" + data); | |
$("#ut").html(ut); | |
$("#temperature").html(bmp085GetTemperature(ut)); | |
}); | |
}); | |
}, 5); | |
} | |
function bmp085ReadUP() { | |
k.i2cStartCondition(); | |
k.i2cWrite(2, [0xF4, 0x34], BMP085_ADDRESS); | |
k.i2cStopCondition(); | |
setTimeout(function() { | |
bmp085Read3bytes(0xF6, function() { | |
k.i2cStopCondition(); | |
k.off(k.KONASHI_EVENT_I2C_READ_COMPLETE); | |
k.i2cRead(3, function(data) { | |
msb = data[0]; | |
lsb = data[1]; | |
xlsb = data[2]; | |
k.log("msb,lsb,xlsb got:" + data); | |
}); | |
}); | |
}, 5); | |
setTimeout(function() { | |
up = ((msb << 16) | (lsb << 8) | xlsb) >>> (8 - OSS); | |
k.log("msb:" + msb); | |
k.log("lsb:" + lsb); | |
k.log("xlsb:" + xlsb); | |
k.log("up got:" + up); | |
$("#up").html(up); | |
$("#barometer").html(bmp085GetPressure(up)); | |
}, 10); | |
} | |
function bmp085GetTemperature(ut) { | |
var x1, x2; | |
x1 = (ut - ac6) * ac5 >>> 15; | |
x2 = (mc << 11) / (x1 + md); | |
PressureCompensate = x1 + x2; | |
var temp = ((PressureCompensate + 8) >>> 4); | |
temp = temp / 10; | |
return temp; | |
} | |
function bmp085GetPressure(up) { | |
// example data from the datasheet. | |
// ac1=408; | |
// ac2=-72; | |
// ac3=-14383; | |
// ac4=32741; | |
// ac5=32757; | |
// ac6=23153; | |
// b1=6190; | |
// b2=4; | |
// mb=-32768; | |
// mc=-8711; | |
// md=2868; | |
// | |
// ut = 27898; | |
// up = 23843; | |
// PressureCompensate = 2399; | |
var x1, x2, x3, b3, b6, p; | |
var b4, b7; | |
b6 = PressureCompensate - 4000; | |
x1 = (b2 * (b6 * b6) >> 12) >> 11; | |
x2 = (ac2 * b6) >> 11; | |
x3 = x1 + x2; | |
b3 = ((((ac1) * 4 + x3) << OSS) + 2) >> 2; | |
// Calculate B4 | |
x1 = (ac3 * b6) >> 13; | |
x2 = (b1 * ((b6 * b6) >>> 12)) >> 16; | |
x3 = ((x1 + x2) + 2) >> 2; | |
b4 = (ac4 * ((x3 + 32768) >>> 0)) >> 15; | |
b7 = (((up - b3) * (50000 >> OSS)) >>> 0); | |
if (b7 < 0x80000000) | |
p = (b7 * 2) / b4; | |
else | |
p = (b7 / b4) * 2; | |
x1 = (p >> 8) * (p >> 8); | |
x1 = (x1 * 3038) >> 16; | |
x2 = (-7357 * p) >> 16; | |
p += (x1 + x2 + 3791) >> 4; | |
var temp = p / 100; | |
return temp; | |
} | |
function convertSignedShort(val) { | |
if (val > 32767) | |
return (-(65536 - val)); | |
else | |
return (val); | |
} | |
function bmp085_init() { | |
bmp085ReadInt(0xAA, function() { | |
k.i2cStopCondition(); | |
k.off(k.KONASHI_EVENT_I2C_READ_COMPLETE); | |
k.i2cRead(2, function(data) { | |
value = convertSignedShort(data[0] * 256 + data[1]); | |
k.log("ac1 got:" + value); | |
$("#ac1").html(value); | |
ac1 = value; | |
}); | |
}); | |
setTimeout(function() { | |
bmp085ReadInt(0xAC, function() { | |
k.i2cStopCondition(); | |
k.off(k.KONASHI_EVENT_I2C_READ_COMPLETE); | |
k.i2cRead(2, function(data) { | |
value = convertSignedShort(data[0] * 256 + data[1]); | |
k.log("ac2 got:" + value); | |
$("#ac2").html(value); | |
ac2 = value; | |
}); | |
}); | |
}, readdelay); | |
setTimeout(function() { | |
bmp085ReadInt(0xAE, function() { | |
k.i2cStopCondition(); | |
k.off(k.KONASHI_EVENT_I2C_READ_COMPLETE); | |
k.i2cRead(2, function(data) { | |
value = convertSignedShort(data[0] * 256 + data[1]); | |
k.log("ac3 got:" + value); | |
$("#ac3").html(value); | |
ac3 = value; | |
}); | |
}); | |
}, readdelay * 2); | |
setTimeout(function() { | |
bmp085ReadInt(0xB0, function() { | |
k.i2cStopCondition(); | |
k.off(k.KONASHI_EVENT_I2C_READ_COMPLETE); | |
k.i2cRead(2, function(data) { | |
value = data[0] * 256 + data[1]; | |
k.log("ac4 got:" + value); | |
$("#ac4").html(value); | |
ac4 = value; | |
}); | |
}); | |
}, readdelay * 3); | |
setTimeout(function() { | |
bmp085ReadInt(0xB2, function() { | |
k.i2cStopCondition(); | |
k.off(k.KONASHI_EVENT_I2C_READ_COMPLETE); | |
k.i2cRead(2, function(data) { | |
value = data[0] * 256 + data[1]; | |
k.log("ac5 got:" + value); | |
$("#ac5").html(value); | |
ac5 = value; | |
}); | |
}); | |
}, readdelay * 4); | |
setTimeout(function() { | |
bmp085ReadInt(0xB4, function() { | |
k.i2cStopCondition(); | |
k.off(k.KONASHI_EVENT_I2C_READ_COMPLETE); | |
k.i2cRead(2, function(data) { | |
value = data[0] * 256 + data[1]; | |
k.log("ac6 got:" + value); | |
$("#ac6").html(value); | |
ac6 = value; | |
}); | |
}); | |
}, readdelay * 5); | |
setTimeout(function() { | |
bmp085ReadInt(0xB6, function() { | |
k.i2cStopCondition(); | |
k.off(k.KONASHI_EVENT_I2C_READ_COMPLETE); | |
k.i2cRead(2, function(data) { | |
value = convertSignedShort(data[0] * 256 + data[1]); | |
k.log("b1 got:" + value); | |
$("#b1").html(value); | |
b1 = value; | |
}); | |
}); | |
}, readdelay * 6); | |
setTimeout(function() { | |
bmp085ReadInt(0xB8, function() { | |
k.i2cStopCondition(); | |
k.off(k.KONASHI_EVENT_I2C_READ_COMPLETE); | |
k.i2cRead(2, function(data) { | |
value = convertSignedShort(data[0] * 256 + data[1]); | |
k.log("b2 got:" + value); | |
$("#b2").html(value); | |
b2 = value; | |
}); | |
}); | |
}, readdelay * 7); | |
setTimeout(function() { | |
bmp085ReadInt(0xBA, function() { | |
k.i2cStopCondition(); | |
k.off(k.KONASHI_EVENT_I2C_READ_COMPLETE); | |
k.i2cRead(2, function(data) { | |
value = convertSignedShort(data[0] * 256 + data[1]); | |
k.log("mb got:" + value); | |
$("#mb").html(value); | |
mb = value; | |
}); | |
}); | |
}, readdelay * 8); | |
setTimeout(function() { | |
bmp085ReadInt(0xBC, function() { | |
k.i2cStopCondition(); | |
k.off(k.KONASHI_EVENT_I2C_READ_COMPLETE); | |
k.i2cRead(2, function(data) { | |
value = convertSignedShort(data[0] * 256 + data[1]); | |
k.log("mc got:" + value); | |
$("#mc").html(value); | |
mc = value; | |
}); | |
}); | |
}, readdelay * 9); | |
setTimeout(function() { | |
bmp085ReadInt(0xBE, function() { | |
k.i2cStopCondition(); | |
k.off(k.KONASHI_EVENT_I2C_READ_COMPLETE); | |
k.i2cRead(2, function(data) { | |
value = convertSignedShort(data[0] * 256 + data[1]); | |
k.log("md got:" + value); | |
$("#md").html(value); | |
md = value; | |
}); | |
}); | |
}, readdelay * 10); | |
} | |
}); |
This file contains hidden or 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
( for name ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment