Copyright ©

Mongoose OS Forum

ATTENTION! This forum has moved to:

Do not post any new messages.

Javascript lib i2c

CCesarCCesar Recife, Brasil

Hi folks.

Have anyone used the javascript lib i2c?
I have some question about that, for example the read function:, addr, data, len, stop)
I don't understand what does data means? and the return that function are true or false, I supposed that when I call that function the return is a value.

Sorry for my stupid question if it's.

Thank you!


  • Example: (read temperature and humidity from SHT30 device)

    let myI2C = I2C.get_default();
    I2C.write(myI2C, 0x45, "\x21\x30", 2, true);  // initiate periodic measurement of SHT30, address 0x45
    let result = "00000000000000000000"; //here the result will be saved, large enough
    I2C.write(myI2C, 0x45, "\xe0\x00", 2, true);  //Initiate date delivery of SHt30
    let success=, 0x45, result, 6, true);  //Read data (6 Bytes) and release bus
    let temp = (((( << 8) + / 65535) * 175) - 45;  //Convert to Values
    let hum = (((( << 8) + / 65535) * 100);

    From the file api_i2c.js
    // ## **, addr, data, len, stop)
      // Read specified number of
      // bytes from the specified address.
      // If stop is true, the bus will be released at the end.
      // Return value: success, true/false.
      read: ffi('bool mgos_i2c_read(void *, int, char *, int, bool)'),
  • CCesarCCesar Recife, Brasil

    Hi Capsicum!
    First at all thank you very much.
    I did a mistake when I was using the i2c.write function
    I was using like that:
    I2C.write(i2c, addr, "00", 2, true);
    but like you said the correct is:
    I2C.write(i2c, addr, "\x00", 2, true);

    I understand as well what data means, actually data are bytes read by the i2c bus.

    Thank you

  • Hello,

    the api_i2c.js has changed.

    Now you have to use:
    let result=, 0x45, 6, true); //Read data (6 Bytes) and release bus

  • CCesarCCesar Recife, Brasil

    Thanks guys!

    Very much clear now.
    Well done

Sign In or Register to comment.