Skip to content

Instantly share code, notes, and snippets.

@TooTallNate
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TooTallNate/63969fc0882310928477 to your computer and use it in GitHub Desktop.
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)
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
}
});
};
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