Skip to content

Commit

Permalink
Adding support for the htu21d hygrometer and temperature
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianGenisio authored and rwaldron committed Oct 6, 2015
1 parent bc507ee commit feb960d
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 0 deletions.
16 changes: 16 additions & 0 deletions eg/hygrometer-htu21d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var five = require("../lib/johnny-five.js");
var board = new five.Board();

board.on("ready", function() {
var hygrometer = new five.Hygrometer({
controller: "HTU21D"
});

hygrometer.on("data", function() {
console.log(this.relativeHumidity + " %");
});
});

// @markdown
// - [HTU21D - Humidity Sensor](https://www.adafruit.com/products/1899)
// @markdown
16 changes: 16 additions & 0 deletions eg/temperature-htu21d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var five = require("../lib/johnny-five.js");
var board = new five.Board();

board.on("ready", function() {
var temperature = new five.Temperature({
controller: "HTU21D"
});

temperature.on("data", function() {
console.log(this.celsius + "°C", this.fahrenheit + "°F");
});
});

// @markdown
// - [HTU21D - Humidity Sensor](https://www.adafruit.com/products/1899)
// @markdown
96 changes: 96 additions & 0 deletions lib/hygrometer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
var Board = require("../lib/board.js"),
Emitter = require("events").EventEmitter,
util = require("util");

// References
//
var Controllers = {
HTU21D: {
initialize: {
value: function(opts, dataHandler) {
var Multi = require("../lib/imu");
var driver = Multi.Drivers.get(this.board, "HTU21D", opts);
driver.on("data", function(data) {
dataHandler(data.humidity);
});
}
},
toRelativeHumidity: {
value: function(raw) {
return (125.0*(raw/65536)) - 6;
}
}
}
};

var priv = new Map();

function Hygrometer(opts) {
var controller, freq, last = 0, raw;

if (!(this instanceof Hygrometer)) {
return new Hygrometer(opts);
}

Board.Component.call(
this, opts = Board.Options(opts)
);

freq = opts.freq || 25;

if (opts.controller && typeof opts.controller === "string") {
controller = Controllers[opts.controller.toUpperCase()];
} else {
controller = opts.controller;
}

if (controller == null) {
controller = Controllers["ANALOG"];
}

priv.set(this, {});

Object.defineProperties(this, controller);

if (!this.toRelativeHumidity) {
this.toRelativeHumidity = opts.toRelativeHumidity || function(x) { return x; };
}

var propDescriptors = {
relativeHumidity: {
get: function() {
return this.toRelativeHumidity(raw);
}
}
};
// Convenience aliases
propDescriptors.RH = propDescriptors.relativeHumidity;

Object.defineProperties(this, propDescriptors);

if (typeof this.initialize === "function") {
this.initialize(opts, function(data) {
raw = data;
});
}

setInterval(function() {
if (raw === undefined) {
return;
}

var data = {};
data.RH = data.relativeHumidity = this.relativeHumidity;

this.emit("data", null, data);

if (this.relativeHumidity !== last) {
last = this.relativeHumidity;
this.emit("change", null, data);
}
}.bind(this), freq);
}

util.inherits(Hygrometer, Emitter);

module.exports = Hygrometer;
58 changes: 58 additions & 0 deletions lib/imu.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,64 @@ var priv = new Map();
var activeDrivers = new Map();

var Drivers = {
// Based on the AdaFruit Arduino driver
// https://github.com/adafruit/Adafruit_HTU21DF_Library
// https://www.adafruit.com/products/1899
HTU21D: {
ADDRESSES: {
value: [0x40]
},
REGISTER: {
value: {
TEMPERATURE: 0xE3,
HUMIDITY: 0xE5
}
},
initialize: {
value: function(board, opts) {
var READLENGTH = 3;
var io = board.io;
var address = opts.address || this.ADDRESSES[0];

var computed = {
temperature: {},
humidity: {}
};

var readCycle = function(isTemp) {
var register = isTemp ? this.REGISTER.TEMPERATURE : this.REGISTER.HUMIDITY;

io.i2cReadOnce(address, register, READLENGTH, function(data) {
var msb = data[0];
var lsb = data[1];

var value = (msb << 8) | lsb;

if (isTemp) {
computed.temperature = value;
} else {
computed.humidity = value;
this.emit("data", computed);
}

readCycle(!isTemp);
}.bind(this));
}.bind(this);

io.i2cConfig(opts);

// Kick off "read loop"
//
readCycle(false);
}
},
identifier: {
value: function(opts) {
var address = opts.address || Drivers["HTU21D"].ADDRESSES.value[0];
return "htu-s1d-" + address;
}
}
},
// Based on the example code from
// http://playground.arduino.cc/Main/MPU-6050
// http://www.invensense.com/mems/gyro/mpu6050.html
Expand Down
2 changes: 2 additions & 0 deletions lib/johnny-five.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
Fn: require("./fn"),
Gripper: require("./gripper"),
Gyro: require("./gyro"),
Hygrometer: require("./hygrometer"),
IMU: require("./imu"),
IR: require("./ir"),
Keypad: require("./keypad"),
Expand Down Expand Up @@ -66,6 +67,7 @@ module.exports.Board.Virtual = function(opts) {
};

module.exports.Multi = module.exports.IMU;
module.exports.Humidity = module.exports.Hygrometer;

module.exports.Analog = function(opts) {
return new module.exports.Sensor(opts);
Expand Down
16 changes: 16 additions & 0 deletions lib/temperature.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,22 @@ var Controllers = {
}
}
},
HTU21D: {
initialize: {
value: function(opts, dataHandler) {
var Multi = require("../lib/imu");
var driver = Multi.Drivers.get(this.board, "HTU21D", opts);
driver.on("data", function(data) {
dataHandler(data.temperature);
});
}
},
toCelsius: {
value: function(raw) {
return (175.25*raw/65536)-46.85;
}
}
},
//http://playground.arduino.cc/Main/MPU-6050
MPU6050: {
initialize: {
Expand Down

0 comments on commit feb960d

Please sign in to comment.