From 5bd91cb834f1748eef38f468860acd3d22f0b0ab Mon Sep 17 00:00:00 2001 From: tyeth Date: Mon, 31 Jul 2023 11:41:01 +0100 Subject: [PATCH] Add BMP280 --- library.properties | 2 +- platformio.ini | 1 + src/components/i2c/WipperSnapper_I2C.cpp | 11 ++ src/components/i2c/WipperSnapper_I2C.h | 2 + .../drivers/WipperSnapper_I2C_Driver_BMP280.h | 139 ++++++++++++++++++ 5 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/components/i2c/drivers/WipperSnapper_I2C_Driver_BMP280.h diff --git a/library.properties b/library.properties index 74fc2cffc..c475ca997 100644 --- a/library.properties +++ b/library.properties @@ -7,4 +7,4 @@ paragraph=Arduino client for Adafruit.io WipperSnapper category=Communication url=https://github.com/adafruit/Adafruit_IO_Arduino architectures=* -depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit SleepyDog Library, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit PM25 AQI Sensor, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit PCT2075, hp_BH1750 +depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit SleepyDog Library, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit PM25 AQI Sensor, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit PCT2075, hp_BH1750 diff --git a/platformio.ini b/platformio.ini index 003873773..983d60831 100644 --- a/platformio.ini +++ b/platformio.ini @@ -24,6 +24,7 @@ lib_deps = adafruit/Adafruit SleepyDog Library adafruit/Adafruit AHTX0 adafruit/Adafruit BME280 Library + adafruit/Adafruit BMP280 Library adafruit/Adafruit DPS310 adafruit/Adafruit HTS221 adafruit/Adafruit PCT2075 diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index 58bb442c1..4ffd09e09 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -247,6 +247,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice( _bme280->configureDriver(msgDeviceInitReq); drivers.push_back(_bme280); WS_DEBUG_PRINTLN("BME280 Initialized Successfully!"); + } else if (strcmp("bmp280", msgDeviceInitReq->i2c_device_name) == 0) { + _bmp280 = new WipperSnapper_I2C_Driver_BMP280(this->_i2c, i2cAddress); + if (!_bmp280->begin()) { + WS_DEBUG_PRINTLN("ERROR: Failed to initialize BMP280!"); + _busStatusResponse = + wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL; + return false; + } + _bmp280->configureDriver(msgDeviceInitReq); + drivers.push_back(_bmp280); + WS_DEBUG_PRINTLN("BMP280 Initialized Successfully!"); } else if (strcmp("bme680", msgDeviceInitReq->i2c_device_name) == 0) { _bme680 = new WipperSnapper_I2C_Driver_BME680(this->_i2c, i2cAddress); if (!_bme680->begin()) { diff --git a/src/components/i2c/WipperSnapper_I2C.h b/src/components/i2c/WipperSnapper_I2C.h index 1a629910e..d4b0b2493 100644 --- a/src/components/i2c/WipperSnapper_I2C.h +++ b/src/components/i2c/WipperSnapper_I2C.h @@ -24,6 +24,7 @@ #include "drivers/WipperSnapper_I2C_Driver_AHTX0.h" #include "drivers/WipperSnapper_I2C_Driver_BH1750.h" #include "drivers/WipperSnapper_I2C_Driver_BME280.h" +#include "drivers/WipperSnapper_I2C_Driver_BMP280.h" #include "drivers/WipperSnapper_I2C_Driver_BME680.h" #include "drivers/WipperSnapper_I2C_Driver_DPS310.h" #include "drivers/WipperSnapper_I2C_Driver_HTS221.h" @@ -97,6 +98,7 @@ class WipperSnapper_Component_I2C { WipperSnapper_I2C_Driver_SCD30 *_scd30 = nullptr; WipperSnapper_I2C_Driver_BH1750 *_bh1750 = nullptr; WipperSnapper_I2C_Driver_BME280 *_bme280 = nullptr; + WipperSnapper_I2C_Driver_BMP280 *_bmp280 = nullptr; WipperSnapper_I2C_Driver_BME680 *_bme680 = nullptr; WipperSnapper_I2C_Driver_HTS221 *_hts221 = nullptr; WipperSnapper_I2C_Driver_MCP9808 *_mcp9808 = nullptr; diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_BMP280.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_BMP280.h new file mode 100644 index 000000000..fe2e60bfa --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_BMP280.h @@ -0,0 +1,139 @@ +/*! + * @file WipperSnapper_I2C_Driver_BMP280.h + * + * Device driver for an AHT Humidity and Temperature sensor. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Copyright (c) Tyeth Gundry 2023 for Adafruit Industries. + * + * MIT license, all text here must be included in any redistribution. + * + */ + +#ifndef WipperSnapper_I2C_Driver_BMP280_H +#define WipperSnapper_I2C_Driver_BMP280_H + +#include "WipperSnapper_I2C_Driver.h" +#include + +#define SEALEVELPRESSURE_HPA (1013.25) ///< Default sea level pressure, in hPa + +/**************************************************************************/ +/*! + @brief Class that provides a sensor driver for the BMP280 temperature + and pressure sensor. +*/ +/**************************************************************************/ +class WipperSnapper_I2C_Driver_BMP280 : public WipperSnapper_I2C_Driver { + +public: + /*******************************************************************************/ + /*! + @brief Constructor for an BMP280 sensor. + @param i2c + The I2C interface. + @param sensorAddress + 7-bit device address. + */ + /*******************************************************************************/ + WipperSnapper_I2C_Driver_BMP280(TwoWire *i2c, uint16_t sensorAddress) + : WipperSnapper_I2C_Driver(i2c, sensorAddress) { + _i2c = i2c; + _sensorAddress = sensorAddress; + } + + /*******************************************************************************/ + /*! + @brief Destructor for an BMP280 sensor. + */ + /*******************************************************************************/ + ~WipperSnapper_I2C_Driver_BMP280() { delete _bmp; } + + /*******************************************************************************/ + /*! + @brief Initializes the BMP280 sensor and begins I2C. + @returns True if initialized successfully, False otherwise. + */ + /*******************************************************************************/ + bool begin() { + _bmp = new Adafruit_BMP280(_i2c); + // attempt to initialize BMP280 + if (!_bmp->begin(_sensorAddress)) + return false; + + // set up sampling as recommended in Adafruit library + _bmp->setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */ + Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */ + Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */ + Adafruit_BMP280::FILTER_X16, /* Filtering. */ + Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */ + + // configure BMP280 device + _bmp_temp = _bmp->getTemperatureSensor(); + _bmp_pressure = _bmp->getPressureSensor(); + return true; + } + + /*******************************************************************************/ + /*! + @brief Gets the BMP280's current temperature. + @param tempEvent + Pointer to an Adafruit_Sensor event. + @returns True if the temperature was obtained successfully, False + otherwise. + */ + /*******************************************************************************/ + bool getEventAmbientTemp(sensors_event_t *tempEvent) { + if (_bmp_temp == NULL) + return false; + _bmp_temp->getEvent(tempEvent); + return true; + } + + /*******************************************************************************/ + /*! + @brief Reads a pressure sensor and converts + the reading into the expected SI unit. + @param pressureEvent + Pointer to an Adafruit_Sensor event. + @returns True if the sensor event was obtained successfully, False + otherwise. + */ + /*******************************************************************************/ + bool getEventPressure(sensors_event_t *pressureEvent) { + if (_bmp_pressure == NULL) + return false; + _bmp_pressure->getEvent(pressureEvent); + return true; + } + + /*******************************************************************************/ + /*! + @brief Reads a the BMP280's altitude sensor into an event. + @param altitudeEvent + Pointer to an adafruit sensor event. + @returns True if the sensor event was obtained successfully, False + otherwise. + */ + /*******************************************************************************/ + bool getEventAltitude(sensors_event_t *altitudeEvent) { + // TODO: Note, this is a hack into Adafruit_Sensor, we should really add an + // altitude sensor type + altitudeEvent->data[0] = _bmp->readAltitude(SEALEVELPRESSURE_HPA); + return true; + } + +protected: + Adafruit_BMP280 *_bmp; ///< BMP280 object + Adafruit_Sensor *_bmp_temp = + NULL; ///< Ptr to an adafruit_sensor representing the temperature + Adafruit_Sensor *_bmp_pressure = + NULL; ///< Ptr to an adafruit_sensor representing the pressure + Adafruit_Sensor *_bmp_humidity = + NULL; ///< Ptr to an adafruit_sensor representing the humidity +}; + +#endif // WipperSnapper_I2C_Driver_BMP280 \ No newline at end of file