ni-c / heimcontrol.js

Home-Automation with node.js and Raspberry PI
MIT License
1.41k stars 297 forks source link

OneWire sensors #68

Open maverik23 opened 10 years ago

maverik23 commented 10 years ago

someone connect a sensor OneWire

Opulence commented 10 years ago

Say please... I can't give you the exact answer because it requires time and you need to know how to handle the sensors output result. ...but, please find below an example implementation for a node.js 1-wire sensor. A ds18b20 temperature sensor to be exact.

function readTemp(callback){ fs.readFile('/sys/bus/w1/devices/28-00000400a88a/w1_slave', function(err, buffer) { if (err){ console.error(err); process.exit(1); }

  // Read data from file (using fast node ASCII encoding).
  var data = buffer.toString('ascii').split(" "); // Split by space

  // Extract temperature from string and divide by 1000 to give celsius
  var temp  = parseFloat(data[data.length-1].split("=")[1])/1000.0;

  // Round to one decimal place
  temp = Math.round(temp * 10) / 10;

  // Add date/time to temperature
var data = {
        temperature_record:[{
        unix_time: Date.now(),
        celsius: temp
        }]};

  // Execute call back with data
  callback(data);

}); };

Hope this helps you on your way. All you need to do is modify the code above and write the socket.io adapters and views. If you're lucky you might find your 1-wire sensors already implemented through the npm. ...also you must run 'sudo mod-probe w1-gpio' and 'sudo mod-probe w1-therm' to enable 1-wire drivers. Sorry I couldn't be of more help.

NicoJuicy commented 9 years ago

Respect for @Opulence ! Because how maverik23 asks his question is just plain rude, i wouldn't have answered him :) !