• 1. For a first step, we develop the module code and usage in single file TemperatureDevTest.js:

    The code below is working and covers basically the above requirements for the module:

    // TemperatureDevTest.js
    
    // 'class' (Prototype definition)
    var Temperature = function // module (class), for multiple instances
      ( name      // name hinting room / location of sensor\
      , oneWire   // one-wire; for example: new OneWire(B8);
      , addr      // address on the one-wire (required for multiples)
      , interval  // interval in milliseconds of measurements
      , preferred // optional, unit - "C" or "F" (default and not F is C)
      ) {
      // set givens
      this.name       = name;
      this.oneWire    = oneWire;
      this.addr       = addr;
      this.interval   = interval;
      this.preferred  = (preferred === "F") ? "F" : "C";
      this.enabled    = (typeof enabled === "undefined") || enabled;
      this.t = (this.preferred==="C") ? 0 : 32; // set current to 'frozen'
      this.intervalId = null; // later also used as indicator for enabled
      this.sensor     = null; // laster also used as indicatore for connected
      // get going
      this.sensor = require("DS18B20").connect(this.oneWire)­;
      this.intervalId = setInterval(function(_this) {
        _this.sensor.getTemp(function(t) {
            _this.t = t;
          });
      }, this.interval, this);
    };
    Temperature.prototype.getTemp = function
    ( unit // optional, unit "F" or "C", default "C"
    ) {
      var u 
          = (typeof unit === "undefined") ? this.preferred
          : (unit === "F"               ) ? "F"
          :                                 "C"
          ;
      var d = (u === "F")
          ? (this.t * 1.8 + 32) + " Fahrenheit"
          : this.t + " Celsius";
      return d;
    };
    
    
    // usage
    
    // setup oneWire
    var oneWire = new OneWire(B8);
    
    // setup 1st temperature sensor ts1
    var ts1 = new Temperature
      ( "office" // name / location of temperatre sensor 1
      , oneWire
      , null     // addr on one-wire currently not implemented / used
      , 5000     // every 5 secs make a read (to keep it not booring)
      , "F"      // preferred unit is Fahrenheit
      );
    
    
    // for sample's sake, do everhthing deferred for more 
    // than 5 seconds in order to have value(s) to display
    setTimeout(function(){
    
    // get temp in preferred and ad-hoc specified units 
    console.log(ts1.getTemp());
    console.log(ts1.getTemp("C"));
    
    },6000);
    

    The output - in the console - is as expected (code unrelated: unusually hot... a heat wave... usually it is around 50 F or even less this early in the morning...)

     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v94 Copyright 2016 G.Williams
    >
    =undefined
    83.1875 Fahrenheit
    28.4375 Celsius
    >
    
About

Avatar for allObjects @allObjects started