Skip to content

Commit 0e688ea

Browse files
authored
Merge pull request #355 from brentru/fix-issue-346
Fix error adding BME680, TSL2591
2 parents d8e4946 + b568697 commit 0e688ea

File tree

6 files changed

+288
-14
lines changed

6 files changed

+288
-14
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=Arduino client for Adafruit.io WipperSnapper
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_IO_Arduino
99
architectures=*
10-
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit SleepyDog Library, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit DPS310, Adafruit SCD30, Sensirion I2C SCD4x, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit TSL2591 Library, Adafruit PM25 AQI Sensor, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit seesaw Library
10+
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit SleepyDog Library, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit DPS310, Adafruit SCD30, Sensirion I2C SCD4x, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit TSL2591 Library, Adafruit PM25 AQI Sensor, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit seesaw Library, Adafruit BME680 Library

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ WipperSnapper_Component_I2C::scanAddresses() {
203203
/*******************************************************************************/
204204
bool WipperSnapper_Component_I2C::initI2CDevice(
205205
wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq) {
206-
WS_DEBUG_PRINTLN("Attempting to initialize an I2C device...");
206+
WS_DEBUG_PRINT("Attempting to initialize I2C device: ");
207+
WS_DEBUG_PRINTLN(msgDeviceInitReq->i2c_device_name);
207208

208209
uint16_t i2cAddress = (uint16_t)msgDeviceInitReq->i2c_device_address;
209210
if (strcmp("aht20", msgDeviceInitReq->i2c_device_name) == 0) {
@@ -227,6 +228,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
227228
_bme280->configureDriver(msgDeviceInitReq);
228229
drivers.push_back(_bme280);
229230
WS_DEBUG_PRINTLN("BME280 Initialized Successfully!");
231+
} else if (strcmp("bme680", msgDeviceInitReq->i2c_device_name) == 0) {
232+
_bme680 = new WipperSnapper_I2C_Driver_BME680(this->_i2c, i2cAddress);
233+
if (!_bme680->begin()) {
234+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize BME680!");
235+
_busStatusResponse =
236+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
237+
return false;
238+
}
239+
_bme680->configureDriver(msgDeviceInitReq);
240+
drivers.push_back(_bme680);
241+
WS_DEBUG_PRINTLN("BME680 Initialized Successfully!");
230242
} else if (strcmp("dps310", msgDeviceInitReq->i2c_device_name) == 0) {
231243
_dps310 = new WipperSnapper_I2C_Driver_DPS310(this->_i2c, i2cAddress);
232244
if (!_dps310->begin()) {
@@ -879,6 +891,29 @@ void WipperSnapper_Component_I2C::update() {
879891
(*iter)->setSensorRawPeriodPrv(curTime);
880892
}
881893

894+
// Gas sensor
895+
curTime = millis();
896+
if ((*iter)->getSensorGasResistancePeriod() != 0L &&
897+
curTime - (*iter)->getSensorGasResistancePeriodPrv() >
898+
(*iter)->getSensorGasResistancePeriod()) {
899+
if ((*iter)->getEventGasResistance(&event)) {
900+
WS_DEBUG_PRINT("Sensor 0x");
901+
WS_DEBUG_PRINTHEX((*iter)->getI2CAddress());
902+
WS_DEBUG_PRINTLN("");
903+
WS_DEBUG_PRINT("\tGas Resistance: ");
904+
WS_DEBUG_PRINT(event.data[0]);
905+
WS_DEBUG_PRINT(" ohms");
906+
907+
fillEventMessage(
908+
&msgi2cResponse, event.data[0],
909+
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_GAS_RESISTANCE);
910+
} else {
911+
WS_DEBUG_PRINTLN(
912+
"ERROR: Failed to obtain gas resistance sensor reading!");
913+
}
914+
(*iter)->setSensorGasResistancePeriodPrv(curTime);
915+
}
916+
882917
// Did this driver obtain data from sensors?
883918
if (msgi2cResponse.payload.resp_i2c_device_event.sensor_event_count == 0)
884919
continue;

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "drivers/WipperSnapper_I2C_Driver.h"
2323
#include "drivers/WipperSnapper_I2C_Driver_AHTX0.h"
2424
#include "drivers/WipperSnapper_I2C_Driver_BME280.h"
25+
#include "drivers/WipperSnapper_I2C_Driver_BME680.h"
2526
#include "drivers/WipperSnapper_I2C_Driver_DPS310.h"
2627
#include "drivers/WipperSnapper_I2C_Driver_LC709203F.h"
2728
#include "drivers/WipperSnapper_I2C_Driver_MCP9601.h"
@@ -83,6 +84,7 @@ class WipperSnapper_Component_I2C {
8384
WipperSnapper_I2C_Driver_DPS310 *_dps310 = nullptr;
8485
WipperSnapper_I2C_Driver_SCD30 *_scd30 = nullptr;
8586
WipperSnapper_I2C_Driver_BME280 *_bme280 = nullptr;
87+
WipperSnapper_I2C_Driver_BME680 *_bme680 = nullptr;
8688
WipperSnapper_I2C_Driver_MCP9808 *_mcp9808 = nullptr;
8789
WipperSnapper_I2C_Driver_MCP9601 *_mcp9601 = nullptr;
8890
WipperSnapper_I2C_Driver_TSL2591 *_tsl2591 = nullptr;

src/components/i2c/drivers/WipperSnapper_I2C_Driver.h

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ class WipperSnapper_I2C_Driver {
114114
break;
115115
case wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE_FAHRENHEIT:
116116
_objectTempFPeriod = sensorPeriod;
117+
break;
118+
case wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_GAS_RESISTANCE:
119+
_gasResistancePeriod = sensorPeriod;
120+
break;
117121
default:
118122
break;
119123
}
@@ -868,6 +872,57 @@ class WipperSnapper_I2C_Driver {
868872
return true;
869873
}
870874

875+
/****************************** SENSOR_TYPE: Gas Resistance (ohms)
876+
* *******************************/
877+
/*********************************************************************************/
878+
/*!
879+
@brief Base implementation - Returns the gas resistance (ohms)
880+
sensor's period, if set.
881+
@returns Time when the gas resistance sensor should be polled,
882+
in seconds.
883+
*/
884+
/*********************************************************************************/
885+
virtual long getSensorGasResistancePeriod() { return _gasResistancePeriod; }
886+
887+
/*********************************************************************************/
888+
/*!
889+
@brief Base implementation - Returns the previous time interval at
890+
which the gas resistance sensor (ohms) was queried last.
891+
@returns Time when the gas resistance sensor was last queried,
892+
in seconds.
893+
*/
894+
/*********************************************************************************/
895+
virtual long getSensorGasResistancePeriodPrv() {
896+
return _gasResistancePeriodPrv;
897+
}
898+
899+
/*******************************************************************************/
900+
/*!
901+
@brief Sets a timestamp for when the object gas resistance sensor
902+
was queried.
903+
@param period
904+
The time when the gas resistance sensor was queried
905+
last.
906+
*/
907+
/*******************************************************************************/
908+
virtual void setSensorGasResistancePeriodPrv(long period) {
909+
_gasResistancePeriodPrv = period;
910+
}
911+
912+
/*******************************************************************************/
913+
/*!
914+
@brief Base implementation - Reads a gas resistance sensor and converts
915+
the reading into the expected SI unit.
916+
@param gasEvent
917+
gas resistance sensor reading, in ohms.
918+
@returns True if the sensor event was obtained successfully, False
919+
otherwise.
920+
*/
921+
/*******************************************************************************/
922+
virtual bool getEventGasResistance(sensors_event_t *gasEvent) {
923+
return false;
924+
}
925+
871926
protected:
872927
TwoWire *_i2c; ///< Pointer to the I2C driver's Wire object
873928
uint16_t _sensorAddress; ///< The I2C driver's unique I2C address.
@@ -921,16 +976,20 @@ class WipperSnapper_I2C_Driver {
921976
///< was last read.
922977
long _rawSensorPeriod =
923978
0L; ///< The time period between reading the Raw sensor's value.
924-
long _rawSensorPeriodPrv = 0L; ///< The time when the Raw sensor
925-
///< was last read.
926-
long _ambientTempFPeriod = 0L; ///< The time period between reading the
927-
///< ambient temp. (°F) sensor's value.
928-
long _ambientTempFPeriodPrv = 0L; ///< The time when the ambient temp. (°F)
929-
///< sensor was last read.
930-
long _objectTempFPeriod = 0L; ///< The time period between reading the
931-
///< object temp. (°F) sensor's value.
932-
long _objectTempFPeriodPrv = 0L; ///< The time when the object temp. (°F)
933-
///< sensor was last read.
979+
long _rawSensorPeriodPrv = 0L; ///< The time when the Raw sensor
980+
///< was last read.
981+
long _ambientTempFPeriod = 0L; ///< The time period between reading the
982+
///< ambient temp. (°F) sensor's value.
983+
long _ambientTempFPeriodPrv = 0L; ///< The time when the ambient temp. (°F)
984+
///< sensor was last read.
985+
long _objectTempFPeriod = 0L; ///< The time period between reading the
986+
///< object temp. (°F) sensor's value.
987+
long _objectTempFPeriodPrv = 0L; ///< The time when the object temp. (°F)
988+
///< sensor was last read.
989+
long _gasResistancePeriod = 0L; ///< The time period between reading the
990+
///< gas resistance sensor's value.
991+
long _gasResistancePeriodPrv = 0L; ///< The time when the gas resistance
992+
///< sensor was last read.
934993
};
935994

936995
#endif // WipperSnapper_I2C_Driver_H
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_BME680.h
3+
*
4+
* Device driver for an AHT Humidity and Temperature sensor.
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Brent Rubell 2021 for Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
16+
#ifndef WipperSnapper_I2C_Driver_BME680_H
17+
#define WipperSnapper_I2C_Driver_BME680_H
18+
19+
#include "WipperSnapper_I2C_Driver.h"
20+
#include <Adafruit_BME680.h>
21+
22+
#define SEALEVELPRESSURE_HPA (1013.25) ///< Default sea level pressure, in hPa
23+
24+
/**************************************************************************/
25+
/*!
26+
@brief Class that provides a sensor driver for the BME680 temperature
27+
and humidity sensor.
28+
*/
29+
/**************************************************************************/
30+
class WipperSnapper_I2C_Driver_BME680 : public WipperSnapper_I2C_Driver {
31+
32+
public:
33+
/*******************************************************************************/
34+
/*!
35+
@brief Constructor for an BME680 sensor.
36+
@param i2c
37+
The I2C interface.
38+
@param sensorAddress
39+
7-bit device address.
40+
*/
41+
/*******************************************************************************/
42+
WipperSnapper_I2C_Driver_BME680(TwoWire *i2c, uint16_t sensorAddress)
43+
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
44+
_i2c = i2c;
45+
_sensorAddress = sensorAddress;
46+
}
47+
48+
/*******************************************************************************/
49+
/*!
50+
@brief Destructor for an BME680 sensor.
51+
*/
52+
/*******************************************************************************/
53+
~WipperSnapper_I2C_Driver_BME680() { delete _bme; }
54+
55+
/*******************************************************************************/
56+
/*!
57+
@brief Initializes the BME680 sensor and begins I2C.
58+
@returns True if initialized successfully, False otherwise.
59+
*/
60+
/*******************************************************************************/
61+
bool begin() {
62+
_bme = new Adafruit_BME680(_i2c);
63+
64+
// attempt to initialize BME680
65+
if (!_bme->begin())
66+
return false;
67+
68+
// Set up oversampling and filter initialization
69+
// defaults from
70+
// https://github.com/adafruit/Adafruit_BME680/blob/master/examples/bme680test/bme680test.ino
71+
_bme->setTemperatureOversampling(BME680_OS_8X);
72+
_bme->setHumidityOversampling(BME680_OS_2X);
73+
_bme->setPressureOversampling(BME680_OS_4X);
74+
_bme->setIIRFilterSize(BME680_FILTER_SIZE_3);
75+
_bme->setGasHeater(320, 150); // 320*C for 150 ms
76+
77+
return true;
78+
}
79+
80+
/*******************************************************************************/
81+
/*!
82+
@brief Performs a reading in blocking mode.
83+
@returns True if the reading succeeded, False otherwise.
84+
*/
85+
/*******************************************************************************/
86+
bool bmePerformReading() { return _bme->performReading(); }
87+
88+
/*******************************************************************************/
89+
/*!
90+
@brief Gets the BME680's current temperature.
91+
@param tempEvent
92+
Pointer to an Adafruit_Sensor event.
93+
@returns True if the temperature was obtained successfully, False
94+
otherwise.
95+
*/
96+
/*******************************************************************************/
97+
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
98+
if (!bmePerformReading())
99+
return false;
100+
tempEvent->temperature = _bme->temperature;
101+
return true;
102+
}
103+
104+
/*******************************************************************************/
105+
/*!
106+
@brief Gets the BME680's current relative humidity reading.
107+
@param humidEvent
108+
Pointer to an Adafruit_Sensor event.
109+
@returns True if the humidity was obtained successfully, False
110+
otherwise.
111+
*/
112+
/*******************************************************************************/
113+
bool getEventRelativeHumidity(sensors_event_t *humidEvent) {
114+
if (!bmePerformReading())
115+
return false;
116+
humidEvent->relative_humidity = _bme->humidity;
117+
return true;
118+
}
119+
120+
/*******************************************************************************/
121+
/*!
122+
@brief Reads a pressure sensor and converts
123+
the reading into the expected SI unit.
124+
@param pressureEvent
125+
Pointer to an Adafruit_Sensor event.
126+
@returns True if the sensor event was obtained successfully, False
127+
otherwise.
128+
*/
129+
/*******************************************************************************/
130+
bool getEventPressure(sensors_event_t *pressureEvent) {
131+
if (!bmePerformReading())
132+
return false;
133+
pressureEvent->pressure = (float)_bme->pressure;
134+
return true;
135+
}
136+
137+
/*******************************************************************************/
138+
/*!
139+
@brief Reads a the BME680's altitude sensor into an event.
140+
@param altitudeEvent
141+
Pointer to an adafruit sensor event.
142+
@returns True if the sensor event was obtained successfully, False
143+
otherwise.
144+
*/
145+
/*******************************************************************************/
146+
bool getEventAltitude(sensors_event_t *altitudeEvent) {
147+
if (!bmePerformReading())
148+
return false;
149+
// NOTE: This is hacked onto Adafruit_Sensor and should eventually be
150+
// removed
151+
altitudeEvent->data[0] = (float)_bme->readAltitude(SEALEVELPRESSURE_HPA);
152+
return true;
153+
}
154+
155+
/*******************************************************************************/
156+
/*!
157+
@brief Base implementation - Reads a gas resistance sensor and converts
158+
the reading into the expected SI unit.
159+
@param gasEvent
160+
gas resistance sensor reading, in ohms.
161+
@returns True if the sensor event was obtained successfully, False
162+
otherwise.
163+
*/
164+
/*******************************************************************************/
165+
virtual bool getEventGasResistance(sensors_event_t *gasEvent) {
166+
if (!bmePerformReading())
167+
return false;
168+
// NOTE: This is hacked onto Adafruit_Sensor and should eventually be
169+
// removed
170+
gasEvent->data[0] = (float)_bme->gas_resistance;
171+
return true;
172+
}
173+
174+
protected:
175+
Adafruit_BME680 *_bme; ///< BME680 object
176+
};
177+
178+
#endif // WipperSnapper_I2C_Driver_BME680

src/components/i2c/drivers/WipperSnapper_I2C_Driver_TSL2591.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ class WipperSnapper_I2C_Driver_TSL2591 : public WipperSnapper_I2C_Driver {
5454
*/
5555
/*******************************************************************************/
5656
bool begin() {
57-
_tsl = new Adafruit_TSL2591();
57+
_tsl = new Adafruit_TSL2591(2591);
5858
// Attempt to initialize TSL2591
59-
if (!_tsl->begin())
59+
if (!_tsl->begin(_i2c, TSL2591_ADDR))
6060
return false;
6161

6262
// Configure TSL2591 sensor

0 commit comments

Comments
 (0)