-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for the htu21d hygrometer and temperature
- Loading branch information
1 parent
bc507ee
commit feb960d
Showing
6 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
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
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 |
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
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; |
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
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
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