You are reading a single comment by @petrynchyn and its replies.
Click here to read the full conversation.
-
Here's my modified @hygy code. I set Timeout depending on Thermometer Resolution and Max Conversion Time. Tested on DS18B20 and ESP-12q. Can someone check and update code DS18B20.js ?
/*
Module for the DS18B20 temperature sensor
var ow = new OneWire(A1);
var sensor = require("DS18B20").connect(ow);
sensor.getTemp(function (temp) { console.log("Temp is "+temp+"°C"); }, true);
sensor.setRes(9);
sensor.getTemp(function (temp) { console.log("Temp is "+temp+"°C"); }, true);
var sensor2 = require("DS18B20").connect(ow, 1);
var sensor3 = require("DS18B20").connect(ow, -8358680895374756824);
*/
var C = {
CONVERT_T: 0x44,
COPY: 0x48,
READ: 0xBE,
WRITE: 0x4E
};
function DS18B20(oneWire, device) {
this.bus = oneWire;
if (device === undefined) {
this.sCode = this.bus.search()[0];
} else {
if (parseInt(device).toString() == device && device >= 0 && device <= 126) {
this.sCode = this.bus.search()[device];
} else {
this.sCode = device;
}
}
this.deviceTypeCode=parseInt(this.sCode[0]+this.sCode[1]);
}
/** For internal use - read the scratchpad region */
DS18B20.prototype._readSpad = function() {
var spad = [];
this.bus.reset();
this.bus.select(this.sCode);
this.bus.write(C.READ);
for (var i = 0; i < 9; i++) {
spad.push(this.bus.read());
}
return spad;
};
/** For internal use - write the scratchpad region */
DS18B20.prototype._writeSpad = function(th, tl, conf) {
this.bus.reset();
this.bus.select(this.sCode);
this.bus.write(C.WRITE);
this.bus.write(th);
this.bus.write(tl);
this.bus.write(conf);
this.bus.reset();
this.bus.select(this.sCode);
this.bus.write(C.COPY);
this.bus.reset();
};
/** Set the resolution in bits. From 8 to 12 bits */
DS18B20.prototype.setRes = function(res) {
var spad = this._readSpad();
res = [0x1F, 0x3F, 0x5F, 0x7F][E.clip(res, 9, 12) - 9];
this._writeSpad(spad[2], spad[3], res);
};
/** Return the resolution in bits. From 8 to 12 bits */
DS18B20.prototype.getRes = function() {
return [0x1F, 0x3F, 0x5F, 0x7F].indexOf(this._readSpad()[4]) + 9;
};
/** Return true if this device is present */
DS18B20.prototype.isPresent = function() {
return this.bus.search().indexOf(this.sCode) !== -1;
};
/** Get a temperature reading, in degrees C */
DS18B20.prototype.getTemp = function(callback,verify) {
if ((verify && !this.isPresent()) || !this.sCode) {
return callback(null);
}
var tim = {9:94, 10:188, 11:375, 12:750}; //Thermometer Resolution (bit) : Max Conversion Time (ms)
this.bus.reset();
this.bus.select(this.sCode);
this.bus.write(C.CONVERT_T, true);
setTimeout(
function(me) {
var s = me._readSpad();
var str="";
s.forEach( function(v) { str+=" 0x"+v.toString(16); } );
var temp = s[0] + (s[1]<<8);
if (temp > 32767) temp -= 65536;
switch (me.deviceTypeCode){
case 10: temp = temp/ 2.0; break;
default: temp = temp/16.0; break;
}
callback(temp);
}, tim[this.getRes()], this);
}
/** Return a list of all DS18B20 sensors with the alarms set */
DS18B20.prototype.searchAlarm = function() {
return this.bus.search(0xEC);
};
/** Set alarm low and high values in degrees C - see DS18B20.prototype.searchAlarm.
If the temperature goes below `lo` or above `hi` the alarm will be set. */
DS18B20.prototype.setAlarm = function(lo, hi) {
lo--; // DS18B20 alarms if (temp<=lo || temp>hi), but we want (temp<lo || temp>hi)
if (lo < 0) lo += 256;
if (hi < 0) hi += 256;
var spad = this._readSpad();
this._writeSpad(hi, lo, spad[4]);
};
/** Initialise a DS18B20 device. Use either as:
connect(new OneWire(pin)) - use the first found DS18B20 device
connect(new OneWire(pin), N) - use the Nth DS18B20 device
connect(new OneWire(pin), ID) - use the DS18B20 device with the given ID
*/
exports.connect = function(oneWire, device) {
return new DS18B20(oneWire, device);
};
At the weekend I changed a little bit the ds18b20 code: Now it works for me with ds18s20 too, and it waits for 1 sec to a conversation and then calls a given callback.
It's based on @tve modification.