mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	BME280 SPI (#5538)
* bme spi finally * linter * CO * tidy * lint * tidy [2] * tidy[-1] * final solution * Update test1.yaml remove failed test * Update test1.1.yaml add test to another file with free GPIO5 pin * fix spi read bytes * fix tests * rename bme280 to bme280_i2c
This commit is contained in:
		| @@ -54,6 +54,8 @@ esphome/components/bl0940/* @tobias- | |||||||
| esphome/components/bl0942/* @dbuezas | esphome/components/bl0942/* @dbuezas | ||||||
| esphome/components/ble_client/* @buxtronix @clydebarrow | esphome/components/ble_client/* @buxtronix @clydebarrow | ||||||
| esphome/components/bluetooth_proxy/* @jesserockz | esphome/components/bluetooth_proxy/* @jesserockz | ||||||
|  | esphome/components/bme280_base/* @esphome/core | ||||||
|  | esphome/components/bme280_spi/* @apbodrov | ||||||
| esphome/components/bme680_bsec/* @trvrnrth | esphome/components/bme680_bsec/* @trvrnrth | ||||||
| esphome/components/bmi160/* @flaviut | esphome/components/bmi160/* @flaviut | ||||||
| esphome/components/bmp3xx/* @martgras | esphome/components/bmp3xx/* @martgras | ||||||
|   | |||||||
| @@ -1,116 +0,0 @@ | |||||||
| import esphome.codegen as cg |  | ||||||
| import esphome.config_validation as cv |  | ||||||
| from esphome.components import i2c, sensor |  | ||||||
| from esphome.const import ( |  | ||||||
|     CONF_HUMIDITY, |  | ||||||
|     CONF_ID, |  | ||||||
|     CONF_IIR_FILTER, |  | ||||||
|     CONF_OVERSAMPLING, |  | ||||||
|     CONF_PRESSURE, |  | ||||||
|     CONF_TEMPERATURE, |  | ||||||
|     DEVICE_CLASS_HUMIDITY, |  | ||||||
|     DEVICE_CLASS_PRESSURE, |  | ||||||
|     DEVICE_CLASS_TEMPERATURE, |  | ||||||
|     STATE_CLASS_MEASUREMENT, |  | ||||||
|     UNIT_CELSIUS, |  | ||||||
|     UNIT_HECTOPASCAL, |  | ||||||
|     UNIT_PERCENT, |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| DEPENDENCIES = ["i2c"] |  | ||||||
|  |  | ||||||
| bme280_ns = cg.esphome_ns.namespace("bme280") |  | ||||||
| BME280Oversampling = bme280_ns.enum("BME280Oversampling") |  | ||||||
| OVERSAMPLING_OPTIONS = { |  | ||||||
|     "NONE": BME280Oversampling.BME280_OVERSAMPLING_NONE, |  | ||||||
|     "1X": BME280Oversampling.BME280_OVERSAMPLING_1X, |  | ||||||
|     "2X": BME280Oversampling.BME280_OVERSAMPLING_2X, |  | ||||||
|     "4X": BME280Oversampling.BME280_OVERSAMPLING_4X, |  | ||||||
|     "8X": BME280Oversampling.BME280_OVERSAMPLING_8X, |  | ||||||
|     "16X": BME280Oversampling.BME280_OVERSAMPLING_16X, |  | ||||||
| } |  | ||||||
|  |  | ||||||
| BME280IIRFilter = bme280_ns.enum("BME280IIRFilter") |  | ||||||
| IIR_FILTER_OPTIONS = { |  | ||||||
|     "OFF": BME280IIRFilter.BME280_IIR_FILTER_OFF, |  | ||||||
|     "2X": BME280IIRFilter.BME280_IIR_FILTER_2X, |  | ||||||
|     "4X": BME280IIRFilter.BME280_IIR_FILTER_4X, |  | ||||||
|     "8X": BME280IIRFilter.BME280_IIR_FILTER_8X, |  | ||||||
|     "16X": BME280IIRFilter.BME280_IIR_FILTER_16X, |  | ||||||
| } |  | ||||||
|  |  | ||||||
| BME280Component = bme280_ns.class_( |  | ||||||
|     "BME280Component", cg.PollingComponent, i2c.I2CDevice |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| CONFIG_SCHEMA = ( |  | ||||||
|     cv.Schema( |  | ||||||
|         { |  | ||||||
|             cv.GenerateID(): cv.declare_id(BME280Component), |  | ||||||
|             cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( |  | ||||||
|                 unit_of_measurement=UNIT_CELSIUS, |  | ||||||
|                 accuracy_decimals=1, |  | ||||||
|                 device_class=DEVICE_CLASS_TEMPERATURE, |  | ||||||
|                 state_class=STATE_CLASS_MEASUREMENT, |  | ||||||
|             ).extend( |  | ||||||
|                 { |  | ||||||
|                     cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( |  | ||||||
|                         OVERSAMPLING_OPTIONS, upper=True |  | ||||||
|                     ), |  | ||||||
|                 } |  | ||||||
|             ), |  | ||||||
|             cv.Optional(CONF_PRESSURE): sensor.sensor_schema( |  | ||||||
|                 unit_of_measurement=UNIT_HECTOPASCAL, |  | ||||||
|                 accuracy_decimals=1, |  | ||||||
|                 device_class=DEVICE_CLASS_PRESSURE, |  | ||||||
|                 state_class=STATE_CLASS_MEASUREMENT, |  | ||||||
|             ).extend( |  | ||||||
|                 { |  | ||||||
|                     cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( |  | ||||||
|                         OVERSAMPLING_OPTIONS, upper=True |  | ||||||
|                     ), |  | ||||||
|                 } |  | ||||||
|             ), |  | ||||||
|             cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( |  | ||||||
|                 unit_of_measurement=UNIT_PERCENT, |  | ||||||
|                 accuracy_decimals=1, |  | ||||||
|                 device_class=DEVICE_CLASS_HUMIDITY, |  | ||||||
|                 state_class=STATE_CLASS_MEASUREMENT, |  | ||||||
|             ).extend( |  | ||||||
|                 { |  | ||||||
|                     cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( |  | ||||||
|                         OVERSAMPLING_OPTIONS, upper=True |  | ||||||
|                     ), |  | ||||||
|                 } |  | ||||||
|             ), |  | ||||||
|             cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum( |  | ||||||
|                 IIR_FILTER_OPTIONS, upper=True |  | ||||||
|             ), |  | ||||||
|         } |  | ||||||
|     ) |  | ||||||
|     .extend(cv.polling_component_schema("60s")) |  | ||||||
|     .extend(i2c.i2c_device_schema(0x77)) |  | ||||||
| ) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| async def to_code(config): |  | ||||||
|     var = cg.new_Pvariable(config[CONF_ID]) |  | ||||||
|     await cg.register_component(var, config) |  | ||||||
|     await i2c.register_i2c_device(var, config) |  | ||||||
|  |  | ||||||
|     if temperature_config := config.get(CONF_TEMPERATURE): |  | ||||||
|         sens = await sensor.new_sensor(temperature_config) |  | ||||||
|         cg.add(var.set_temperature_sensor(sens)) |  | ||||||
|         cg.add(var.set_temperature_oversampling(temperature_config[CONF_OVERSAMPLING])) |  | ||||||
|  |  | ||||||
|     if pressure_config := config.get(CONF_PRESSURE): |  | ||||||
|         sens = await sensor.new_sensor(pressure_config) |  | ||||||
|         cg.add(var.set_pressure_sensor(sens)) |  | ||||||
|         cg.add(var.set_pressure_oversampling(pressure_config[CONF_OVERSAMPLING])) |  | ||||||
|  |  | ||||||
|     if humidity_config := config.get(CONF_HUMIDITY): |  | ||||||
|         sens = await sensor.new_sensor(humidity_config) |  | ||||||
|         cg.add(var.set_humidity_sensor(sens)) |  | ||||||
|         cg.add(var.set_humidity_oversampling(humidity_config[CONF_OVERSAMPLING])) |  | ||||||
|  |  | ||||||
|     cg.add(var.set_iir_filter(config[CONF_IIR_FILTER])) |  | ||||||
							
								
								
									
										1
									
								
								esphome/components/bme280_base/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								esphome/components/bme280_base/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | CODEOWNERS = ["@esphome/core"] | ||||||
| @@ -1,9 +1,14 @@ | |||||||
| #include "bme280.h" | #include <cmath> | ||||||
|  | #include <cstdint> | ||||||
|  | 
 | ||||||
|  | #include "bme280_base.h" | ||||||
| #include "esphome/core/hal.h" | #include "esphome/core/hal.h" | ||||||
| #include "esphome/core/log.h" | #include "esphome/core/log.h" | ||||||
|  | #include <esphome/components/sensor/sensor.h> | ||||||
|  | #include <esphome/core/component.h> | ||||||
| 
 | 
 | ||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace bme280 { | namespace bme280_base { | ||||||
| 
 | 
 | ||||||
| static const char *const TAG = "bme280.sensor"; | static const char *const TAG = "bme280.sensor"; | ||||||
| 
 | 
 | ||||||
| @@ -46,7 +51,24 @@ static const uint8_t BME280_STATUS_IM_UPDATE = 0b01; | |||||||
| 
 | 
 | ||||||
| inline uint16_t combine_bytes(uint8_t msb, uint8_t lsb) { return ((msb & 0xFF) << 8) | (lsb & 0xFF); } | inline uint16_t combine_bytes(uint8_t msb, uint8_t lsb) { return ((msb & 0xFF) << 8) | (lsb & 0xFF); } | ||||||
| 
 | 
 | ||||||
| static const char *oversampling_to_str(BME280Oversampling oversampling) { | const char *iir_filter_to_str(BME280IIRFilter filter) {  // NOLINT
 | ||||||
|  |   switch (filter) { | ||||||
|  |     case BME280_IIR_FILTER_OFF: | ||||||
|  |       return "OFF"; | ||||||
|  |     case BME280_IIR_FILTER_2X: | ||||||
|  |       return "2x"; | ||||||
|  |     case BME280_IIR_FILTER_4X: | ||||||
|  |       return "4x"; | ||||||
|  |     case BME280_IIR_FILTER_8X: | ||||||
|  |       return "8x"; | ||||||
|  |     case BME280_IIR_FILTER_16X: | ||||||
|  |       return "16x"; | ||||||
|  |     default: | ||||||
|  |       return "UNKNOWN"; | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | const char *oversampling_to_str(BME280Oversampling oversampling) {  // NOLINT
 | ||||||
|   switch (oversampling) { |   switch (oversampling) { | ||||||
|     case BME280_OVERSAMPLING_NONE: |     case BME280_OVERSAMPLING_NONE: | ||||||
|       return "None"; |       return "None"; | ||||||
| @@ -65,23 +87,6 @@ static const char *oversampling_to_str(BME280Oversampling oversampling) { | |||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static const char *iir_filter_to_str(BME280IIRFilter filter) { |  | ||||||
|   switch (filter) { |  | ||||||
|     case BME280_IIR_FILTER_OFF: |  | ||||||
|       return "OFF"; |  | ||||||
|     case BME280_IIR_FILTER_2X: |  | ||||||
|       return "2x"; |  | ||||||
|     case BME280_IIR_FILTER_4X: |  | ||||||
|       return "4x"; |  | ||||||
|     case BME280_IIR_FILTER_8X: |  | ||||||
|       return "8x"; |  | ||||||
|     case BME280_IIR_FILTER_16X: |  | ||||||
|       return "16x"; |  | ||||||
|     default: |  | ||||||
|       return "UNKNOWN"; |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| void BME280Component::setup() { | void BME280Component::setup() { | ||||||
|   ESP_LOGCONFIG(TAG, "Setting up BME280..."); |   ESP_LOGCONFIG(TAG, "Setting up BME280..."); | ||||||
|   uint8_t chip_id = 0; |   uint8_t chip_id = 0; | ||||||
| @@ -112,7 +117,7 @@ void BME280Component::setup() { | |||||||
|   // Wait until the NVM data has finished loading.
 |   // Wait until the NVM data has finished loading.
 | ||||||
|   uint8_t status; |   uint8_t status; | ||||||
|   uint8_t retry = 5; |   uint8_t retry = 5; | ||||||
|   do { |   do {  // NOLINT
 | ||||||
|     delay(2); |     delay(2); | ||||||
|     if (!this->read_byte(BME280_REGISTER_STATUS, &status)) { |     if (!this->read_byte(BME280_REGISTER_STATUS, &status)) { | ||||||
|       ESP_LOGW(TAG, "Error reading status register."); |       ESP_LOGW(TAG, "Error reading status register."); | ||||||
| @@ -175,7 +180,6 @@ void BME280Component::setup() { | |||||||
| } | } | ||||||
| void BME280Component::dump_config() { | void BME280Component::dump_config() { | ||||||
|   ESP_LOGCONFIG(TAG, "BME280:"); |   ESP_LOGCONFIG(TAG, "BME280:"); | ||||||
|   LOG_I2C_DEVICE(this); |  | ||||||
|   switch (this->error_code_) { |   switch (this->error_code_) { | ||||||
|     case COMMUNICATION_FAILED: |     case COMMUNICATION_FAILED: | ||||||
|       ESP_LOGE(TAG, "Communication with BME280 failed!"); |       ESP_LOGE(TAG, "Communication with BME280 failed!"); | ||||||
| @@ -226,14 +230,14 @@ void BME280Component::update() { | |||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     int32_t t_fine = 0; |     int32_t t_fine = 0; | ||||||
|     float temperature = this->read_temperature_(data, &t_fine); |     float const temperature = this->read_temperature_(data, &t_fine); | ||||||
|     if (std::isnan(temperature)) { |     if (std::isnan(temperature)) { | ||||||
|       ESP_LOGW(TAG, "Invalid temperature, cannot read pressure & humidity values."); |       ESP_LOGW(TAG, "Invalid temperature, cannot read pressure & humidity values."); | ||||||
|       this->status_set_warning(); |       this->status_set_warning(); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     float pressure = this->read_pressure_(data, t_fine); |     float const pressure = this->read_pressure_(data, t_fine); | ||||||
|     float humidity = this->read_humidity_(data, t_fine); |     float const humidity = this->read_humidity_(data, t_fine); | ||||||
| 
 | 
 | ||||||
|     ESP_LOGV(TAG, "Got temperature=%.1f°C pressure=%.1fhPa humidity=%.1f%%", temperature, pressure, humidity); |     ESP_LOGV(TAG, "Got temperature=%.1f°C pressure=%.1fhPa humidity=%.1f%%", temperature, pressure, humidity); | ||||||
|     if (this->temperature_sensor_ != nullptr) |     if (this->temperature_sensor_ != nullptr) | ||||||
| @@ -257,11 +261,11 @@ float BME280Component::read_temperature_(const uint8_t *data, int32_t *t_fine) { | |||||||
|   const int32_t t2 = this->calibration_.t2; |   const int32_t t2 = this->calibration_.t2; | ||||||
|   const int32_t t3 = this->calibration_.t3; |   const int32_t t3 = this->calibration_.t3; | ||||||
| 
 | 
 | ||||||
|   int32_t var1 = (((adc >> 3) - (t1 << 1)) * t2) >> 11; |   int32_t const var1 = (((adc >> 3) - (t1 << 1)) * t2) >> 11; | ||||||
|   int32_t var2 = (((((adc >> 4) - t1) * ((adc >> 4) - t1)) >> 12) * t3) >> 14; |   int32_t const var2 = (((((adc >> 4) - t1) * ((adc >> 4) - t1)) >> 12) * t3) >> 14; | ||||||
|   *t_fine = var1 + var2; |   *t_fine = var1 + var2; | ||||||
| 
 | 
 | ||||||
|   float temperature = (*t_fine * 5 + 128) >> 8; |   float const temperature = (*t_fine * 5 + 128) >> 8; | ||||||
|   return temperature / 100.0f; |   return temperature / 100.0f; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @@ -303,11 +307,11 @@ float BME280Component::read_pressure_(const uint8_t *data, int32_t t_fine) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| float BME280Component::read_humidity_(const uint8_t *data, int32_t t_fine) { | float BME280Component::read_humidity_(const uint8_t *data, int32_t t_fine) { | ||||||
|   uint16_t raw_adc = ((data[6] & 0xFF) << 8) | (data[7] & 0xFF); |   uint16_t const raw_adc = ((data[6] & 0xFF) << 8) | (data[7] & 0xFF); | ||||||
|   if (raw_adc == 0x8000) |   if (raw_adc == 0x8000) | ||||||
|     return NAN; |     return NAN; | ||||||
| 
 | 
 | ||||||
|   int32_t adc = raw_adc; |   int32_t const adc = raw_adc; | ||||||
| 
 | 
 | ||||||
|   const int32_t h1 = this->calibration_.h1; |   const int32_t h1 = this->calibration_.h1; | ||||||
|   const int32_t h2 = this->calibration_.h2; |   const int32_t h2 = this->calibration_.h2; | ||||||
| @@ -325,7 +329,7 @@ float BME280Component::read_humidity_(const uint8_t *data, int32_t t_fine) { | |||||||
| 
 | 
 | ||||||
|   v_x1_u32r = v_x1_u32r < 0 ? 0 : v_x1_u32r; |   v_x1_u32r = v_x1_u32r < 0 ? 0 : v_x1_u32r; | ||||||
|   v_x1_u32r = v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r; |   v_x1_u32r = v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r; | ||||||
|   float h = v_x1_u32r >> 12; |   float const h = v_x1_u32r >> 12; | ||||||
| 
 | 
 | ||||||
|   return h / 1024.0f; |   return h / 1024.0f; | ||||||
| } | } | ||||||
| @@ -351,5 +355,5 @@ uint16_t BME280Component::read_u16_le_(uint8_t a_register) { | |||||||
| } | } | ||||||
| int16_t BME280Component::read_s16_le_(uint8_t a_register) { return this->read_u16_le_(a_register); } | int16_t BME280Component::read_s16_le_(uint8_t a_register) { return this->read_u16_le_(a_register); } | ||||||
| 
 | 
 | ||||||
| }  // namespace bme280
 | }  // namespace bme280_base
 | ||||||
| }  // namespace esphome
 | }  // namespace esphome
 | ||||||
| @@ -2,10 +2,9 @@ | |||||||
| 
 | 
 | ||||||
| #include "esphome/core/component.h" | #include "esphome/core/component.h" | ||||||
| #include "esphome/components/sensor/sensor.h" | #include "esphome/components/sensor/sensor.h" | ||||||
| #include "esphome/components/i2c/i2c.h" |  | ||||||
| 
 | 
 | ||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace bme280 { | namespace bme280_base { | ||||||
| 
 | 
 | ||||||
| /// Internal struct storing the calibration values of an BME280.
 | /// Internal struct storing the calibration values of an BME280.
 | ||||||
| struct BME280CalibrationData { | struct BME280CalibrationData { | ||||||
| @@ -57,8 +56,8 @@ enum BME280IIRFilter { | |||||||
|   BME280_IIR_FILTER_16X = 0b100, |   BME280_IIR_FILTER_16X = 0b100, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| /// This class implements support for the BME280 Temperature+Pressure+Humidity i2c sensor.
 | /// This class implements support for the BME280 Temperature+Pressure+Humidity sensor.
 | ||||||
| class BME280Component : public PollingComponent, public i2c::I2CDevice { | class BME280Component : public PollingComponent { | ||||||
|  public: |  public: | ||||||
|   void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; } |   void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; } | ||||||
|   void set_pressure_sensor(sensor::Sensor *pressure_sensor) { pressure_sensor_ = pressure_sensor; } |   void set_pressure_sensor(sensor::Sensor *pressure_sensor) { pressure_sensor_ = pressure_sensor; } | ||||||
| @@ -91,6 +90,11 @@ class BME280Component : public PollingComponent, public i2c::I2CDevice { | |||||||
|   uint16_t read_u16_le_(uint8_t a_register); |   uint16_t read_u16_le_(uint8_t a_register); | ||||||
|   int16_t read_s16_le_(uint8_t a_register); |   int16_t read_s16_le_(uint8_t a_register); | ||||||
| 
 | 
 | ||||||
|  |   virtual bool read_byte(uint8_t a_register, uint8_t *data) = 0; | ||||||
|  |   virtual bool write_byte(uint8_t a_register, uint8_t data) = 0; | ||||||
|  |   virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) = 0; | ||||||
|  |   virtual bool read_byte_16(uint8_t a_register, uint16_t *data) = 0; | ||||||
|  | 
 | ||||||
|   BME280CalibrationData calibration_; |   BME280CalibrationData calibration_; | ||||||
|   BME280Oversampling temperature_oversampling_{BME280_OVERSAMPLING_16X}; |   BME280Oversampling temperature_oversampling_{BME280_OVERSAMPLING_16X}; | ||||||
|   BME280Oversampling pressure_oversampling_{BME280_OVERSAMPLING_16X}; |   BME280Oversampling pressure_oversampling_{BME280_OVERSAMPLING_16X}; | ||||||
| @@ -106,5 +110,5 @@ class BME280Component : public PollingComponent, public i2c::I2CDevice { | |||||||
|   } error_code_{NONE}; |   } error_code_{NONE}; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| }  // namespace bme280
 | }  // namespace bme280_base
 | ||||||
| }  // namespace esphome
 | }  // namespace esphome
 | ||||||
							
								
								
									
										106
									
								
								esphome/components/bme280_base/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										106
									
								
								esphome/components/bme280_base/sensor.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,106 @@ | |||||||
|  | import esphome.codegen as cg | ||||||
|  | import esphome.config_validation as cv | ||||||
|  | from esphome.components import sensor | ||||||
|  | from esphome.const import ( | ||||||
|  |     CONF_HUMIDITY, | ||||||
|  |     CONF_ID, | ||||||
|  |     CONF_IIR_FILTER, | ||||||
|  |     CONF_OVERSAMPLING, | ||||||
|  |     CONF_PRESSURE, | ||||||
|  |     CONF_TEMPERATURE, | ||||||
|  |     DEVICE_CLASS_HUMIDITY, | ||||||
|  |     DEVICE_CLASS_PRESSURE, | ||||||
|  |     DEVICE_CLASS_TEMPERATURE, | ||||||
|  |     STATE_CLASS_MEASUREMENT, | ||||||
|  |     UNIT_CELSIUS, | ||||||
|  |     UNIT_HECTOPASCAL, | ||||||
|  |     UNIT_PERCENT, | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | bme280_ns = cg.esphome_ns.namespace("bme280_base") | ||||||
|  | BME280Oversampling = bme280_ns.enum("BME280Oversampling") | ||||||
|  | OVERSAMPLING_OPTIONS = { | ||||||
|  |     "NONE": BME280Oversampling.BME280_OVERSAMPLING_NONE, | ||||||
|  |     "1X": BME280Oversampling.BME280_OVERSAMPLING_1X, | ||||||
|  |     "2X": BME280Oversampling.BME280_OVERSAMPLING_2X, | ||||||
|  |     "4X": BME280Oversampling.BME280_OVERSAMPLING_4X, | ||||||
|  |     "8X": BME280Oversampling.BME280_OVERSAMPLING_8X, | ||||||
|  |     "16X": BME280Oversampling.BME280_OVERSAMPLING_16X, | ||||||
|  | } | ||||||
|  |  | ||||||
|  | BME280IIRFilter = bme280_ns.enum("BME280IIRFilter") | ||||||
|  | IIR_FILTER_OPTIONS = { | ||||||
|  |     "OFF": BME280IIRFilter.BME280_IIR_FILTER_OFF, | ||||||
|  |     "2X": BME280IIRFilter.BME280_IIR_FILTER_2X, | ||||||
|  |     "4X": BME280IIRFilter.BME280_IIR_FILTER_4X, | ||||||
|  |     "8X": BME280IIRFilter.BME280_IIR_FILTER_8X, | ||||||
|  |     "16X": BME280IIRFilter.BME280_IIR_FILTER_16X, | ||||||
|  | } | ||||||
|  |  | ||||||
|  | CONFIG_SCHEMA_BASE = cv.Schema( | ||||||
|  |     { | ||||||
|  |         cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( | ||||||
|  |             unit_of_measurement=UNIT_CELSIUS, | ||||||
|  |             accuracy_decimals=1, | ||||||
|  |             device_class=DEVICE_CLASS_TEMPERATURE, | ||||||
|  |             state_class=STATE_CLASS_MEASUREMENT, | ||||||
|  |         ).extend( | ||||||
|  |             { | ||||||
|  |                 cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( | ||||||
|  |                     OVERSAMPLING_OPTIONS, upper=True | ||||||
|  |                 ), | ||||||
|  |             } | ||||||
|  |         ), | ||||||
|  |         cv.Optional(CONF_PRESSURE): sensor.sensor_schema( | ||||||
|  |             unit_of_measurement=UNIT_HECTOPASCAL, | ||||||
|  |             accuracy_decimals=1, | ||||||
|  |             device_class=DEVICE_CLASS_PRESSURE, | ||||||
|  |             state_class=STATE_CLASS_MEASUREMENT, | ||||||
|  |         ).extend( | ||||||
|  |             { | ||||||
|  |                 cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( | ||||||
|  |                     OVERSAMPLING_OPTIONS, upper=True | ||||||
|  |                 ), | ||||||
|  |             } | ||||||
|  |         ), | ||||||
|  |         cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( | ||||||
|  |             unit_of_measurement=UNIT_PERCENT, | ||||||
|  |             accuracy_decimals=1, | ||||||
|  |             device_class=DEVICE_CLASS_HUMIDITY, | ||||||
|  |             state_class=STATE_CLASS_MEASUREMENT, | ||||||
|  |         ).extend( | ||||||
|  |             { | ||||||
|  |                 cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( | ||||||
|  |                     OVERSAMPLING_OPTIONS, upper=True | ||||||
|  |                 ), | ||||||
|  |             } | ||||||
|  |         ), | ||||||
|  |         cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum( | ||||||
|  |             IIR_FILTER_OPTIONS, upper=True | ||||||
|  |         ), | ||||||
|  |     } | ||||||
|  | ).extend(cv.polling_component_schema("60s")) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | async def to_code(config, func=None): | ||||||
|  |     var = cg.new_Pvariable(config[CONF_ID]) | ||||||
|  |     await cg.register_component(var, config) | ||||||
|  |     if func is not None: | ||||||
|  |         await func(var, config) | ||||||
|  |  | ||||||
|  |     if temperature_config := config.get(CONF_TEMPERATURE): | ||||||
|  |         sens = await sensor.new_sensor(temperature_config) | ||||||
|  |         cg.add(var.set_temperature_sensor(sens)) | ||||||
|  |         cg.add(var.set_temperature_oversampling(temperature_config[CONF_OVERSAMPLING])) | ||||||
|  |  | ||||||
|  |     if pressure_config := config.get(CONF_PRESSURE): | ||||||
|  |         sens = await sensor.new_sensor(pressure_config) | ||||||
|  |         cg.add(var.set_pressure_sensor(sens)) | ||||||
|  |         cg.add(var.set_pressure_oversampling(pressure_config[CONF_OVERSAMPLING])) | ||||||
|  |  | ||||||
|  |     if humidity_config := config.get(CONF_HUMIDITY): | ||||||
|  |         sens = await sensor.new_sensor(humidity_config) | ||||||
|  |         cg.add(var.set_humidity_sensor(sens)) | ||||||
|  |         cg.add(var.set_humidity_oversampling(humidity_config[CONF_OVERSAMPLING])) | ||||||
|  |  | ||||||
|  |     cg.add(var.set_iir_filter(config[CONF_IIR_FILTER])) | ||||||
							
								
								
									
										30
									
								
								esphome/components/bme280_i2c/bme280_i2c.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								esphome/components/bme280_i2c/bme280_i2c.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | |||||||
|  | #include <cstddef> | ||||||
|  | #include <cstdint> | ||||||
|  |  | ||||||
|  | #include "bme280_i2c.h" | ||||||
|  | #include "esphome/components/i2c/i2c.h" | ||||||
|  | #include "../bme280_base/bme280_base.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace bme280_i2c { | ||||||
|  |  | ||||||
|  | bool BME280I2CComponent::read_byte(uint8_t a_register, uint8_t *data) { | ||||||
|  |   return I2CDevice::read_byte(a_register, data); | ||||||
|  | }; | ||||||
|  | bool BME280I2CComponent::write_byte(uint8_t a_register, uint8_t data) { | ||||||
|  |   return I2CDevice::write_byte(a_register, data); | ||||||
|  | }; | ||||||
|  | bool BME280I2CComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) { | ||||||
|  |   return I2CDevice::read_bytes(a_register, data, len); | ||||||
|  | }; | ||||||
|  | bool BME280I2CComponent::read_byte_16(uint8_t a_register, uint16_t *data) { | ||||||
|  |   return I2CDevice::read_byte_16(a_register, data); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | void BME280I2CComponent::dump_config() { | ||||||
|  |   LOG_I2C_DEVICE(this); | ||||||
|  |   BME280Component::dump_config(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | }  // namespace bme280_i2c | ||||||
|  | }  // namespace esphome | ||||||
							
								
								
									
										20
									
								
								esphome/components/bme280_i2c/bme280_i2c.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								esphome/components/bme280_i2c/bme280_i2c.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | |||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include "esphome/components/bme280_base/bme280_base.h" | ||||||
|  | #include "esphome/components/i2c/i2c.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace bme280_i2c { | ||||||
|  |  | ||||||
|  | static const char *const TAG = "bme280_i2c.sensor"; | ||||||
|  |  | ||||||
|  | class BME280I2CComponent : public esphome::bme280_base::BME280Component, public i2c::I2CDevice { | ||||||
|  |   bool read_byte(uint8_t a_register, uint8_t *data) override; | ||||||
|  |   bool write_byte(uint8_t a_register, uint8_t data) override; | ||||||
|  |   bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) override; | ||||||
|  |   bool read_byte_16(uint8_t a_register, uint16_t *data) override; | ||||||
|  |   void dump_config() override; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | }  // namespace bme280_i2c | ||||||
|  | }  // namespace esphome | ||||||
							
								
								
									
										19
									
								
								esphome/components/bme280_i2c/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								esphome/components/bme280_i2c/sensor.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | |||||||
|  | import esphome.codegen as cg | ||||||
|  | from esphome.components import i2c | ||||||
|  | from ..bme280_base.sensor import to_code as to_code_base, cv, CONFIG_SCHEMA_BASE | ||||||
|  |  | ||||||
|  | DEPENDENCIES = ["i2c"] | ||||||
|  | AUTO_LOAD = ["bme280_base"] | ||||||
|  |  | ||||||
|  | bme280_ns = cg.esphome_ns.namespace("bme280_i2c") | ||||||
|  | BME280I2CComponent = bme280_ns.class_( | ||||||
|  |     "BME280I2CComponent", cg.PollingComponent, i2c.I2CDevice | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | CONFIG_SCHEMA = CONFIG_SCHEMA_BASE.extend( | ||||||
|  |     i2c.i2c_device_schema(default_address=0x77) | ||||||
|  | ).extend({cv.GenerateID(): cv.declare_id(BME280I2CComponent)}) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | async def to_code(config): | ||||||
|  |     await to_code_base(config, func=i2c.register_i2c_device) | ||||||
							
								
								
									
										1
									
								
								esphome/components/bme280_spi/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								esphome/components/bme280_spi/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | CODEOWNERS = ["@apbodrov"] | ||||||
							
								
								
									
										66
									
								
								esphome/components/bme280_spi/bme280_spi.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								esphome/components/bme280_spi/bme280_spi.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,66 @@ | |||||||
|  | #include <cstdint> | ||||||
|  | #include <cstddef> | ||||||
|  |  | ||||||
|  | #include "bme280_spi.h" | ||||||
|  | #include <esphome/components/bme280_base/bme280_base.h> | ||||||
|  |  | ||||||
|  | int set_bit(uint8_t num, int position) { | ||||||
|  |   int mask = 1 << position; | ||||||
|  |   return num | mask; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | int clear_bit(uint8_t num, int position) { | ||||||
|  |   int mask = 1 << position; | ||||||
|  |   return num & ~mask; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace bme280_spi { | ||||||
|  |  | ||||||
|  | void BME280SPIComponent::setup() { | ||||||
|  |   this->spi_setup(); | ||||||
|  |   BME280Component::setup(); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | // In SPI mode, only 7 bits of the register addresses are used; the MSB of register address is not used | ||||||
|  | // and replaced by a read/write bit (RW = ‘0’ for write and RW = ‘1’ for read). | ||||||
|  | // Example: address 0xF7 is accessed by using SPI register address 0x77. For write access, the byte | ||||||
|  | // 0x77 is transferred, for read access, the byte 0xF7 is transferred. | ||||||
|  | // https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf | ||||||
|  |  | ||||||
|  | bool BME280SPIComponent::read_byte(uint8_t a_register, uint8_t *data) { | ||||||
|  |   this->enable(); | ||||||
|  |   // cause: *data = this->delegate_->transfer(tmp) doesnt work | ||||||
|  |   this->delegate_->transfer(set_bit(a_register, 7)); | ||||||
|  |   *data = this->delegate_->transfer(0); | ||||||
|  |   this->disable(); | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | bool BME280SPIComponent::write_byte(uint8_t a_register, uint8_t data) { | ||||||
|  |   this->enable(); | ||||||
|  |   this->delegate_->transfer(clear_bit(a_register, 7)); | ||||||
|  |   this->delegate_->transfer(data); | ||||||
|  |   this->disable(); | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | bool BME280SPIComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) { | ||||||
|  |   this->enable(); | ||||||
|  |   this->delegate_->transfer(set_bit(a_register, 7)); | ||||||
|  |   this->delegate_->read_array(data, len); | ||||||
|  |   this->disable(); | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | bool BME280SPIComponent::read_byte_16(uint8_t a_register, uint16_t *data) { | ||||||
|  |   this->enable(); | ||||||
|  |   this->delegate_->transfer(set_bit(a_register, 7)); | ||||||
|  |   ((uint8_t *) data)[1] = this->delegate_->transfer(0); | ||||||
|  |   ((uint8_t *) data)[0] = this->delegate_->transfer(0); | ||||||
|  |   this->disable(); | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | }  // namespace bme280_spi | ||||||
|  | }  // namespace esphome | ||||||
							
								
								
									
										20
									
								
								esphome/components/bme280_spi/bme280_spi.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								esphome/components/bme280_spi/bme280_spi.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | |||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include "esphome/components/bme280_base/bme280_base.h" | ||||||
|  | #include "esphome/components/spi/spi.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace bme280_spi { | ||||||
|  |  | ||||||
|  | class BME280SPIComponent : public esphome::bme280_base::BME280Component, | ||||||
|  |                            public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, | ||||||
|  |                                                  spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_200KHZ> { | ||||||
|  |   void setup() override; | ||||||
|  |   bool read_byte(uint8_t a_register, uint8_t *data) override; | ||||||
|  |   bool write_byte(uint8_t a_register, uint8_t data) override; | ||||||
|  |   bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) override; | ||||||
|  |   bool read_byte_16(uint8_t a_register, uint16_t *data) override; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | }  // namespace bme280_spi | ||||||
|  | }  // namespace esphome | ||||||
							
								
								
									
										24
									
								
								esphome/components/bme280_spi/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								esphome/components/bme280_spi/sensor.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | |||||||
|  | import esphome.codegen as cg | ||||||
|  | from esphome.components import spi | ||||||
|  | from esphome.components.bme280_base.sensor import ( | ||||||
|  |     to_code as to_code_base, | ||||||
|  |     cv, | ||||||
|  |     CONFIG_SCHEMA_BASE, | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | DEPENDENCIES = ["spi"] | ||||||
|  | AUTO_LOAD = ["bme280_base"] | ||||||
|  |  | ||||||
|  |  | ||||||
|  | bme280_spi_ns = cg.esphome_ns.namespace("bme280_spi") | ||||||
|  | BME280SPIComponent = bme280_spi_ns.class_( | ||||||
|  |     "BME280SPIComponent", cg.PollingComponent, spi.SPIDevice | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | CONFIG_SCHEMA = CONFIG_SCHEMA_BASE.extend(spi.spi_device_schema()).extend( | ||||||
|  |     {cv.GenerateID(): cv.declare_id(BME280SPIComponent)} | ||||||
|  | ) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | async def to_code(config): | ||||||
|  |     await to_code_base(config, func=spi.register_spi_device) | ||||||
| @@ -690,7 +690,7 @@ sensor: | |||||||
|     update_interval: 30s |     update_interval: 30s | ||||||
|     mode: low_power |     mode: low_power | ||||||
|     i2c_id: i2c_bus |     i2c_id: i2c_bus | ||||||
|   - platform: bme280 |   - platform: bme280_i2c | ||||||
|     temperature: |     temperature: | ||||||
|       name: Outside Temperature |       name: Outside Temperature | ||||||
|       oversampling: 16x |       oversampling: 16x | ||||||
| @@ -704,6 +704,21 @@ sensor: | |||||||
|     iir_filter: 16x |     iir_filter: 16x | ||||||
|     update_interval: 15s |     update_interval: 15s | ||||||
|     i2c_id: i2c_bus |     i2c_id: i2c_bus | ||||||
|  |   - platform: bme280_spi | ||||||
|  |     temperature: | ||||||
|  |       name: Outside Temperature | ||||||
|  |       oversampling: 16x | ||||||
|  |     pressure: | ||||||
|  |       name: Outside Pressure | ||||||
|  |       oversampling: none | ||||||
|  |     humidity: | ||||||
|  |       name: Outside Humidity | ||||||
|  |       oversampling: 8x | ||||||
|  |     cs_pin: | ||||||
|  |       allow_other_uses: true | ||||||
|  |       number: GPIO23 | ||||||
|  |     iir_filter: 16x | ||||||
|  |     update_interval: 15s | ||||||
|   - platform: bme680 |   - platform: bme680 | ||||||
|     temperature: |     temperature: | ||||||
|       name: Outside Temperature |       name: Outside Temperature | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user