From db59efbc0715181a4b2bb1c0a6e1e8b08dcd91b0 Mon Sep 17 00:00:00 2001 From: tyeth Date: Wed, 27 Sep 2023 17:19:36 +0100 Subject: [PATCH] Add HTU21D/DF + HTU31D --- library.properties | 2 +- platformio.ini | 5 ++ src/components/i2c/WipperSnapper_I2C.cpp | 22 +++++ src/components/i2c/WipperSnapper_I2C.h | 4 + .../drivers/WipperSnapper_I2C_Driver_HTU21D.h | 89 +++++++++++++++++++ .../drivers/WipperSnapper_I2C_Driver_HTU31D.h | 87 ++++++++++++++++++ 6 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 src/components/i2c/drivers/WipperSnapper_I2C_Driver_HTU21D.h create mode 100644 src/components/i2c/drivers/WipperSnapper_I2C_Driver_HTU31D.h diff --git a/library.properties b/library.properties index 53f86918b..da6dda71e 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 INA219, Adafruit LTR390 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit PM25 AQI Sensor, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork +depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR390 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit PM25 AQI Sensor, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork diff --git a/platformio.ini b/platformio.ini index bad10cbeb..eeb7fb3ca 100644 --- a/platformio.ini +++ b/platformio.ini @@ -30,6 +30,8 @@ lib_deps = adafruit/Adafruit DPS310 adafruit/Adafruit INA219 adafruit/Adafruit HTS221 + adafruit/Adafruit HTU21DF Library + adafruit/Adafruit HTU31D Library adafruit/Adafruit LTR390 Library adafruit/Adafruit PCT2075 adafruit/Adafruit SCD30 @@ -89,6 +91,8 @@ lib_ldf_mode = deep extends = common:esp32 board = featheresp32 build_flags = -DARDUINO_FEATHER_ESP32 +board_build.filesystem = littlefs + ; Adafruit ESP32 Feather V2 [env:featheresp32v2] @@ -96,6 +100,7 @@ extends = common:esp32 board = adafruit_feather_esp32_v2 board_build.partitions = default_8MB.csv build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32_V2 +board_build.filesystem = littlefs ; Adafruit Feather ESP32-S2 [env:featheresp32s2] diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index 3ccbfb8bb..0fea9a3e4 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -318,6 +318,28 @@ bool WipperSnapper_Component_I2C::initI2CDevice( _hts221->configureDriver(msgDeviceInitReq); drivers.push_back(_hts221); WS_DEBUG_PRINTLN("HTS221 Initialized Successfully!"); + } else if (strcmp("htu21d", msgDeviceInitReq->i2c_device_name) == 0) { + _htu21d = new WipperSnapper_I2C_Driver_HTU21D(this->_i2c, i2cAddress); + if (!_htu21d->begin()) { + WS_DEBUG_PRINTLN("ERROR: Failed to initialize HTU21D!"); + _busStatusResponse = + wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL; + return false; + } + _htu21d->configureDriver(msgDeviceInitReq); + drivers.push_back(_htu21d); + WS_DEBUG_PRINTLN("HTU21D Initialized Successfully!"); + } else if (strcmp("htu31d", msgDeviceInitReq->i2c_device_name) == 0) { + _htu31d = new WipperSnapper_I2C_Driver_HTU31D(this->_i2c, i2cAddress); + if (!_htu31d->begin()) { + WS_DEBUG_PRINTLN("ERROR: Failed to initialize HTU31D!"); + _busStatusResponse = + wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL; + return false; + } + _htu31d->configureDriver(msgDeviceInitReq); + drivers.push_back(_htu31d); + WS_DEBUG_PRINTLN("HTU31D Initialized Successfully!"); } else if (strcmp("scd30", msgDeviceInitReq->i2c_device_name) == 0) { _scd30 = new WipperSnapper_I2C_Driver_SCD30(this->_i2c, i2cAddress); if (!_scd30->begin()) { diff --git a/src/components/i2c/WipperSnapper_I2C.h b/src/components/i2c/WipperSnapper_I2C.h index 2659d9f0d..de3b57f82 100644 --- a/src/components/i2c/WipperSnapper_I2C.h +++ b/src/components/i2c/WipperSnapper_I2C.h @@ -30,6 +30,8 @@ #include "drivers/WipperSnapper_I2C_Driver_DPS310.h" #include "drivers/WipperSnapper_I2C_Driver_ENS160.h" #include "drivers/WipperSnapper_I2C_Driver_HTS221.h" +#include "drivers/WipperSnapper_I2C_Driver_HTU21D.h" +#include "drivers/WipperSnapper_I2C_Driver_HTU31D.h" #include "drivers/WipperSnapper_I2C_Driver_INA219.h" #include "drivers/WipperSnapper_I2C_Driver_LC709203F.h" #include "drivers/WipperSnapper_I2C_Driver_LPS22HB.h" @@ -115,6 +117,8 @@ class WipperSnapper_Component_I2C { WipperSnapper_I2C_Driver_BMP3XX *_bmp3xx = nullptr; WipperSnapper_I2C_Driver_BME680 *_bme680 = nullptr; WipperSnapper_I2C_Driver_HTS221 *_hts221 = nullptr; + WipperSnapper_I2C_Driver_HTU21D *_htu21d = nullptr; + WipperSnapper_I2C_Driver_HTU31D *_htu31d = nullptr; WipperSnapper_I2C_Driver_INA219 *_ina219 = nullptr; WipperSnapper_I2C_Driver_LTR390 *_ltr390 = nullptr; WipperSnapper_I2C_Driver_MCP9808 *_mcp9808 = nullptr; diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_HTU21D.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_HTU21D.h new file mode 100644 index 000000000..bfc689216 --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_HTU21D.h @@ -0,0 +1,89 @@ +/*! + * @file WipperSnapper_I2C_Driver_HTU21D.h + * + * Device driver for an HTU21D Humidity and Temperature sensor. + */ + +#ifndef WipperSnapper_I2C_Driver_HTU21D_H +#define WipperSnapper_I2C_Driver_HTU21D_H + +#include "WipperSnapper_I2C_Driver.h" +#include + +/**************************************************************************/ +/*! + @brief Class that provides a sensor driver for the HTU21D humidity and + temperature sensor. +*/ +/**************************************************************************/ +class WipperSnapper_I2C_Driver_HTU21D : public WipperSnapper_I2C_Driver { + +public: + /*******************************************************************************/ + /*! + @brief Constructor for an HTU21D sensor. + @param i2c + The I2C interface. + @param sensorAddress + 7-bit device address. + */ + /*******************************************************************************/ + WipperSnapper_I2C_Driver_HTU21D(TwoWire *i2c, uint16_t sensorAddress) + : WipperSnapper_I2C_Driver(i2c, sensorAddress) { + _i2c = i2c; + _sensorAddress = sensorAddress; + } + + /*******************************************************************************/ + /*! + @brief Destructor for an HTU21D sensor. + */ + /*******************************************************************************/ + ~WipperSnapper_I2C_Driver_HTU21D() { delete _htu21d; } + + /*******************************************************************************/ + /*! + @brief Initializes the HTU21D sensor and begins I2C. + @returns True if initialized successfully, False otherwise. + + */ + /*******************************************************************************/ + bool begin() { + // attempt to initialize the HTU21D using the I2C interface + _htu21d = new Adafruit_HTU21DF(); + return _htu21d->begin(_i2c); + } + + /*******************************************************************************/ + /*! + @brief Gets the HTU21D'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) { + tempEvent->temperature = _htu21d->readTemperature(); + return true; + } + + /*******************************************************************************/ + /*! + @brief Gets the HTU21D's current humidity. + @param humidEvent + Pointer to an Adafruit_Sensor event. + @returns True if the humidity was obtained successfully, False + otherwise. + */ + /*******************************************************************************/ + bool getEventRelativeHumidity(sensors_event_t *humidEvent) { + humidEvent->relative_humidity = _htu21d->readHumidity(); + return true; + } + +protected: + Adafruit_HTU21DF *_htu21d; ///< Pointer to an HTU21D object +}; + +#endif // WipperSnapper_I2C_Driver_HTU21D \ No newline at end of file diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_HTU31D.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_HTU31D.h new file mode 100644 index 000000000..aace146b0 --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_HTU31D.h @@ -0,0 +1,87 @@ +/*! + * @file WipperSnapper_I2C_Driver_HTU31D.h + * + * Device driver for an HTU31D Humidity and Temperature sensor. + */ + +#ifndef WipperSnapper_I2C_Driver_HTU31D_H +#define WipperSnapper_I2C_Driver_HTU31D_H + +#include "WipperSnapper_I2C_Driver.h" +#include + +/**************************************************************************/ +/*! + @brief Class that provides a sensor driver for the HTU31D humidity and + temperature sensor. +*/ +/**************************************************************************/ +class WipperSnapper_I2C_Driver_HTU31D : public WipperSnapper_I2C_Driver { + +public: + /*******************************************************************************/ + /*! + @brief Constructor for an HTU31D sensor. + @param i2c + The I2C interface. + @param sensorAddress + 7-bit device address. + */ + /*******************************************************************************/ + WipperSnapper_I2C_Driver_HTU31D(TwoWire *i2c, uint16_t sensorAddress) + : WipperSnapper_I2C_Driver(i2c, sensorAddress) { + _i2c = i2c; + _sensorAddress = sensorAddress; + } + + /*******************************************************************************/ + /*! + @brief Destructor for an HTU31D sensor. + */ + /*******************************************************************************/ + ~WipperSnapper_I2C_Driver_HTU31D() { delete _htu31d; } + + /*******************************************************************************/ + /*! + @brief Initializes the HTU31D sensor and begins I2C. + @returns True if initialized successfully, False otherwise. + + */ + /*******************************************************************************/ + bool begin() { + // attempt to initialize the HTU31D using the I2C interface + _htu31d = new Adafruit_HTU31D(); + return _htu31d->begin(_sensorAddress, _i2c); + } + + /*******************************************************************************/ + /*! + @brief Gets the HTU31D'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) { + return _htu31d->getEvent(nullptr, tempEvent); + } + + /*******************************************************************************/ + /*! + @brief Gets the HTU31D's current humidity. + @param humidEvent + Pointer to an Adafruit_Sensor event. + @returns True if the humidity was obtained successfully, False + otherwise. + */ + /*******************************************************************************/ + bool getEventRelativeHumidity(sensors_event_t *humidEvent) { + return _htu31d->getEvent(humidEvent, nullptr); + } + +protected: + Adafruit_HTU31D *_htu31d; ///< Pointer to an HTU31D object +}; + +#endif // WipperSnapper_I2C_Driver_HTU31D \ No newline at end of file