Last active
August 29, 2015 14:26
-
-
Save TooTallNate/63969fc0882310928477 to your computer and use it in GitHub Desktop.
Module for finding and reading OneWire temperature sensors, specifically beginning with ID "28" (like the DS18B20)
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
var fs = require('fs'); | |
var path = require('path'); | |
var devicesRoot = '/sys/bus/w1/devices/'; | |
var slavesFilename = path.resolve(devicesRoot, 'w1_bus_master1/w1_master_slaves'); | |
exports.find = function find (fn) { | |
fs.readFile(slavesFilename, 'ascii', function (err, data) { | |
if (err) return fn(err); | |
var slaves = data.trim().split('\n'); | |
var therms = slaves.filter(function (id) { | |
return /^28-/.test(id); | |
}); | |
fn(null, therms); | |
}); | |
}; | |
exports.read = function read (id, fn) { | |
var filename = path.resolve(devicesRoot, id, 'w1_slave'); | |
fs.readFile(filename, 'ascii', function (err, data) { | |
if (err) return fn(err); | |
if (/\bYES\b/.test(data)) { | |
var value = parseInt(/t=(\d+)/.exec(data)[1], 10); | |
var c = value / 1000; | |
fn(null, c); | |
} else { | |
// TODO: handle bad reading | |
} | |
}); | |
}; |
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
var temp = require('./temp'); | |
temp.find(function (err, therms) { | |
therms.forEach(function (id) { | |
temp.read(id, function (err, c) { | |
var f = (c * (9/5)) + 32; | |
console.log(id, c, f); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment