mauriciobarroso / bme68x_lib

ESP-IDF component for BME680 and BME688 4 in 1 enviromental sensors
MIT License
5 stars 0 forks source link

Having issue with i2c #1

Open MingyaoLiu opened 1 year ago

MingyaoLiu commented 1 year ago

Hello, I just got a BME688 and a ESP32C6 wroom board, so I'm using the latest master on esp-idf to have support for esp32c6. I tried to do something like this in app main:

    i2c_t i2cinfo;
    i2cinfo.i2c_addr = BME68X_I2C_ADDR_LOW;
    i2cinfo.i2c_conf = conf;
    i2cinfo.i2c_num = 0;
    memset(&bsec2_dev, 0, sizeof(bsec2_t));
    bsec2_init(&bsec2_dev, &i2cinfo, BME68X_I2C_INTF);
    if (bsec2_dev.status != 0)
    {
        printf("Something is wrong with bme688 %d \n", bsec2_dev.status);
    }
        bsec_sensor_configuration_t requested_virtual_sensors[7];
    uint8_t n_requested_virtual_sensors = BSEC_MAX_PHYSICAL_SENSOR;

    requested_virtual_sensors[0].sensor_id = BSEC_OUTPUT_IAQ;
    requested_virtual_sensors[0].sample_rate = BSEC_SAMPLE_RATE_CONT;
    requested_virtual_sensors[1].sensor_id = BSEC_OUTPUT_RAW_TEMPERATURE;
    requested_virtual_sensors[1].sample_rate = BSEC_SAMPLE_RATE_CONT;
    requested_virtual_sensors[2].sensor_id = BSEC_OUTPUT_RAW_PRESSURE;
    requested_virtual_sensors[2].sample_rate = BSEC_SAMPLE_RATE_CONT;
    requested_virtual_sensors[3].sensor_id = BSEC_OUTPUT_RAW_HUMIDITY;
    requested_virtual_sensors[3].sample_rate = BSEC_SAMPLE_RATE_CONT;
    requested_virtual_sensors[4].sensor_id = BSEC_OUTPUT_RAW_GAS;
    requested_virtual_sensors[4].sample_rate = BSEC_SAMPLE_RATE_CONT;
    requested_virtual_sensors[5].sensor_id = BSEC_OUTPUT_STABILIZATION_STATUS;
    requested_virtual_sensors[5].sample_rate = BSEC_SAMPLE_RATE_CONT;
    requested_virtual_sensors[6].sensor_id = BSEC_OUTPUT_RUN_IN_STATUS;
    requested_virtual_sensors[6].sample_rate = BSEC_SAMPLE_RATE_CONT;

    // Allocate a struct for the returned physical sensor settings
    bsec_sensor_configuration_t required_sensor_settings[BSEC_MAX_PHYSICAL_SENSOR];
    uint8_t n_required_sensor_settings = BSEC_MAX_PHYSICAL_SENSOR;

    // Call bsec_update_subscription() to enable/disable the requested virtual sensors
    bsec_library_return_t update_result = bsec_update_subscription(requested_virtual_sensors, n_requested_virtual_sensors, required_sensor_settings, &n_required_sensor_settings);
    printf("update sub result is %d \n", update_result);

    xTaskCreate(bme_task, "bme688", 4096, NULL, 5, NULL);

Then in the task I'm doing requestion data from bme688 with:

bsec2_run(&bsec2_dev);
        vTaskDelay(5000 / portTICK_PERIOD_MS);

        bsec_outputs_t *thisout = bsec2_get_outputs(&bsec2_dev);
        if (thisout)
            printf("bme688 Sensor id: %.2d %%\n", thisout->output->sensor_id);
        }

        bsec_data_t temp_data = bsec2_get_data(&bsec2_dev, BSEC_OUTPUT_RAW_TEMPERATURE);
        printf("temp data is %0.2f \n", temp_data.signal);

but im getting I2C error, so I looked into the I2C stuff and it seems to using the wrong i2c number and/or i2c address?

Sorry my code is super messy, I'm quite new to the esp-idf stuff and also bme688, so everything seems quite difficult for me, I wonder if you can point me in the right direction on how to properly use this? Thanks

mauriciobarroso commented 1 year ago

Hi @MingyaoLiu to initialize correctly the bsec2 and bme688 device you need to execute the next code:

#include "bsec2.h"

static i2c_t i2c;
static bsec2_t bsec2;

void app_main(void) {
  /* I2C parameters to configure the BME68x device */
  i2c.i2c_conf.mode = I2C_MODE_MASTER;
  i2c.i2c_conf.sda_io_num = GPIO_NUM_18;
  i2c.i2c_conf.scl_io_num = GPIO_NUM_19;
  i2c.i2c_conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
  i2c.i2c_conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
  i2c.i2c_conf.master.clk_speed = 400000;
  i2c.i2c_addr = BME68X_I2C_ADDR_LOW;
  i2c.i2c_num = I2C_NUM_0;

  /* Initialize the library and interfaces */
  if (!bsec2_init(&bsec2, (void *)&i2c, BME68X_I2C_INTF)) {
    bsec_check_status(&bsec2);
  }

 /* Check the version of BSEC */
  ESP_LOGI(TAG,"\bBSEC library version %d.%d.%d.%d", bsec2.version.major, bsec2.version.minor, bsec2.version.major_bugfix, bsec2.version.minor_bugfix);
}

The i2c_t variable must be global and you can change the I2C GPIOS pins.

Something important is that BSEC does not support ESP32-C6, for now they only have the precompiled library for ESP32, ESP32-S2, ESP32-S3 and ESP32-C3. You can check it in https://github.com/boschsensortec/Bosch-BSEC2-Library

MingyaoLiu commented 1 year ago

I see, I will test on esp32c3 for now, and see if I can get it working. Thanks for the help

MingyaoLiu commented 1 year ago

@mauriciobarroso may I ask a really stupid question? What's the difference between the bme68x_lib and bsec2 lib? I can see bsec2 lib is based on bme68x_lib, but do I have to use the bsec2 lib? Is it possible to just use bme68x_lib to get readings off sensor? Is IAQ something only possible with the bsec2 lib?

mauriciobarroso commented 1 year ago

Hi @MingyaoLiu, bme68x_lib is the libraty to interact with the physical sensor while bsec2 is the library to perform IA algoritms based on the bme688 raw data. If you only need the raw data from bme688 (temperature, humidity, pressure and gas resistance) you can use the bme688_lib, but if you need more relevant data (IAQ, VOC, CO2 and gas selectivity) you must use the bsec2 library. In my opinion, the only reason I would buy the bme688 is to use the BSEC2 library, in any other case I would buy any other cheaper sensor.