You are reading a single comment by @d0773d and its replies. Click here to read the full conversation.
  • For what it's worth here is my code that I hacked together before you responded a second time to my my original post. Its rather messy and I could probably do this a different way especially change the nested setTimeouts. The reason why I used setTimouts is to wait before I gather the other sensor data. On average, it takes 300ms to process a "read command" then I can issue a a "readFrom". On some occasions, depending on the sensor, it can take 1300ms to process a "read command" before I can issue an I2C "readFrom". The nested timeouts allows me to:

    First, send a read command to the temp sensor then take the temp
    value and update the ph and ec temperature compensation json data.
    Also, save the temp value to a json object.

    Second, send a temp compensation update command to both the ph and
    ec sensor. It takes 300ms for each of the ph and ec sensors to process
    that command.

    Third, send a ph read command and wait 300ms to execute an i2c
    readFrom the ph sensor to gather the data. Also, save the ph value
    to a json object.

    Fourth, send an ec read command and wait 1300ms to execute an i2c
    readFrom the ec sensor to gather the data. Also, save the ec value
    to a json object.

    After all that I send the JSON object via serial to another device.

    Another reason for my nested timeouts was, well atleast I thought, If I send a read command to both the ph and ec sensors at the the same time(after their temp compensations was updated), thought the i2c would crash or timeout...

    I'll have to read through your post and attempt to implement the information into my code.

    I2C1.setup({scl:B8, sda:B9});
    
    Serial2.setup(9600/*baud*/);
    
    var resTempVal = 0;
    var phVal = "";
    var ecVal = "";
    
    var ready = true;
    var alwaysTempCompensate = false;
    
    var sensorReadings = { restempvalue : 0, phvalue : 0, ecvalue : 0 };
    
    //console.log(sensorReadings.phvalue);
    
    function Sensor(id, type, address, buffer) {
        this.sensorName = id;
        this.sensorType = type;
        this.sensorAddress = address;
        this.sensorResultBuff = buffer;
        //this.sensorReading = "";
        this.sensorRequireTempComp = false;
    
        this.sensorResTempValue = 0;
    
        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,19.5", "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;
          var celsiusVal = 0;
        }
    
        if (type == "ph") {
          this.cmdTable.Calibrate = this.phCal;
          this.sensorRequireTempComp = true;
        }
    
        if (type == "ec") {
          this.cmdTable.Calibrate = this.ecCal;
          this.sensorRequireTempComp = true;
        }
    }
    
    Sensor.prototype.updateTempCompJSON = function (temp) {
      //console.log("Temp Comp: " + temp);
      this.cmdTable.Temperature.T.cmd = "T," + temp;
    };
    
    Sensor.prototype.updateTempComp = function () {
      var a = this.sensorAddress;
      var cmd = this.cmdTable.Temperature.T.cmd;
    
      I2C1.writeTo(a, cmd);
    };
    
    Sensor.prototype.takeReading = function(callback) {
      var a = this.sensorAddress;
      var c = this.cmdTable.Reading.R.cmd;
      var w = this.cmdTable.Reading.R.wait;
      var b = this.sensorResultBuff;
    
      var d = "";
      var sensorStatus = "";
      var sensorValue = "";
    
      var captureSensorData = false;
    
      I2C1.writeTo(a, c);
    
      setTimeout(function () {
        d = I2C1.readFrom(a, b);
    
        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;
    
        callback(sensorValue);
      }, w);
    };
    
    var restemp = new Sensor("RES TEMP", "temp", 77, 9);
    var ph = new Sensor("PH", "ph", 78, 9);
    var ec = new Sensor("EC", "ec", 99, 14);
    
    function getAllSensorData() {
      setTimeout(function () {
        restemp.takeReading(function(val) {
          var f = parseFloat(val).toFixed(1);
          var celsiusVal = (5/9) * (f-32);
    
          //console.log(celsiusVal);
    
          sensorReadings.restempvalue = f;
    
          setTimeout(function () {
            ph.updateTempCompJSON(celsiusVal.toFixed­(1));
            ph.updateTempComp();
          }, 1000);
    
          setTimeout(function () {
            ec.updateTempCompJSON(celsiusVal.toFixed­(1));
    
            ec.updateTempComp();
          }, 2000);
          console.log("RES TEMP READING: " + sensorReadings.restempvalue);
        });
      }, 100);
    
      setTimeout(function () {
        ph.takeReading(function(val) {
          var f = parseFloat(val).toFixed(1);
          sensorReadings.phvalue = f;
          console.log("PH READING: " + sensorReadings.phvalue);
        });
      }, 4000);
    
      setTimeout(function () {
        ec.takeReading(function(val) {
          var f = val;
          sensorReadings.ecvalue = f;
          console.log("EC READING: " + sensorReadings.ecvalue);
        });
      }, 5300);
    }
    
    setInterval(function () {
      //console.log("Taking Reading: ");
      getAllSensorData();
    }, 10000);
    
About

Avatar for d0773d @d0773d started