I'm expecting a different output....

Posted on
  • I'm trying to receive a value from my temp sensor. Expect I'm receiving "77 r" (without quotes). I should be receiving a number value.

    code:

    I2C1.setup({scl:B8, sda:B9});
    
    Serial2.setup(9600/*baud*/);
    
    var resTempVal = "";
    var phVal = "";
    var ecVal = "";
    
    var ready = true;
    var alwaysTempCompensate = false;
    
    function Sensor(id, type, address) {
        this.sensorName = id;
        this.sensorType = type;
        this.sensorAddress = address;
        this.sensorRequireTempComp = false;
    
        this.resTempCal = {
          "clear" : { "cmd" : "Cal,Clear", "wait" : 300  },
          "one"   : { "cmd" : "Cal,n",     "wait" : 1300 },
          "query" : { "cmd" : "Cal,?",     "wait" : 300  }
        };
    
        this.phCal = {
          "clear" : { "cmd" : "Cal,Clear",      "wait" : 300  },
          "mid"   : { "cmd" : "Cal,mid,7.00",   "wait" : 1300 },
          "low"   : { "cmd" : "Cal,low,4.00",   "wait" : 1300 },
          "high"  : { "cmd" : "Cal,high,10.00", "wait" : 1300 },
          "query" : { "cmd" : "Cal,?",          "wait" : 300  }
        };
    
        this.ecCal = {
          "clear" : { "cmd" : "Cal,Clear",  "wait" : 300  },
          "dry"   : { "cmd" : "Cal,dry",    "wait" : 2000 },
          "one"   : { "cmd" : "Cal,one,n",  "wait" : 1300 },
          "low"   : { "cmd" : "Cal,low,n",  "wait" : 1300 },
          "high"  : { "cmd" : "Cal,high,n", "wait" : 1300 },
          "query" : { "cmd" : "Cal,?",      "wait" : 300  }
        };
    
        this.cmdTable = {
          "Calibrate" : {  //List of Calibration commands and timeout value.
          },
          "Information" : {  //Device Information
          },
          "LED" : {  //Enable / Disable or Query the LEDs
            "L0" : { "cmd" : "L,0", "wait" : 300 },
            "L1" : { "cmd" : "L,1", "wait" : 300 },
            "L?" : { "cmd" : "L,?", "wait" : 300 }
          },
          "Reading" : {  //Takes a single reading
            "R" : { "cmd" : "r", "wait" : 1000 } //Takes a single temperature compensated reading
          },
          "Serial"      : {  //Switch back to UART mode
          },
          "Sleep"       : {  //Enter low power sleep mode
          },
          "Status"      : {  //Retrieve status information
          },
          "Temperature" : {  //Set or Query the temperature compensation
            "T"  : { "cmd" : "T", "wait" : 300 },  //Where the temperature is any value; floating point, or int, in ASCII form
            "T?" : { "cmd" : "T,?",   "wait" : 300 }  //Query the set temerature
          },
          "Factory"     : {  //Factory reset
          },
        };
    
        if (type == "temp") {
          this.cmdTable.Calibrate = this.resTempCal;
          //console.log(this.cmdTable.Calibrate);
        }
    
        if (type == "ph") {
          this.cmdTable.Calibrate = this.phCal;
          //console.log(this.cmdTable.Calibrate);
          this.sensorRequireTempComp = true;
        }
    
        if (type == "ec") {
          this.cmdTable.Calibrate = this.ecCal;
          //console.log(this.cmdTable.Calibrate);
          this.sensorRequireTempComp = true;
        }
    }
    
    Sensor.prototype.updateTempComp = function (temp) {
      this.cmdTable.Temperature.T.cmd = "T," + temp;
    };
    
    Sensor.prototype.takeReading = function() {
      var a = this.sensorAddress;
      var c = this.cmdTable.Reading.R.cmd;
      var w = this.cmdTable.Reading.R.wait;
    
      var d = "";
      var sensorStatus = "";
      var sensorValue = "";
    
      var captureSensorData = false;
    
      console.log(a + " " + c);
      I2C1.writeTo(a, c);
    
      setTimeout(function () {
        d = I2C1.readFrom(a, 9);
    
        if (d.length > 0) {
          var dContent = d[0];
    
          switch (dContent) {
            case 1:
              //console.log("Success");
              sensorStatus = 1;
              captureSensorData = true;
              break;
            case 255:
              sensorStatus = 255;
              break;
            case 254:
              sensorStatus = 254;
              break;
            case 2:
              sensorStatus = 2;
              break;
          }
    
          if (captureSensorData === true) {
            for (i = 1; i < d.length; i++) {
              //console.log(i + " = " + d[i]);
              sensorValue += String.fromCharCode(d[i]);
            }
            console.log(sensorValue); //<-----Outputs the value correctly
          }
        }
    
        captureSensorData = false;
    
        return sensorValue;
        //console.log(sensorValue);
      }, w);
    };
    
    var restemp = new Sensor("RES TEMP", "temp", 77);
    var ph = new Sensor("PH", "ph", 78);
    var ec = new Sensor("EC", "ec", 99);
    
    function getAllSensorData() {
      resTempVal = restemp.takeReading();
      return resTempVal;
    }
    
    setInterval(function () {
      console.log("Taking Reading: ");
      getAllSensorData();
    }, 5000);
    
  • I don't really understand - so you're saying that the console.log(sensorValue) is actually correct, when it gets to that point?

    Isn't 77 r coming from console.log(a + " " + c);?

  • Oh crap, ya its coming from console.log(a + " " + c); I forgot to comment that out.

  • I'm getting undefined for resTempVal and undefined outputs before console.log(resTempVal); So, i'm assuming there's a small delay while taking the sensor reading and the variable resTempVal isn't set. Is there a way to wait until resTempVal gets set before I continue?

    code:

    Sensor.prototype.takeReading = function() {
      var a = this.sensorAddress;
      var c = this.cmdTable.Reading.R.cmd;
      var w = this.cmdTable.Reading.R.wait;
    
      var d = "";
      var sensorStatus = "";
      var sensorValue = "";
    
      var captureSensorData = false;
    
      I2C1.writeTo(a, c);
    
      setTimeout(function () {
        d = I2C1.readFrom(a, 9);
    
        if (d.length > 0) {
          var dContent = d[0];
    
          switch (dContent) {
            case 1:
              sensorStatus = 1;
              captureSensorData = true;
              break;
            case 255:
              sensorStatus = 255;
              break;
            case 254:
              sensorStatus = 254;
              break;
            case 2:
              sensorStatus = 2;
              break;
          }
    
          if (captureSensorData === true) {
            for (i = 1; i < d.length; i++) {
              sensorValue += String.fromCharCode(d[i]);
            }
          }
    
          captureSensorData = false;
          console.log(sensorValue); //<-- I get output
          return sensorValue;
        }
      }, w);
    };
    
    var restemp = new Sensor("RES TEMP", "temp", 77);
    var ph = new Sensor("PH", "ph", 78);
    var ec = new Sensor("EC", "ec", 99);
    
    function getAllSensorData() {
      resTempVal = restemp.takeReading();
      console.log(resTempVal); //<-- undefined
    }
    
    setInterval(function () {
      console.log("Taking Reading: ");
      getAllSensorData();
    }, 5000);
    
  • As you say, your problem is that takeReading returns immediately - even though it sets a timeout using setTimeout.

    This is classic JS though. You need to use a callback function:

    Sensor.prototype.takeReading = function(callback) {
      // ...
      setTimeout(function () {
        // ...
        callback(sensorValue);
      }, w);
    };
    
    
    function getAllSensorData() {
      restemp.takeReading(function(resTempVal)­ {
        console.log(resTempVal);
      });
    }
    
  • @Gordon. That did the trick. Thanks

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

I'm expecting a different output....

Posted by Avatar for d0773d @d0773d

Actions