Skip to content

Commit 6e49411

Browse files
authored
Merge pull request #623 from tyeth/add-HDC302x
Add HDC3021/HDC302x
2 parents 06427ff + 2a0564b commit 6e49411

File tree

5 files changed

+145
-1
lines changed

5 files changed

+145
-1
lines changed

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
99
architectures=*
10-
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, 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 VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, 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
10+
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, 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 VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, 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

platformio.ini

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ lib_deps =
3030
adafruit/Adafruit DPS310
3131
adafruit/Adafruit DS248x
3232
adafruit/Adafruit INA219
33+
adafruit/Adafruit HDC302x
3334
adafruit/Adafruit HTS221
3435
adafruit/Adafruit HTU21DF Library
3536
adafruit/Adafruit HTU31D Library

src/components/i2c/WipperSnapper_I2C.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
318318
_ens160->configureDriver(msgDeviceInitReq);
319319
drivers.push_back(_ens160);
320320
WS_DEBUG_PRINTLN("ENS160 Initialized Successfully!");
321+
} else if (strcmp("hdc302x", msgDeviceInitReq->i2c_device_name) == 0) {
322+
_hdc302x = new WipperSnapper_I2C_Driver_HDC302X(this->_i2c, i2cAddress);
323+
if (!_hdc302x->begin()) {
324+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize HDC302X!");
325+
_busStatusResponse =
326+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
327+
return false;
328+
}
329+
_hdc302x->configureDriver(msgDeviceInitReq);
330+
drivers.push_back(_hdc302x);
331+
WS_DEBUG_PRINTLN("HDC302X Initialized Successfully!");
321332
} else if (strcmp("hts221", msgDeviceInitReq->i2c_device_name) == 0) {
322333
_hts221 = new WipperSnapper_I2C_Driver_HTS221(this->_i2c, i2cAddress);
323334
if (!_hts221->begin()) {

src/components/i2c/WipperSnapper_I2C.h

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "drivers/WipperSnapper_I2C_Driver_DPS310.h"
3131
#include "drivers/WipperSnapper_I2C_Driver_DS2484.h"
3232
#include "drivers/WipperSnapper_I2C_Driver_ENS160.h"
33+
#include "drivers/WipperSnapper_I2C_Driver_HDC302X.h"
3334
#include "drivers/WipperSnapper_I2C_Driver_HTS221.h"
3435
#include "drivers/WipperSnapper_I2C_Driver_HTU21D.h"
3536
#include "drivers/WipperSnapper_I2C_Driver_HTU31D.h"
@@ -141,6 +142,7 @@ class WipperSnapper_Component_I2C {
141142
WipperSnapper_I2C_Driver_BMP280 *_bmp280 = nullptr;
142143
WipperSnapper_I2C_Driver_BMP3XX *_bmp3xx = nullptr;
143144
WipperSnapper_I2C_Driver_BME680 *_bme680 = nullptr;
145+
WipperSnapper_I2C_Driver_HDC302X *_hdc302x = nullptr;
144146
WipperSnapper_I2C_Driver_HTS221 *_hts221 = nullptr;
145147
WipperSnapper_I2C_Driver_HTU21D *_htu21d = nullptr;
146148
WipperSnapper_I2C_Driver_HTU31D *_htu31d = nullptr;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_HDC302X.h
3+
*
4+
* Device driver for an HDC302X Humidity and Temperature sensor.
5+
*/
6+
7+
#ifndef WipperSnapper_I2C_Driver_HDC302X_H
8+
#define WipperSnapper_I2C_Driver_HDC302X_H
9+
10+
#include "WipperSnapper_I2C_Driver.h"
11+
#include <Adafruit_HDC302x.h>
12+
13+
/**************************************************************************/
14+
/*!
15+
@brief Class that provides a sensor driver for the HDC302X humidity and
16+
temperature sensor. This implementation uses the 1 Hz data rate.
17+
*/
18+
/**************************************************************************/
19+
class WipperSnapper_I2C_Driver_HDC302X : public WipperSnapper_I2C_Driver {
20+
21+
public:
22+
/*******************************************************************************/
23+
/*!
24+
@brief Constructor for an HDC302X sensor.
25+
@param i2c
26+
The I2C interface.
27+
@param sensorAddress
28+
7-bit device address.
29+
*/
30+
/*******************************************************************************/
31+
WipperSnapper_I2C_Driver_HDC302X(TwoWire *i2c, uint16_t sensorAddress)
32+
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
33+
_i2c = i2c;
34+
_sensorAddress = sensorAddress;
35+
}
36+
37+
/*******************************************************************************/
38+
/*!
39+
@brief Destructor for an HDC302X sensor.
40+
*/
41+
/*******************************************************************************/
42+
~WipperSnapper_I2C_Driver_HDC302X() { delete _hdc302x; }
43+
44+
/*******************************************************************************/
45+
/*!
46+
@brief Initializes the HDC302X sensor and begins I2C.
47+
@returns True if initialized successfully, False otherwise.
48+
49+
*/
50+
/*******************************************************************************/
51+
bool begin() {
52+
// attempt to initialize the HDC302X using the I2C interface
53+
_hdc302x = new Adafruit_HDC302x();
54+
if (!_hdc302x->begin(_sensorAddress, _i2c))
55+
return false;
56+
57+
// set the HDC302X's data rate to 1 Hz lowest noise
58+
_hdc302x->setAutoMode(EXIT_AUTO_MODE);
59+
// discard first reading (It returned -45c for me once)
60+
_hdc302x->readTemperatureHumidityOnDemand(_temp, _humidity,
61+
TRIGGERMODE_LP0);
62+
return true;
63+
}
64+
65+
/*******************************************************************************/
66+
/*!
67+
@brief Reads the HDC302X's temperature and humidity data.
68+
@returns True if the data was read successfully, False otherwise.
69+
*/
70+
/*******************************************************************************/
71+
bool readSensorData() {
72+
uint16_t status = _hdc302x->readStatus();
73+
if (status & 0x0010) {
74+
WS_DEBUG_PRINTLN(F("Device Reset Detected"));
75+
return false;
76+
}
77+
78+
if (status & 0x0001) {
79+
WS_DEBUG_PRINTLN(
80+
F("Checksum Verification Fail (incorrect checksum received)"));
81+
return false;
82+
}
83+
84+
if (!_hdc302x->readTemperatureHumidityOnDemand(_temp, _humidity,
85+
TRIGGERMODE_LP0)) {
86+
WS_DEBUG_PRINTLN(F("Failed to read temperature and humidity."));
87+
return false;
88+
}
89+
return true;
90+
}
91+
92+
/*******************************************************************************/
93+
/*!
94+
@brief Gets the HDC302X's current temperature.
95+
@param tempEvent
96+
Pointer to an Adafruit_Sensor event.
97+
@returns True if the temperature was obtained successfully, False
98+
otherwise.
99+
*/
100+
/*******************************************************************************/
101+
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
102+
if (readSensorData() == false)
103+
return false;
104+
tempEvent->temperature = _temp;
105+
return true;
106+
}
107+
108+
/*******************************************************************************/
109+
/*!
110+
@brief Gets the HDC302X's current humidity.
111+
@param humidEvent
112+
Pointer to an Adafruit_Sensor event.
113+
@returns True if the humidity was obtained successfully, False
114+
otherwise.
115+
*/
116+
/*******************************************************************************/
117+
bool getEventRelativeHumidity(sensors_event_t *humidEvent) {
118+
if (readSensorData() == false)
119+
return false;
120+
humidEvent->relative_humidity = _humidity;
121+
return true;
122+
}
123+
124+
protected:
125+
Adafruit_HDC302x *_hdc302x; ///< Pointer to an HDC302X object
126+
double _temp = 0.0; ///< Holds data for the HDC302X's temperature sensor
127+
double _humidity = 0.0; ///< Holds data for the HDC302X's humidity sensor
128+
};
129+
130+
#endif // WipperSnapper_I2C_Driver_HDC302X

0 commit comments

Comments
 (0)