6 Replies Latest reply on Jan 2, 2015 9:23 AM by Intel_Alvarado

    I need help with NodeJS i2c.read()

    dottedquad

      So far I believe I have my ph sensor hooked up properly to i2c pins A4 and A5.  My code is as follows:

      var m = require('mraa');
      var i2c = new m.I2c(1);
      
      i2c.address(0x63);
      i2c.write("R,56.26");
      
      console.log("Reading I2C..");
      
      function readPH() {
          var data = i2c.read();
          console.log( ">> I2C value: " + data);
      }
      setTimeout(function (e) { readPH(); }, 1000);
      

      The code is pretty much straight forward.  I send the command R,56.26 to the ph sensor, wait 1 second and then execute i2c.read().

       

      The datasheet of the ph sensor explains:

      i2c.png

      I used a setTimeout at 1sec to allow enough time for the ph sensor to calculate the value.  After 1second is up readPH() function executes.  i2c.read() does read data; however, when I output the data value I only receive a 1 as the output. How do I read the complete 7 byte string?

        • 1. Re: I need help with NodeJS i2c.read()
          Intel_Alvarado

          Hi dottedquad,

           

          From the datasheet image you posted I assume you are using this pH sensor https://cdn.sparkfun.com/datasheets/Sensors/Biometric/pH_EZO_datasheet_v13.pdf , is that right? According to the datasheet, when that sensor is in i2C mode the power LED lights blue and when data transmission is being sent and read the LED lights in different colors. Do you have this behavior with your device? Also, I see from the i2C.write() line that you are using the device in Single reading mode, why don’t you try using a continuous reading mode to see if there are any changes?

           

          Regards

          Sergio

          • 2. Re: I need help with NodeJS i2c.read()
            dottedquad

            Hi, here is the updated datasheet directly from the manufacturers website:

            http://www.atlas-scientific.com/_files/_datasheets/_circuit/pH_EZO_datasheet.pdf?

             

            I also placed a power isolator between the Intel Edison Arduino break out board i2c bus (A4/A5) and the ph sensor. The data sheet for the power isolator can be found here:

            http://www.atlas-scientific.com/_files/_datasheets/_circuit/pwr-iso.pdf?

            I believe the power isolator already has 10k pullups.

             

            I don't think the continuous mode will work when the ph sensor is in i2c mode and I don't see the continuous command listed under the i2c available commands.

             

            I see the lights blink and change color the way they are supposed to when the command is  sent and when the i2c read is executed.

             

            The 1 that I am receiving corresponds to "Success". To troubleshoot further,  I wrote R,34.53 to the ph sensor and then prematurely executed i2c.read () and received 254 which corresponds to "Pending". So, I believe I am receiving the result or a partial result from the sensor.

             

            I'm confused as to where is the remaining data? Is the i2c connection getting terminated prematurely? Or maybe the issue has to do with the power isolator already having 10k pullups?

            • 3. Re: I need help with NodeJS i2c.read()
              dottedquad

              I forgot to mention, the ph sensor operates at 100khz.

              • 4. Re: I need help with NodeJS i2c.read()
                dottedquad

                Hi Intel_Alvarado,

                I read through the mraa github example section which explains, "There is no explicit nodejs API documentation, see the examples. The API itself is very similar to the python one but with a js syntax." So I wrote a rather simple python script that gets the Ph Sensor value successfully. 

                 

                Python code:

                    import time

                    import mraa

                   

                    i2c = mraa.I2c(1)

                    i2c.address(0x63)

                   

                    i2c.write("R,23.5")

                   

                    time.sleep(1.3)

                    d = "       "

                    i2c.read(d)

                    print(d)

                  The python code outputs: 2.974 Getting a result proves my circuit design works and now I need to figure out what the NodeJS API is to get a successful i2c reading. Would you or anyone else know the NodeJS API for getting an i2c reading?

                • 5. Re: I need help with NodeJS i2c.read()
                  dottedquad

                  I was able to get a working example :-)

                   

                  var m = require('mraa');

                  var i2c = new m.I2c(1);

                  i2c.address(0x63);

                  i2c.write("R,56.26");

                  console.log("Reading I2C..");

                  function readPH() {

                      var d = i2c.read(7);

                      console.log(">> " + d);

                  }

                  setTimeout(function (e) { readPH(); }, 1000);

                  • 6. Re: I need help with NodeJS i2c.read()
                    Intel_Alvarado

                    Hi dottedquad,

                     

                    I’m sorry for the delay in my response. If you have found the answer for this thread, please mark as assumed or correct answer.

                     

                    Regards

                    Sergio