Files
2025-04-04 04:48:13 +02:00

8.2 KiB

Bosch BME680 Sensor

License: MIT Edited with VS Code Build with PlatformIO PlatformIO Registry ESP Component Registry

This ESP32 espressif IoT development framework (esp-idf) i2c peripheral driver was developed for the Bosch BME680 pressure, temperature, humidity and gas sensor. Information on features and functionality are documented and can be found in the bme680.h header file and in the documentation folder.

Repository

The component is hosted on github and is located here: https://github.com/K0I05/ESP32-S3_ESP-IDF_COMPONENTS/tree/main/components/peripherals/i2c/esp_bme680

General Usage

To get started, simply copy the component to your project's components folder and reference the bme680.h header file as an include. The component includes documentation for the peripheral such as the datasheet, application notes, and/or user manual where applicable.

components
└── esp_bme680
    ├── CMakeLists.txt
    ├── README.md
    ├── LICENSE
    ├── idf_component.yml
    ├── library.json
    ├── documentation
    │   └── datasheets, etc.
    ├── include
    │   └── bme680_version.h
    │   └── bme680.h
    └── bme680.c

Basic Example

Once a driver instance is instantiated the sensor is ready for usage as shown in the below example. This basic implementation of the driver utilizes default configuration settings and makes a measurement request from the sensor at user defined interval and prints the results.

#include <bme680.h>


static inline void print_registers(bme680_handle_t handle) {
    /* configuration registers */
    bme680_control_measurement_register_t ctrl_meas_reg;
    bme680_control_humidity_register_t    ctrl_humi_reg;
    bme680_config_register_t              config_reg;
    bme680_control_gas0_register_t        ctrl_gas0_reg;
    bme680_control_gas1_register_t        ctrl_gas1_reg;

    /* attempt to read control humidity register */
    bme680_get_control_humidity_register(handle, &ctrl_humi_reg);

    /* attempt to read control measurement register */
    bme680_get_control_measurement_register(handle, &ctrl_meas_reg);

    /* attempt to read configuration register */
    bme680_get_configuration_register(handle, &config_reg);

    /* attempt to read control gas 0 register */
    bme680_get_control_gas0_register(handle, &ctrl_gas0_reg);

    /* attempt to read control gas 1 register */
    bme680_get_control_gas1_register(handle, &ctrl_gas1_reg);

    ESP_LOGI(APP_TAG, "Variant Id          (0x%02x): %s", handle->variant_id,uint8_to_binary(handle->variant_id));
    ESP_LOGI(APP_TAG, "Configuration       (0x%02x): %s", config_reg.reg,    uint8_to_binary(config_reg.reg));
    ESP_LOGI(APP_TAG, "Control Measurement (0x%02x): %s", ctrl_meas_reg.reg, uint8_to_binary(ctrl_meas_reg.reg));
    ESP_LOGI(APP_TAG, "Control Humidity    (0x%02x): %s", ctrl_humi_reg.reg, uint8_to_binary(ctrl_humi_reg.reg));
    ESP_LOGI(APP_TAG, "Control Gas 0       (0x%02x): %s", ctrl_gas0_reg.reg, uint8_to_binary(ctrl_gas0_reg.reg));
    ESP_LOGI(APP_TAG, "Control Gas 1       (0x%02x): %s", ctrl_gas1_reg.reg, uint8_to_binary(ctrl_gas1_reg.reg));
}

void i2c0_bme680_task( void *pvParameters ) {
    // initialize the xLastWakeTime variable with the current time.
    TickType_t          last_wake_time  = xTaskGetTickCount ();
    //
    // initialize i2c device configuration
    bme680_config_t dev_cfg         = I2C_BME680_CONFIG_DEFAULT;
    bme680_handle_t dev_hdl;
    //
    // init device
    bme680_init(i2c0_bus_hdl, &dev_cfg, &dev_hdl);
    if (dev_hdl == NULL) {
        ESP_LOGE(APP_TAG, "bme680 handle init failed");
        assert(dev_hdl);
    }
    
    print_registers(dev_hdl);

    // task loop entry point
    for ( ;; ) {
        ESP_LOGI(APP_TAG, "######################## BME680 - START #########################");
        //
        // handle sensor

        bme680_data_t data;
        esp_err_t result = bme680_get_measurements(dev_hdl, &data);
        if(result != ESP_OK) {
            ESP_LOGE(APP_TAG, "bme680 device read failed (%s)", esp_err_to_name(result));
        } else {
            data.barometric_pressure = data.barometric_pressure / 100;
            ESP_LOGI(APP_TAG, "air temperature:     %.2f °C", data.air_temperature);
            ESP_LOGI(APP_TAG, "dewpoint temperature:%.2f °C", data.dewpoint_temperature);
            ESP_LOGI(APP_TAG, "relative humidity:   %.2f %%", data.relative_humidity);
            ESP_LOGI(APP_TAG, "barometric pressure: %.2f hPa", data.barometric_pressure);
            ESP_LOGI(APP_TAG, "gas resistance:      %.2f kOhms", data.gas_resistance/1000);
            ESP_LOGI(APP_TAG, "iaq score:           %u (%s)", data.iaq_score, bme680_iaq_air_quality_to_string(data.iaq_score));
        }
        //
        ESP_LOGI(APP_TAG, "######################## BME680 - END ###########################");
        //
        //
        // pause the task per defined wait period
        vTaskDelaySecUntil( &last_wake_time, I2C0_TASK_SAMPLING_RATE );
    }
    //
    // free resources
    bme680_delete( dev_hdl );
    vTaskDelete( NULL );
}

Copyright (c) 2024 Eric Gionet (gionet.c.eric@gmail.com)