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

Avatar for d0773d @d0773d started