BMP085 I2C pressure/temperature sensor graphing demo
Demo
var b = require('bonescript');
var i2c = '/sys/class/i2c-adapter/i2c-1/';
b.writeTextFile(i2c + 'new_device', 'bmp085 0x77');
var graphDataSize = 50;
window.graphData = new Array(graphDataSize);
var canvas = document.getElementById("canvas1");
var processing = new Processing(canvas, sketchProc);
setInterval(readTemp, 100);
function readTemp() {
b.readTextFile(i2c + '1-0077/temp0_input', updateGraph);
}
function updateGraph(x) {
for(var i=0; i<graphDataSize-1; i++) {
window.graphData[i] = window.graphData[i+1];
}
window.graphData[i] = (parseInt(x.data, 10)/200.0)-0.7;
processing.redraw();
}
function sketchProc(p) {
p.size(500, 300);
// variables referenced elsewhere
window.height = p.height;
// variables that might get updated
window.rangeLow = 0;
window.rangeHigh = 1;
window.scaleY = window.height /
(window.rangeHigh - window.rangeLow);
// local variables
var stepX = p.width / (graphDataSize - 1);
var centerY = window.height / 2;
p.noLoop();
p.draw = function() {
// erase background
p.background(224);
// draw axis
p.stroke(25);
p.strokeWeight(1);
p.line(0, centerY, p.width, centerY);
// draw graph
p.stroke(0);
p.strokeWeight(3);
//p.line(0, centerY+1, p.width, centerY+1);
var lastX = 0, nextX = 0, lastY, nextY;
for(var point in window.graphData) {
nextY = ((window.rangeHigh -
window.graphData[point]) * scaleY);
if(point !== 0) {
p.line(lastX, lastY, nextX, nextY);
lastX += stepX;
}
nextX += stepX;
lastY = nextY;
}
};
}
Build and execute instructions
- Disconnect your board from power (including USB)
- Connect BMP085 GND to P9_1
- Connect BMP085 VCC to P9_3
- Connect BMP085 SCL to P9_19
- Connect BMP085 SDA to P9_20
- Reapply power and refresh this page before running the demo code
See also
Topics
Related functions