diff --git a/Documentation/pycardium/bme680.rst b/Documentation/pycardium/bme680.rst new file mode 100644 index 0000000000000000000000000000000000000000..907d5956b8287646734fa4a3907b12ea09b9e6ac --- /dev/null +++ b/Documentation/pycardium/bme680.rst @@ -0,0 +1,9 @@ +``bme680`` - 4-in-1 Sensor +========================== + +.. py:function:: bme680.get_bme_data() + + Does a single measurement to get environmental data. + + :return: Tuple containing ``temperature`` (°C), ``humidity`` (% r.h.), + ``pressure`` (hPa) and ``gas resistance`` (Ohm). diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index a1921400cc5962dec6674449d03ead467dac1255..9778a259cd07d13ec382524ee5e5f1fefd2bf726 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -675,10 +675,10 @@ API(API_LEDS_CLEAR_ALL, void epic_leds_clear_all(uint8_t r, uint8_t g, uint8_t b struct bme_sensor_data { /*! Temperature in degree celsius */ float temperature; + /*! Humidity in % relative humidity */ + float humidity; /*! Pressure in Pascal */ float pressure; - /*! Humidity in % relative humidity x1000 */ - float humidity; /*! Gas resistance in Ohms */ float gas_resistance; }; diff --git a/epicardium/modules/bme680.c b/epicardium/modules/bme680.c index 1d0b01d7caa60de97f119e810e8316c344dc6727..bdce3aadcd3e611bc621daa351798a81049ce7bb 100644 --- a/epicardium/modules/bme680.c +++ b/epicardium/modules/bme680.c @@ -8,10 +8,8 @@ #include "epicardium.h" -#define HEATR_DUR 2000 -#define N_MEAS 6 -#define LOW_TEMP 150 -#define HIGH_TEMP 350 +#define HEATR_TEMP 320 +#define HEATR_DUR 150 static bool initialized; static struct bme680_dev bme; @@ -38,14 +36,17 @@ int epic_bme_get_data(struct bme_sensor_data *data) uint16_t settings_sel; if (__builtin_expect(!initialized, 0)) { - /* - bma.intf_ptr = NULL; - bma.delay_ms = card10_bosch_delay; - bma.dev_id = BMA400_I2C_ADDRESS_SDO_LOW; - bma.read = card10_bosch_i2c_read_ex; - bma.write = card10_bosch_i2c_write_ex; - bma.intf = BMA400_I2C_INTF; - */ + + bme.dev_id = BME680_I2C_ADDR_PRIMARY; + bme.intf = BME680_I2C_INTF; + bme.read = card10_bosch_i2c_read; + bme.write = card10_bosch_i2c_write; + bme.delay_ms = card10_bosch_delay; + /* 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. + */ + bme.amb_temp = 25; + result = bme680_init(&bme); if (result != BME680_OK) { printf("bme680_init error: %d\n", result); @@ -56,65 +57,58 @@ int epic_bme_get_data(struct bme_sensor_data *data) /* Must be set before writing the sensor configuration */ bme.power_mode = BME680_FORCED_MODE; - /* Set the temperature, pressure and humidity & filter settings */ - bme.tph_sett.os_hum = BME680_OS_1X; - bme.tph_sett.os_pres = BME680_OS_16X; - bme.tph_sett.os_temp = BME680_OS_2X; + /* Set the temperature, pressure and humidity settings */ + bme.tph_sett.os_hum = BME680_OS_2X; + bme.tph_sett.os_pres = BME680_OS_4X; + bme.tph_sett.os_temp = BME680_OS_8X; + bme.tph_sett.filter = BME680_FILTER_SIZE_3; /* Set the remaining gas sensor settings and link the heating profile */ - bme.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS; - bme.gas_sett.heatr_dur = HEATR_DUR; + bme.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS; + /* Create a ramp heat waveform in 3 steps */ + bme.gas_sett.heatr_temp = HEATR_TEMP; /* degree Celsius */ + bme.gas_sett.heatr_dur = HEATR_DUR; /* milliseconds */ - settings_sel = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_GAS_SENSOR_SEL; + /* Set the required sensor settings needed */ + settings_sel = BME680_OST_SEL | BME680_OSP_SEL | + BME680_OSH_SEL | BME680_FILTER_SEL | + BME680_GAS_SENSOR_SEL; + + result = bme680_set_sensor_settings(settings_sel, &bme); + if (result != BME680_OK) { + printf("bme680_set_sensor_settings error: %d\n", result); + return -convert_error(result); + } initialized = true; } - struct bme680_field_data raw_data[N_MEAS]; + struct bme680_field_data raw_data; uint16_t profile_dur = 0; bme680_get_profile_dur(&profile_dur, &bme); - uint8_t i = 0; - while ((result == BME680_OK) && (i < N_MEAS)) { - if (result == BME680_OK) { + if (result == BME680_OK) { - if (i % 2 == 0) - bme.gas_sett.heatr_temp = HIGH_TEMP; /* Higher temperature */ - else - bme.gas_sett.heatr_temp = LOW_TEMP; /* Lower temperature */ - - result = bme680_set_sensor_settings(settings_sel, &bme); - if (result != BME680_OK) { - printf("bme680_set_sensor_settings error: %d\n", result); - return -convert_error(result); - } - - if (result == BME680_OK) { - - result = bme680_set_sensor_mode(&bme); /* Trigger a measurement */ - if (result != BME680_OK) { - printf("bme680_set_sensor_mode error: %d\n", result); - return -convert_error(result); - } + result = bme680_set_sensor_mode(&bme); /* Trigger a measurement */ + if (result != BME680_OK) { + printf("bme680_set_sensor_mode error: %d\n", result); + return -convert_error(result); + } - bme.delay_ms(profile_dur); /* Wait for the measurement to complete */ + bme.delay_ms(profile_dur); /* Wait for the measurement to complete */ - result = bme680_get_sensor_data(&raw_data[i], &bme); - if (result != BME680_OK) { - printf("bme680_get_sensor_data error: %d\n", result); - return -convert_error(result); - } - } + result = bme680_get_sensor_data(&raw_data, &bme); + if (result != BME680_OK) { + printf("bme680_get_sensor_data error: %d\n", result); + return -convert_error(result); } - - i++; } - data->temperature = raw_data[0].temperature; - data->humidity = raw_data[0].humidity; - data->pressure = raw_data[0].pressure; - data->gas_resistance = raw_data[0].gas_resistance; + data->temperature = raw_data.temperature / 100.0l; + data->humidity = raw_data.humidity / 1000.0l; + data->pressure = raw_data.pressure / 100.0l; + data->gas_resistance = raw_data.gas_resistance; return 0; } diff --git a/pycardium/modules/bme680.c b/pycardium/modules/bme680.c index 3c781f353c3a78577b700eccbfb32eccdba5b55f..e1d4d9f0586c45717a35e1b102acf8c57b84b174 100644 --- a/pycardium/modules/bme680.c +++ b/pycardium/modules/bme680.c @@ -4,7 +4,7 @@ #include "epicardium.h" -static mp_obj_t mp_bme_get_data() +static mp_obj_t mp_get_bme_data() { struct bme_sensor_data data; int ret = epic_bme_get_data(&data); @@ -17,10 +17,11 @@ static mp_obj_t mp_bme_get_data() mp_obj_new_float(data.temperature), mp_obj_new_float(data.humidity), mp_obj_new_float(data.pressure), + mp_obj_new_float(data.gas_resistance) }; - return mp_obj_new_tuple(3, values_list); + return mp_obj_new_tuple(4, values_list); } -static MP_DEFINE_CONST_FUN_OBJ_0(bme_get_bme_data_obj, mp_bme_get_data); +static MP_DEFINE_CONST_FUN_OBJ_0(bme_get_bme_data_obj, mp_get_bme_data); static const mp_rom_map_elem_t bme_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bme680) }, @@ -33,5 +34,6 @@ const mp_obj_module_t bme_module = { .globals = (mp_obj_dict_t *)&bme_module_globals, }; + /* Register the module to make it available in Python */ -MP_REGISTER_MODULE(MP_QSTR_bme680, bme_module, MODULE_BME_ENABLED); \ No newline at end of file +MP_REGISTER_MODULE(MP_QSTR_bme680, bme_module, MODULE_BME680_ENABLED); \ No newline at end of file