Bosch Sensortec Community

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

    issue when reading BME680

    issue when reading BME680

    arnaud
    Member

    Hello everyone,

    I am trying to use a BME680 with this library : https://github.com/BoschSensortec/BME680_driver

    I don't want to use an other one because this library doesn't use arduino.h and I would like to use this code for an other MCU.

    So the code detect if a sensor is plug. But when I am reading I have the value 0 so everything should be OK but the measure don't move they are fix.

    here is my code hope someone could help me : 


    #include <Wire.h>
    #include"bme680.h"
    //#define BME680_DEBUG
    struct bme680_dev gas_sensor;
    int8_t user_i2c_read (uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len){
      int result=0;
     
      Wire.beginTransmission(dev_id);
      Wire.write(reg_addr);   // Accès au registre
      Wire.endTransmission(false);
      Wire.requestFrom(dev_id, len);
      if (len<=Wire.available()){
        for(int i=0;i<len;i++){
            data[i] = Wire.read();       
        }
      }else{
        result=5;
      }
      return result;
    }
    /**
     * Write the content into the register targeted register for a specified device
     *
     * Parameter :
     *    dev_id : the I2C adress of the device
     *    reg_addr : the adresse of the targeted register
     *    *data : pointer to the data to write
     *    len : number in byte of the data
     *
     * Return:  Error code
     */
    int8_t user_i2c_write (uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len){
      int result=0;
      Wire.beginTransmission(dev_id);
      Wire.write(reg_addr);
      for(int i=0;i<len;i++){
        Wire.write(data[i]);
      }
      Wire.endTransmission();
      return result;
    }
    /**
     * function to wait the time
     *
     * Parameter :
     *    period : time that will be waited in millisecond
     */
    void user_delay_ms (uint32_t period){
      delay(period);
    }
     

    int8_t rslt = BME680_OK;
    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 = 320; /* degree Celsius */
        gas_sensor.gas_sett.heatr_dur = 150; /* 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);
    }
    void setup() {
     
      Serial.begin(9600);
     
       
        init_bme();
        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;
        /* amb_temp can be set to 25 prior to configuring the gas sensor
         * or by performing a few temperature readings without operating the gas sensor.
         */
        gas_sensor.amb_temp = 25;
       
       
       
        Wire.begin();
        rslt = bme680_init(&gas_sensor);
        Serial.println(rslt);
       

    }
    void loop() {
      // put your main code here, to run repeatedly:
      uint16_t meas_period = 10000 ;
      bme680_get_profile_dur(&meas_period, &gas_sensor);
      struct bme680_field_data data;
       
        while(1){
      delay(meas_period);
      rslt = bme680_get_sensor_data(&data, &gas_sensor);
      Serial.println(rslt);
      Serial.print ("T : ");
      Serial.print(data.temperature / 100.0);
      Serial.print ("P : ");
      Serial.print(data.pressure / 100.0);
      Serial.print ("H :");
      Serial.println(data.humidity / 1000.0);
      /* Avoid using measurements from an unstable heating setup */
      if (data.status & BME680_GASM_VALID_MSK)
      {
        Serial.print("G: ");
        Serial.print(data.gas_resistance);
        Serial.println("Kohms");
      }
     
      /* Trigger the next measurement if you would like to read data out continuously */
      if (gas_sensor.power_mode == BME680_FORCED_MODE) {
        rslt = bme680_set_sensor_mode(&gas_sensor);
      }
     
        }
    }
    2 REPLIES 2

    Vincent
    Community Moderator
    Community Moderator

    You should put init_bme() after bme680_init.

     

    thank you very much ! 

    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