forked from stibi/node-red-contrib-ds18b20
-
Notifications
You must be signed in to change notification settings - Fork 0
/
98-ds18b20.js
41 lines (34 loc) · 1.26 KB
/
98-ds18b20.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module.exports = function(RED) {
var sense = require('ds18b20');
function DS18B20Node(config) {
RED.nodes.createNode(this, config);
this.sensorid = config.sensorid;
// user input is in minutes, this is conversion to miliseconds
this.timer = config.timer * 1000 * 60;
var node = this;
var readSensor = function() {
//node.log("reading a sensor with id=" + node.sensorid);
// TODO error handling
sense.temperature(node.sensorid, function(err, value) {
var msg = { payload: value };
msg.sensorid = node.sensorid;
msg.topic = node.name;
node.send(msg);
});
}
node.tout = setInterval(readSensor, node.timer);
this.on("close", function() {
clearInterval(this.tout);
});
}
RED.nodes.registerType("ds18b20", DS18B20Node);
RED.httpAdmin.get('/sensors/1wire',function(req,res) {
// TODO how to handle this credential thing?
//var credentials = RED.nodes.getCredentials(req.params.id);
//if (credentials) {
sense.sensors(function(err, ids) {
// TODO error handling
res.send(JSON.stringify(ids));
});
});
}