Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 
    SOLVED

    Not getting gas resistance values in bme680. (Got Output for Temperature, Pressure and Humidity)

    Not getting gas resistance values in bme680. (Got Output for Temperature, Pressure and Humidity)

    VinayakMH
    Member

    Dear All,

    I am working with bme 680 environmental sensor interfacing with nRF52840-DK. I got Temperature, pressure and humidity data. But i am not getting Gas resistance values.  i have attched my code also. I will be very happy if you help in this.

    Thank you all.

    7 REPLIES 7

    Minhwan
    Community Moderator
    Community Moderator

    Hello VinayakMH, 

     

    I think you optimize our bsec library example. 

    Could you use our function? 

    Since there is some return value in our function, we can check why it doens't work. 

    If you follow our example code and there is no return value, could you upload your source code one more time? 

    Thanks, 

     

    Thank you Minhwan for your reply.

    This is my source code.

     

    #include <stdio.h>

    #include "boards.h"

    #include "app_util_platform.h"

    #include "app_error.h"

    #include "nrf_drv_twi.h"

    #include "nrf_delay.h"

     

    #include "nrf_log.h"

    #include "nrf_log_ctrl.h"

    #include "nrf_log_default_backends.h"

    #include "bme680.h"

    #include "bme680_defs.h"

     

     

     

    struct bme680_dev gas_sensor;

    int8_t rslt = BME680_OK;

     

    /* TWI instance ID. */

    #define TWI_INSTANCE_ID 0

     

    /* TWI instance. */

    static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);

     

    int8_t user_i2c_read (uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len)

    {

    ret_code_t err;

    uint8_t var[30]={0};

     

    int size=0, result = 0, j=0;

    uint8_t reg[1]={reg_addr};

    nrf_drv_twi_tx(&m_twi, dev_id, reg, 1,false);

    err = nrf_drv_twi_rx(&m_twi, dev_id, var, len);

     

    if(err==0)

    {

    size=1;

    for(int i = 0 ;i<30;i++)

    {

    if(var[i]!=0) size=i+1;

    }

    }

    else

    {

    size=0;

    }

     

    if(size ==15)

    size=len;

    if(len<=size)

    {

    for (int i=0;i<len;i++)

    {

    data[i]=var[i];

    }

    }

    else

    {

    result=5;

    }

    return result;

    }

     

    int8_t user_i2c_write (uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len)

    {

    int result=0;

    uint8_t reg[]={reg_addr,*data};

    nrf_drv_twi_tx(&m_twi, dev_id, reg, len+1, false);

    for(int i = 0;i<len;i++)

    return result;

    }

    void user_delay_ms (uint32_t period)

    {

    nrf_delay_ms(period);

    }

    void twi_init (void)

    {

    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config = {

    .scl = 27,

    .sda = 26,

    .frequency = NRF_DRV_TWI_FREQ_100K,

    .interrupt_priority = APP_IRQ_PRIORITY_HIGH,

    .clear_bus_init = false

    };

    err_code = nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL);

    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi);

    }

     

    void init_bme (void)

    {

    uint8_t set_required_settings;

     

    /* Set the temperature, pressure and humidity settings */

     

    gas_sensor.tph_sett.os_hum = BME680_OS_2X;

    gas_sensor.tph_sett.os_pres = BME680_OS_4X;

    gas_sensor.tph_sett.os_temp = BME680_OS_8X;

    gas_sensor.tph_sett.filter = BME680_FILTER_SIZE_3;

     

    /* Set the remaining gas sensor settings and link the heating profile */

    gas_sensor.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS;

     

    /* Create a ramp heat waveform in 3 steps */

     

    gas_sensor.gas_sett.heatr_temp = 300; /* degree Celsius */

    gas_sensor.gas_sett.heatr_dur = 100; /* milliseconds */

     

    /* Select the power mode */

    /* Must be set before writing the sensor configuration */

    gas_sensor.power_mode = BME680_FORCED_MODE;

     

    /* Set the required sensor settings needed */

    set_required_settings = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_FILTER_SEL| BME680_GAS_SENSOR_SEL;

     

    /* Set the desired sensor configuration */

    rslt = bme680_set_sensor_settings(set_required_settings,&gas_sensor);

    /* Set the power mode */

    rslt = bme680_set_sensor_mode(&gas_sensor);

    }

     

    int main(void)

    {

    ret_code_t err_code;

     

    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));

    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO("TWI scanner started.");

    NRF_LOG_FLUSH();

    twi_init();

     

    gas_sensor.dev_id = BME680_I2C_ADDR_SECONDARY;

    gas_sensor.intf = BME680_I2C_INTF;

    gas_sensor.read = user_i2c_read;

    gas_sensor.write = user_i2c_write;

    gas_sensor.delay_ms = user_delay_ms;

    gas_sensor.amb_temp = 25;

    rslt = bme680_init(&gas_sensor);

     

    init_bme();

    NRF_LOG_INFO("chip_id : %d :",gas_sensor.chip_id);

    NRF_LOG_FLUSH();

     

    NRF_LOG_INFO("Rslt : %d",rslt);

    NRF_LOG_FLUSH();

    if(rslt!=0)

    {

    while(1);

    }

    uint8_t data[1]={140};

    uint8_t regt[1]={0x74};

    uint8_t regh[1]={0x72};

    uint8_t regs[1]={0x71};

    uint8_t addr = 0xE0;

    uint8_t val = 0xB6;

    bme680_set_regs(&addr, &val, 1, &gas_sensor);

    bme680_set_regs(regt, data, 1, &gas_sensor);

    bme680_set_regs(regh, data, 1, &gas_sensor);

    bme680_set_regs(regs, data, 1, &gas_sensor);

    //NRF_LOG_INFO("Register1 0x74 : %d",dat[0]);

    nrf_delay_ms(500);

    //NRF_LOG_INFO("Register2 0x72 : %d",dat[0]);

    nrf_delay_ms(500);

    NRF_LOG_FLUSH();

     

     

    while (1)

    {

    uint16_t meas_period;

    bme680_get_profile_dur(&meas_period, &gas_sensor);

    struct bme680_field_data data;

    char temperature[20];

    char pressure[20];

    char humidity[20];

     

    while(1)

    {

    nrf_delay_ms(meas_period);

    rslt = bme680_get_sensor_data(&data, &gas_sensor);

    //NRF_LOG_INFO("Read Result: %d",rslt);

    NRF_LOG_FLUSH();

    //NRF_LOG_INFO("%d",data.temperature);

    nrf_delay_ms(2000);

    NRF_LOG_FLUSH();

    NRF_LOG_ERROR( "TEMPERATURE : " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.temperature /100.0));

    NRF_LOG_FLUSH();

    nrf_delay_ms(2000);

    NRF_LOG_ERROR( "PRESSURE : " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.pressure/1000.0));

    NRF_LOG_FLUSH();

    nrf_delay_ms(2000);

    NRF_LOG_ERROR( "HUMIDITY: " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.humidity /1000.0));

    NRF_LOG_FLUSH();

    nrf_delay_ms(2000);

    //NRF_LOG_ERROR( "GAS: " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.gas_resistance /1000.0));

    //NRF_LOG_FLUSH();

    //nrf_delay_ms(500);

     

    if (data.status & BME680_GASM_VALID_MSK)

    {

    NRF_LOG_ERROR( "GAS RESISTANCE : " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.gas_resistance /1000.0));

    NRF_LOG_FLUSH();

    nrf_delay_ms(500);

    }

    }

    }

    }

    Minhwan
    Community Moderator
    Community Moderator

    Hello VinayakMH, 

     

    You don't follow our producesure 100%. 

    Please check our example and follow it. And also please get return value, and we can find where you get some error and why. 

    Thanks, 

    Hi Mr.

    Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist