You are reading a single comment by @Wilberforce and its replies. Click here to read the full conversation.
  • The callback needs some sort of identifier, as you can't assume the order of the readings in the callbacks. I have two sensors connected:

    var pin=D5; // update for your hardware!
    var ow = new OneWire(pin);
    
    ds = require("https://raw.githubusercontent.c­om/espruino/EspruinoDocs/master/devices/­DS18B20.js");
    
    function cb(t) {  
      print({cb_temp:t});
    }
    var s=ow.search();
    
    console.log( { search:s } );
    
    var sensors = s.map(function (device) {
      return ds.connect(ow, device);
    });
    
    setInterval(function() {
       sensors.forEach(function (sensor, index) {
          console.log(index + ": " + sensor.getTemp(cb));
      });
    },2000);
    
    {
      "search": [
        "28cc2e230500006b",
        "283260dc04000001"
       ]
     }
    =undefined
    0: undefined
    1: undefined
    { "cb_temp": 25.8125 }
    { "cb_temp": 24.9375 }
    0: undefined
    1: undefined
    { "cb_temp": 25.8125 }
    { "cb_temp": 24.9375 }
    0: undefined
    1: undefined
    

    There are a couple of options:

    if (callback) callback(temp);

    if (callback) callback(this);
    function cb(t) {
    print({cb_temp:t.getTemp()});
    }

    if (callback) callback(temp,this.sCode);

    or probably most flexible:

    if (callback) callback(temp,this);
    And then you have the object if you need it...

About

Avatar for Wilberforce @Wilberforce started