You are reading a single comment by @petrynchyn and its replies. Click here to read the full conversation.
  • 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.

    1. var C = {
    2. CONVERT_T: 0x44,
    3. COPY: 0x48,
    4. READ: 0xBE,
    5. WRITE: 0x4E
    6. };
    7. function DS18B20(oneWire, device) {
    8. this.bus = oneWire;
    9. if (device === undefined) {
    10. this.sCode = this.bus.search()[0];
    11. } else {
    12. if (parseInt(device).toString() == device && device >= 0 && device <= 126) {
    13. this.sCode = this.bus.search()[device];
    14. } else {
    15. this.sCode = device;
    16. }
    17. }
    18. this.deviceTypeCode=parseInt(this.sCode[0]+this.sCode[1]);
    19. }
    20. /** For internal use - read the scratchpad region */
    21. DS18B20.prototype._readSpad = function(convert_t) {
    22. var spad = [];
    23. this.bus.reset();
    24. this.bus.select(this.sCode);
    25. convert_t=false;
    26. if (convert_t) {
    27. this.bus.write(C.CONVERT_T, true);
    28. this.bus.reset();
    29. this.bus.select(this.sCode);
    30. }
    31. this.bus.write(C.READ);
    32. for (var i = 0; i < 9; i++) {
    33. spad.push(this.bus.read());
    34. }
    35. return spad;
    36. };
    37. /** For internal use - write the scratchpad region */
    38. DS18B20.prototype._writeSpad = function(th, tl, conf) {
    39. this.bus.reset();
    40. this.bus.select(this.sCode);
    41. this.bus.write(C.WRITE);
    42. this.bus.write(th);
    43. this.bus.write(tl);
    44. this.bus.write(conf);
    45. this.bus.reset();
    46. this.bus.select(this.sCode);
    47. this.bus.write(C.COPY);
    48. this.bus.reset();
    49. };
    50. /** Set the resolution in bits. From 8 to 12 bits */
    51. DS18B20.prototype.setRes = function(res) {
    52. var spad = this._readSpad();
    53. res = [0x1F, 0x3F, 0x5F, 0x7F][E.clip(res, 9, 12) - 9];
    54. this._writeSpad(spad[2], spad[3], res);
    55. };
    56. /** Return the resolution in bits. From 8 to 12 bits */
    57. DS18B20.prototype.getRes = function() {
    58. return [0x1F, 0x3F, 0x5F, 0x7F].indexOf(this._readSpad()[4]) + 9;
    59. };
    60. /** Return true if this device is present */
    61. DS18B20.prototype.isPresent = function() {
    62. return this.bus.search().indexOf(this.sCode) !== -1;
    63. };
    64. /** Get a temperature reading, in degrees C */
    65. DS18B20.prototype.getTempOld = function(verify) {
    66. var spad = null;
    67. var temp = null;
    68. if ((verify && !this.isPresent()) || !this.sCode) {
    69. return temp;
    70. }
    71. spad = this._readSpad(true);
    72. temp = spad[0] + (spad[1] << 8);
    73. if (temp > 32767) {
    74. temp -= 65536;
    75. }
    76. temp = temp / 16.0;
    77. return temp;
    78. };
    79. DS18B20.prototype.getTemp = function(callback) {
    80. var me=this;
    81. var temp=null;
    82. var spad=null;
    83. ow.reset();
    84. ow.select(this.sCode);
    85. ow.write(C.CONVERT_T, true);
    86. setTimeout(
    87. function() {
    88. var s = sensor._readSpad(false);
    89. var str="";
    90. s.forEach( function(v) { str+=" 0x"+v.toString(16); } );
    91. var temp = s[0] + (s[1]<<8);
    92. if (temp > 32767) temp -= 65536;
    93. switch (me.deviceTypeCode)
    94. {
    95. case 10:
    96. temp = temp/2.0;
    97. break;
    98. default:
    99. temp = temp/16;
    100. break;
    101. }
    102. callback(temp);
    103. },
    104. 1000);
    105. }
    106. /** Return a list of all DS18B20 sensors with the alarms set */
    107. DS18B20.prototype.searchAlarm = function() {
    108. return this.bus.search(0xEC);
    109. };
    110. /** Set alarm low and high values in degrees C - see DS18B20.prototype.searchAlarm.
    111. If the temperature goes below `lo` or above `hi` the alarm will be set. */
    112. DS18B20.prototype.setAlarm = function(lo, hi) {
    113. lo--; // DS18B20 alarms if (temp<=lo || temp>hi), but we want (temp<lo || temp>hi)
    114. if (lo < 0) lo += 256;
    115. if (hi < 0) hi += 256;
    116. var spad = this._readSpad();
    117. this._writeSpad(hi, lo, spad[4]);
    118. };
    119. /** Initialise a DS18B20 device. Use either as:
    120. connect(new OneWire(pin)) - use the first found DS18B20 device
    121. connect(new OneWire(pin), N) - use the Nth DS18B20 device
    122. connect(new OneWire(pin), ID) - use the DS18B20 device with the given ID
    123. */
    124. exports.connect = function(oneWire, device) {
    125. return new DS18B20(oneWire, device);
    126. };
  • 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 ?

    1. /*
    2. Module for the DS18B20 temperature sensor
    3. var ow = new OneWire(A1);
    4. var sensor = require("DS18B20").connect(ow);
    5. sensor.getTemp(function (temp) { console.log("Temp is "+temp+"°C"); }, true);
    6. sensor.setRes(9);
    7. sensor.getTemp(function (temp) { console.log("Temp is "+temp+"°C"); }, true);
    8. var sensor2 = require("DS18B20").connect(ow, 1);
    9. var sensor3 = require("DS18B20").connect(ow, -8358680895374756824);
    10. */
    11. var C = {
    12. CONVERT_T: 0x44,
    13. COPY: 0x48,
    14. READ: 0xBE,
    15. WRITE: 0x4E
    16. };
    17. function DS18B20(oneWire, device) {
    18. this.bus = oneWire;
    19. if (device === undefined) {
    20. this.sCode = this.bus.search()[0];
    21. } else {
    22. if (parseInt(device).toString() == device && device >= 0 && device <= 126) {
    23. this.sCode = this.bus.search()[device];
    24. } else {
    25. this.sCode = device;
    26. }
    27. }
    28. this.deviceTypeCode=parseInt(this.sCode[0]+this.sCode[1]);
    29. }
    30. /** For internal use - read the scratchpad region */
    31. DS18B20.prototype._readSpad = function() {
    32. var spad = [];
    33. this.bus.reset();
    34. this.bus.select(this.sCode);
    35. this.bus.write(C.READ);
    36. for (var i = 0; i < 9; i++) {
    37. spad.push(this.bus.read());
    38. }
    39. return spad;
    40. };
    41. /** For internal use - write the scratchpad region */
    42. DS18B20.prototype._writeSpad = function(th, tl, conf) {
    43. this.bus.reset();
    44. this.bus.select(this.sCode);
    45. this.bus.write(C.WRITE);
    46. this.bus.write(th);
    47. this.bus.write(tl);
    48. this.bus.write(conf);
    49. this.bus.reset();
    50. this.bus.select(this.sCode);
    51. this.bus.write(C.COPY);
    52. this.bus.reset();
    53. };
    54. /** Set the resolution in bits. From 8 to 12 bits */
    55. DS18B20.prototype.setRes = function(res) {
    56. var spad = this._readSpad();
    57. res = [0x1F, 0x3F, 0x5F, 0x7F][E.clip(res, 9, 12) - 9];
    58. this._writeSpad(spad[2], spad[3], res);
    59. };
    60. /** Return the resolution in bits. From 8 to 12 bits */
    61. DS18B20.prototype.getRes = function() {
    62. return [0x1F, 0x3F, 0x5F, 0x7F].indexOf(this._readSpad()[4]) + 9;
    63. };
    64. /** Return true if this device is present */
    65. DS18B20.prototype.isPresent = function() {
    66. return this.bus.search().indexOf(this.sCode) !== -1;
    67. };
    68. /** Get a temperature reading, in degrees C */
    69. DS18B20.prototype.getTemp = function(callback,verify) {
    70. if ((verify && !this.isPresent()) || !this.sCode) {
    71. return callback(null);
    72. }
    73. var tim = {9:94, 10:188, 11:375, 12:750}; //Thermometer Resolution (bit) : Max Conversion Time (ms)
    74. this.bus.reset();
    75. this.bus.select(this.sCode);
    76. this.bus.write(C.CONVERT_T, true);
    77. setTimeout(
    78. function(me) {
    79. var s = me._readSpad();
    80. var str="";
    81. s.forEach( function(v) { str+=" 0x"+v.toString(16); } );
    82. var temp = s[0] + (s[1]<<8);
    83. if (temp > 32767) temp -= 65536;
    84. switch (me.deviceTypeCode){
    85. case 10: temp = temp/ 2.0; break;
    86. default: temp = temp/16.0; break;
    87. }
    88. callback(temp);
    89. }, tim[this.getRes()], this);
    90. }
    91. /** Return a list of all DS18B20 sensors with the alarms set */
    92. DS18B20.prototype.searchAlarm = function() {
    93. return this.bus.search(0xEC);
    94. };
    95. /** Set alarm low and high values in degrees C - see DS18B20.prototype.searchAlarm.
    96. If the temperature goes below `lo` or above `hi` the alarm will be set. */
    97. DS18B20.prototype.setAlarm = function(lo, hi) {
    98. lo--; // DS18B20 alarms if (temp<=lo || temp>hi), but we want (temp<lo || temp>hi)
    99. if (lo < 0) lo += 256;
    100. if (hi < 0) hi += 256;
    101. var spad = this._readSpad();
    102. this._writeSpad(hi, lo, spad[4]);
    103. };
    104. /** Initialise a DS18B20 device. Use either as:
    105. connect(new OneWire(pin)) - use the first found DS18B20 device
    106. connect(new OneWire(pin), N) - use the Nth DS18B20 device
    107. connect(new OneWire(pin), ID) - use the DS18B20 device with the given ID
    108. */
    109. exports.connect = function(oneWire, device) {
    110. return new DS18B20(oneWire, device);
    111. };
About

Avatar for petrynchyn @petrynchyn started