Copyright © https://mongoose-os.com

Mongoose OS Forum

frame
ATTENTION! This forum has moved to:

https://community.mongoose-os.com

Do not post any new messages.

Unable to read data from 12C support sensor.

mtgmtg pakistan
edited March 2018 in Mongoose OS

Hi
I am new to mongoose os and wants to explore it more.currently i am interfacing my Gy-521 sensor to my ESP8266 but after a lot of try i am unable to get it done.
Here is my simple app code in js.
load('api_gpio.js');
load('api_mqtt.js');
load('api_sys.js');
load('api_config.js');
load('api_i2c.js');
load('api_timer.js');

let pin = 0;   // GPIO 0 is typically a 'Flash' button
let led=16;
let buff = '\x41'; // i want to read from data from this register.
let addr = 68; //my 12C sensor address.
let i2c = I2C.get_default();
GPIO.set_button_handler(pin, GPIO.PULL_UP, GPIO.INT_EDGE_NEG,200, function(x) {
  let topic = Cfg.get('device.id') + '/button_pressed';
  let message = JSON.stringify({
    total_ram: Sys.total_ram(),
    free_ram: Sys.free_ram()
  });
  let ok = MQTT.pub(topic, message, 1);
  print('Published:', ok ? 'yes' : 'no', 'topic:', topic, 'message:', message);
}, true);


GPIO.set_mode(led,GPIO.MODE_OUTPUT);
Timer.set(3000 /* 1 sec */, true /* repeat */, function() {
  I2C.write(i2c, addr, buff, buff.length, true);
  let buf = 'xy';
  print("I2C.read()?", I2C.read(i2c, addr, buf, 1, true));
  print("buf.length?", buf.length);
  print("buf?", buf);
  print('tick');
}, null);
print('Flash button is configured on GPIO pin ', pin);
print('Press the flash button now!');

Here is my Log info

E0321 22:39:11.878658 6916 ui.go:537] Calling: Config.Get
[Mar 21 22:39:12.610] E0321 22:39:12.610964 6916 ui.go:563] Call complete, success
[Mar 21 22:39:13.583] at api_i2c.js:31
[Mar 21 22:39:13.587] at init.js:34
[Mar 21 22:39:13.588] MJS callback error: failed to call FFIed function: actual arg #3 is not an int (the type idx is: string)
[Mar 21 22:39:16.584] at api_i2c.js:31
[Mar 21 22:39:16.587] at init.js:34

Any one have any idea about the working or correct my mistakes please help.any help would be appreciated.

«1

Comments

  • nliviunliviu Romania

    Read I2C.read
    It looks like you are not using it correctly.
    You can try

    let buf=I2C.read(i2c, addr, 1, true);
    
  • mtgmtg pakistan
    edited March 2018

    Hi sir thank you for your precious time i modified my code.
    Timer.set(3000 /* 1 sec /, true / repeat */, function() {
    I2C.write(i2c, addr, buff, buff.length, true);
    let buf=I2C.read(i2c, addr, 1, true);
    print('buff data is ',buf);

          print('tick');
        }, null);
    

    but now its say in the log

    buff data is null

  • mtgmtg pakistan

    this is my modified mongoose js code sir.but i am still confuse sir and i am getting any output sir.i am new to mongoose os so making a silly mistakes.again thank you for precious time sir.

        load('api_gpio.js');
        load('api_mqtt.js');
        load('api_sys.js');
        load('api_config.js');
        load('api_i2c.js');
        load('api_timer.js');
    
        let pin = 0;   // GPIO 0 is typically a 'Flash' button
        let led=16;
        let buff = '\x41'; 
        let addr = '\x68';
        let p_r='6B';
        let z_r='0';
        let i2c = I2C.get_default();
        I2C.write(i2c,addr,p_r,p_r.length,false);
        I2C.write(i2c,addr,z_r,z_r.length,true);
    
        GPIO.set_mode(led,GPIO.MODE_OUTPUT);
        Timer.set(1000 /* 1 sec */, true /* repeat */, function() {
           print('tick');
          I2C.write(i2c, addr, buff, buff.length, true);
          let data=I2C.read(i2c,addr,6, true);
          print('buff data is ',data);
         if (data) print(JSON.stringify([data.at(0), data.at(1), data.at(2)]));
    
        }, null);
        print('Flash button is configured on GPIO pin ', pin);
        print('Press the flash button now!');   
    

    And this is my working arduino code sir.

    #include<Wire.h>
    #include<LiquidCrystal.h>
    const int MPU=0x68; //I2C address of MPU
    float temp;
    void setup()
    {
    Wire.begin();
    Wire.beginTransmission(MPU);
    Wire.write(0x6B); //power management register 1
    Wire.write(0);
    Wire.endTransmission(true);
    Serial.begin(9600);
    }

    void loop()
    {
    Wire.beginTransmission(MPU);
    Wire.write(0x41); //starts with MPU register 41(TEMP_OUT_H)
    Wire.endTransmission(false);
    Wire.requestFrom(MPU,6,true); //requests 2 registers
    
    temp=Wire.read()<<8|Wire.read();
    temp=(temp/340)+36.53; //equation for temperature from datasheet
    Serial.print(" Temperature = "); Serial.println(temp);
    delay(1000);
    }
    
  • mtgmtg pakistan

    this is my log info sir.
    mgos_init Init done, RAM: 52008 total, 42832 free, 42232 min free
    at init.js:15
    MJS error: failed to call FFIed function: actual arg #1 is not an int (the type idx is: string)

  • nliviunliviu Romania

    You might want to try:

    let addr = 0x68;
    let i2c = I2C.get(); // don't use get_default because is deprecated 
    //https://github.com/mongoose-os-libs/i2c/blob/90f4ef96811dea7b82e59adfb67e58c8cb5ce7e2/mjs_fs/api_i2c.js#L8
    
    //init sensor - write 0x00 into register 0x6B
    let initOK = I2C.writeRegB(i2c, addr, 0x6B, 0x00); // doc for I2C.writeRegB
    //https://github.com/mongoose-os-libs/i2c/blob/90f4ef96811dea7b82e59adfb67e58c8cb5ce7e2/mjs_fs/api_i2c.js#L80
    if (false === initOK) {
        print('!!!! - failed to init sensor - !!!!');
    } else {
        // setup the read timer
        Timer.set(1000 /* 1 sec */, true /* repeat */, function () {
            // Read the temperature data - a 16 bit value from registers 0x41 and 0x42
            let temp = I2C.readRegW(i2c, addr, 0x41); //doc for I2C.readRegW 
            //https://github.com/mongoose-os-libs/i2c/blob/90f4ef96811dea7b82e59adfb67e58c8cb5ce7e2/mjs_fs/api_i2c.js#L51
            if (null === temp) {
                print('!!!! - Could not get temperature data - !!!!');
            } else {
                temp = (temp / 340) + 36.53; //equation for temperature from datasheet
                print('++++ - Temperature=', temp);
            }
        }, null);
    }
    
  • mtgmtg pakistan

    Hi sir now i am getting this on LOG.
    mgos_init Init done, RAM: 52008 total, 42832 free, 42232 min free
    [Mar 22 23:25:35.234] !!!! - failed to init sensor - !!!!
    [Mar 22 23:25:35.239] mgos_aws_shadow_init AWS Device Shadow requires MQTT
    [Mar 22 23:25:35.248] mongoose_poll New heap free LWM: 32088

  • nliviunliviu Romania
    edited March 2018

    Do you have a line like this in your console log?

    [Mar 22 20:19:50.574] mgos_i2c_create      I2C GPIO init ok (SDA: 4, SCL: 5, freq: 100000)
    

    The important part is mgos_i2c_create.

    If not, you must add in mos.yml,

    config_schema:
     - ["i2c.enable", true]
    
    libs:
      - origin: https://github.com/mongoose-os-libs/i2c
    

    After that rebuild and reflash the application.
    The default i2c pins for ESP8266 are

    - ["i2c.sda_gpio", 12]
    - ["i2c.scl_gpio", 14]
    

    If you use other pins, they also have to be defined in mos.yml.
    Eg:

    config_schema:
     - ["i2c.sda_gpio", 4]
     - ["i2c.scl_gpio", 5]
    
  • mtgmtg pakistan

    this is Log info sir
    mgos_mdns_hal_join_g Joining multicast group 224.0.0.251
    [Mar 22 23:39:25.214] mgos_i2c_create I2C GPIO init ok (SDA: 12, SCL: 14, freq: 100000)
    [Mar 22 23:39:25.221] mgos_ota_shadow_init OTA over shadow initialised
    and this is my mos.yml file

    author: mongoose-os
    description: Internet button on AWS IoT
    # arch: PLATFORM
    version: 1.0
    manifest_version: 2017-05-18
    
    libs_version: ${mos.version}
    modules_version: ${mos.version}
    mongoose_os_version: ${mos.version}
    
    tags:
      - js
      - c
      - cloud
      - aws
    
    filesystem:
      - fs
    config_schema:
     - ["i2c.enable", true]
    libs:
        # common mgos libs
      - origin: https://github.com/mongoose-os-libs/ca-bundle
      - origin: https://github.com/mongoose-os-libs/i2c
      - origin: https://github.com/mongoose-os-libs/rpc-service-config
      - origin: https://github.com/mongoose-os-libs/rpc-service-fs
      - origin: https://github.com/mongoose-os-libs/rpc-uart
      - origin: https://github.com/mongoose-os-libs/spi
    
        # libs necessary for the current app
      - origin: https://github.com/mongoose-os-libs/aws
      - origin: https://github.com/mongoose-os-libs/mjs
      - origin: https://github.com/mongoose-os-libs/wifi
    
  • nliviunliviu Romania

    And you have SDA connected to GPIO12 and SCL to GPIO14?

  • mtgmtg pakistan

    yes sir.
    my SDA is connected to my node mcu D6(12) and SCL to D5(14)...the arduino code is working fine sir without changing any connection.

  • nliviunliviu Romania

    I'm out of ideas. It should work, unless there is something wrong with the pins mapping...

  • mtgmtg pakistan

    but sir when i upload the arduino sketch then it works but when i try it from mongoose os then it gives this error...dont know what to do next sir.

  • nliviunliviu Romania

    What board did you select in Arduino IDE?

  • mtgmtg pakistan

    check this.

  • nliviunliviu Romania

    Most of the boards use

    #define PIN_WIRE_SDA (4)
    #define PIN_WIRE_SCL (5)
    

    Try to modify

    config_schema:
     - ["i2c.sda_gpio", 4]
     - ["i2c.scl_gpio", 5]
    

    rebuild and reflash.

  • mtgmtg pakistan

    now my mos.yml file sir
    tags:
    - js
    - c
    - cloud
    - aws
    filesystem:
    - fs
    config_schema:
    - ["i2c.enable", true]
    - ["i2c.sda_gpio", 4]
    - ["i2c.scl_gpio", 5]
    libs:
    # common mgos libs
    - origin: https://github.com/mongoose-os-libs/ca-bundle
    - origin: https://github.com/mongoose-os-libs/i2c
    - origin: https://github.com/mongoose-os-libs/rpc-service-config
    - origin: https://github.com/mongoose-os-libs/rpc-service-fs
    - origin: https://github.com/mongoose-os-libs/rpc-uart
    - origin: https://github.com/mongoose-os-libs/spi

        # libs necessary for the current app
      - origin: https://github.com/mongoose-os-libs/aws
      - origin: https://github.com/mongoose-os-libs/mjs
      - origin: https://github.com/mongoose-os-libs/wifi
    

    and i connect my sda of sensor to D2 ans scl to D1,and now the log again shows this error.

    mgos_mdns_hal_join_g Joining multicast group 224.0.0.251
    [Mar 23 15:46:01.478] mgos_i2c_create I2C GPIO init ok (SDA: 12, SCL: 14, freq: 100000)
    [Mar 23 15:46:01.485] mgos_ota_shadow_init OTA over shadow initialised
    [Mar 23 15:46:01.490] scandone
    [Mar 23 15:46:01.492] del if0
    [Mar 23 15:46:01.493] usl
    [Mar 23 15:46:01.493] mode : null
    [Mar 23 15:46:01.494] mgos_wifi_setup WiFi mode: STA
    [Mar 23 15:46:01.498] mgos_wifi_set_mode WiFi mode: STA
    [Mar 23 15:46:01.502] mode : sta(2c:3a:e8:0b:5b:b5)
    [Mar 23 15:46:01.505] add if0
    [Mar 23 15:46:01.505] WPA2 ENTERPRISE VERSION: [v2.0] disable
    [Mar 23 15:46:01.509] mgos_wifi_setup_sta WiFi STA: Connecting to M T G
    [Mar 23 15:46:01.515] mgos_http_server_ini HTTP server started on [80]
    [Mar 23 15:46:01.522] mgos_ota_http_client Init done, ota_lib=(null), ota handler 1
    [Mar 23 15:46:01.530] mg_rpc_channel_uart 0x3fff124c UART0
    [Mar 23 15:46:01.536] mgos_init Init done, RAM: 52008 total, 42832 free, 42232 min free
    [Mar 23 15:46:02.111] !!!! - failed to init sensor - !!!!
    [Mar 23 15:46:02.115] mgos_aws_shadow_init AWS Device Shadow requires MQTT

  • nliviunliviu Romania

    Do not change the connections. Keep the sensor connected as it works with the Arduino.
    Something is wrong because the I2C inits with 12 and 14, instead of 4 and 5 mgos_i2c_create I2C GPIO init ok (SDA: 12, SCL: 14, freq: 100000)
    mos build --platform esp8266 --clean and mos flash --port <your_port_here>

  • mtgmtg pakistan

    Same result sir...failed to init sensor.

  • nliviunliviu Romania

    Did mgos_i2c_create I2C GPIO init ok (SDA: 12, SCL: 14, freq: 100000) change to mgos_i2c_create I2C GPIO init ok (SDA: 4, SCL: 5, freq: 100000)?
    Enable i2c.debug and set debug.level to 3, rebuild, reflash.

    config_schema:
     - ["i2c.debug", true]
     - ["debug.level", 3]
    
  • mtgmtg pakistan
    edited March 2018

    my mos.yml file

        author: mongoose-os
        description: Internet button on AWS IoT
        # arch: PLATFORM
        version: 1.0
        manifest_version: 2017-05-18
        libs_version: ${mos.version}
        modules_version: ${mos.version}
        mongoose_os_version: ${mos.version}
        tags:
          - js
          - c
          - cloud
          - aws
        filesystem:
          - fs
        config_schema:
         - ["i2c.enable", true]
         - ["i2c.debug", true]
         - ["debug.level", 3]
        libs:
            # common mgos libs
          - origin: https://github.com/mongoose-os-libs/ca-bundle
          - origin: https://github.com/mongoose-os-libs/i2c
          - origin: https://github.com/mongoose-os-libs/rpc-service-config
          - origin: https://github.com/mongoose-os-libs/rpc-service-fs
          - origin: https://github.com/mongoose-os-libs/rpc-uart
          - origin: https://github.com/mongoose-os-libs/spi
            # libs necessary for the current app
          - origin: https://github.com/mongoose-os-libs/aws
          - origin: https://github.com/mongoose-os-libs/mjs
          - origin: https://github.com/mongoose-os-libs/wifi
    

    my log info sir.

    mg_rpc_channel_uart  0x3fff124c UART0
    [Mar 23 16:47:33.389] mgos_init            Init done, RAM: 52008 total, 42832 free, 42232 min free
    [Mar 23 16:47:33.970] !!!! - failed to init sensor - !!!! 
    [Mar 23 16:47:33.981] Flash button is configured on GPIO pin  0 
    [Mar 23 16:47:33.987] Press the flash button now! 
    [Mar 23 16:47:33.990] mgos_aws_shadow_init AWS Device Shadow requires MQTT
    [Mar 23 16:47:34.001] mongoose_poll        New heap free LWM: 31632
    [Mar 23 16:47:34.006] mgos_net_on_change_c WiFi STA: connecting
    [Mar 23 16:47:34.028] E0323 16:47:34.027953    2612 ui.go:537] Calling: Sys.GetInfo
    [Mar 23 16:47:34.314] E0323 16:47:34.314135    2612 ui.go:563] Call complete, success
    [Mar 23 16:47:36.738] scandone
    [Mar 23 16:47:37.622] state: 0 -> 2 (b0)
    [Mar 23 16:47:37.626] state: 2 -> 3 (0)
    [Mar 23 16:47:37.630] state: 3 -> 5 (10)
    

    my init.js code sir.

    load('api_gpio.js');
    load('api_mqtt.js');
    load('api_sys.js');
    load('api_config.js');
    load('api_i2c.js');
    load('api_timer.js');
    let pin = 0;   // GPIO 0 is typically a 'Flash' button
    let addr = 0x68;
    let i2c = I2C.get(); // don't use get_default because is deprecated 
    //https://github.com/mongoose-os-libs/i2c/blob/90f4ef96811dea7b82e59adfb67e58c8cb5ce7e2/mjs_fs/api_i2c.js#L8
    
    //init sensor - write 0x00 into register 0x6B
    let initOK = I2C.writeRegB(i2c, addr, 0x6B, 0x00); // doc for I2C.writeRegB
    //https://github.com/mongoose-os-libs/i2c/blob/90f4ef96811dea7b82e59adfb67e58c8cb5ce7e2/mjs_fs/api_i2c.js#L80
    if (false === initOK) {
        print('!!!! - failed to init sensor - !!!!');
    } else {
        // setup the read timer
        Timer.set(1000 /* 1 sec */, true /* repeat */, function () {
            // Read the temperature data - a 16 bit value from registers 0x41 and 0x42
            let temp = I2C.readRegW(i2c, addr, 0x41); //doc for I2C.readRegW 
            //https://github.com/mongoose-os-libs/i2c/blob/90f4ef96811dea7b82e59adfb67e58c8cb5ce7e2/mjs_fs/api_i2c.js#L51
            if (null === temp) {
                print('!!!! - Could not get temperature data - !!!!');
            } else {
                temp = (temp / 340) + 36.53; //equation for temperature from datasheet
                print('++++ - Temperature=', temp);
            }
        }, null);
    }
    
    GPIO.set_button_handler(pin, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 50, function(x) {
      let topic = Cfg.get('device.id') + '/button_pressed';
      let message = JSON.stringify({
        total_ram: Sys.total_ram(),
        free_ram: Sys.free_ram()
      });
      let ok = MQTT.pub(topic, message, 1);
      print('Published:', ok ? 'yes' : 'no', 'topic:', topic, 'message:', message);
    }, true);
    
    print('Flash button is configured on GPIO pin ', pin);
    print('Press the flash button now!');
    
  • nliviunliviu Romania

    Please show the output of the following commands:
    mos config-get debug
    mos config-get i2c

  • mtgmtg pakistan
    edited March 2018

    By entering > mos config-get debug in terminal

        pm open,type:2 0
        [Mar 23 18:54:32.160] {
        [Mar 23 18:54:32.160]   "factory_reset_gpio": -1,
        [Mar 23 18:54:32.160]   "filter": "",
        [Mar 23 18:54:32.160]   "level": 2,
        [Mar 23 18:54:32.160]   "mbedtls_level": 0,
        [Mar 23 18:54:32.160]   "mg_mgr_hexdump_file": "",
        [Mar 23 18:54:32.160]   "stderr_topic": "",
        [Mar 23 18:54:32.160]   "stderr_uart": 0,
        [Mar 23 18:54:32.160]   "stdout_topic": "",
        [Mar 23 18:54:32.160]   "stdout_uart": 0,
        [Mar 23 18:54:32.160]   "udp_log_addr": ""
        [Mar 23 18:54:32.160] }
    

    By entering > mos config-get i2c

         {
        [Mar 23 18:56:04.608]   "debug": false,
        [Mar 23 18:56:04.608]   "enable": true,
        [Mar 23 18:56:04.608]   "freq": 100000,
        [Mar 23 18:56:04.608]   "scl_gpio": 14,
        [Mar 23 18:56:04.608]   "sda_gpio": 12
        [Mar 23 18:56:04.608] }
    
  • nliviunliviu Romania

    I've asked you earlier to rebuild and reflash the firmware. It looks like you didn't do that.
    The settings in the mos.yml were not applied.
    It's useless to modify mos.yml without rebuild and reflash:
    mos build --platform esp8266 --clean and mos flash --port <your_port_here>

  • mtgmtg pakistan

    By entering this on terminal sir

    build --platform esp8266 --clean

    mongoose_poll New heap free LWM: 27912
    [Mar 23 20:09:47.378] E0323 20:09:47.378474 3760 ui.go:537] Calling: Sys.GetInfo
    [Mar 23 20:09:47.664] E0323 20:09:47.664462 3760 ui.go:563] Call complete, success
    Server Error: {"error":"No mos.yml file"}
    [Mar 23 20:10:24.575] Published: no topic: esp8266_0B5BB5/button_pressed message: {"free_ram":36532,"total_ram":52008}
    Server Error: {"error":"No mos.yml file"}
    [Mar 23 20:10:46.270] Published: no topic: esp8266_0B5BB5/button_pressed message: {"free_ram":36496,"total_ram":52008}

    and by entering

    mos flash --port <12>

    it simply flash the demo project into esp8266 that blinks an led

  • nliviunliviu Romania

    Aahhhh. You use the web IDE.
    I suppose you saved mos.yml after modifying it.
    Then click the build button and when the build process is finished, click the flash button.
    If you see something like Fetching https://github.com/mongoose-os-apps/demo-js/releases/download/latest..., after clicking flash, in the log window, close the browser tab, go to your cmd window, press Ctrl+C to exit mos, restart mos and click the flash button. This time it should flash the firmware and reboot the device.
    Search the lines in the log with mgos_i2c_ and post them here.

  • mtgmtg pakistan

    Hi sir sorry for delying..i completely reinstall my window and try to download the mongoose os tool and retry it but again i am getting the same result sir.

    bb.png 200.7K
  • nliviunliviu Romania

    The line mgos_i2c_send_byte sent 0xd0, recd NAK says that the device is not present on the I2C bus.

  • mtgmtg pakistan

    i don;t know sir what's going on and what to do when i upload the code and check it via arduino ide it works fine as expected sir.

  • The first thing to check is that the I2C bus can see your sensor.

    The easiest way to do this is to use the Device Service tab from the Web IDE.

    If you run I2C,scan command you should see the address of your sensor in the result (You are looking for a value of 102 (0x68). If don't see this value it is going to be a configuration error rather than a coding issue.

    The MPU6050 is not the easiest chip to work with, but it is possible.

Sign In or Register to comment.