CanGrow/include/Sensor/03_BME680.h

98 lines
3.3 KiB
C

/*
*
* include/Sensor/00_ADC_builtin.h - sensor header for BME680 I2C sensor
*
*
*
*/
#include <Adafruit_BME680.h>
#define SENSOR_03_NAME "BME680"
/* available addresses in byte array, default is at 0 */
const byte Sensor_03_BME680_Addr[] = { 0x77, 0x76 };
Adafruit_BME680 BME680[sizeof(Sensor_03_BME680_Addr)];
unsigned long BME680_endtime[sizeof(Sensor_03_BME680_Addr)];
/*struct Sensor_03_BME680 {
float humidity;
float temperature;
float pressure;
float altitude;
float gas_resistance;
};
*/
/* creation of BME680 Value Struct, as many as addresses */
/*Sensor_03_BME680 Sensor_03_BME680_Data[sizeof(Sensor_03_BME680_Addr)];*/
float Sensor_03_BME680[sizeof(Sensor_03_BME680_Addr)][5];
/* for async read of BME680 we need to trigger a new reading cycle (as adafruit doc says) */
void Sensor_03_BME680_BeginReading(const byte AddrId) {
const static char LogLoc[] PROGMEM = "[Sensor:03_BME680:BeginReading]";
#ifdef DEBUG3
Log.warning(F("%s Start reading %u , finishing %u (0x%x)" CR), LogLoc, millis(), BME680_endtime[AddrId], Sensor_03_BME680_Addr[AddrId]);
#endif
// Tell BME680 to begin measurement.
BME680_endtime[AddrId] = BME680[AddrId].beginReading();
if(BME680_endtime[AddrId] == 0) {
Log.warning(F("%s Failed to begin reading (0x%x)" CR), LogLoc, Sensor_03_BME680_Addr[AddrId]);
}
}
void Sensor_03_BME680_Update(const byte AddrId) {
const static char LogLoc[] PROGMEM = "[Sensor:03_BME680:Update]";
#ifdef DEBUG3
Log.warning(F("%s Start reading %u , finishing %u (0x%x)" CR), LogLoc, millis(), BME680_endtime[AddrId], Sensor_03_BME680_Addr[AddrId]);
#endif
if(!BME680[AddrId].endReading()) {
Log.warning(F("%s Failed to complete reading (0x%x)" CR), LogLoc, Sensor_03_BME680_Addr[AddrId]);
return;
}
Sensor_03_BME680[AddrId][0] = BME680[AddrId].readTemperature();
Sensor_03_BME680[AddrId][1] = BME680[AddrId].readHumidity();
Sensor_03_BME680[AddrId][2] = BME680[AddrId].readPressure() / 1000;
Sensor_03_BME680[AddrId][3] = BME680[AddrId].readAltitude(SEALEVELPRESSURE_HPA);
Sensor_03_BME680[AddrId][4] = BME680[AddrId].gas_resistance / 1000.0;
/* begin new reading cycle */
Sensor_03_BME680_BeginReading(AddrId);
}
bool Sensor_03_BME680_Init(const byte AddrId) {
const static char LogLoc[] PROGMEM = "[Sensor:03_BME680:Init]";
bool returnCode;
//Log.notice(F("%s Init at addr 0x%x (%d)" CR), LogLoc, Sensor_03_BME680_Addr[AddrId], AddrId);
if(BME680[AddrId].begin(Sensor_03_BME680_Addr[AddrId])) {
Log.notice(F("%s found at addr 0x%x" CR), LogLoc, Sensor_03_BME680_Addr[AddrId]);
// Set up oversampling and filter initialization
BME680[AddrId].setTemperatureOversampling(BME680_OS_8X);
BME680[AddrId].setHumidityOversampling(BME680_OS_2X);
BME680[AddrId].setPressureOversampling(BME680_OS_4X);
BME680[AddrId].setIIRFilterSize(BME680_FILTER_SIZE_3);
BME680[AddrId].setGasHeater(320, 150); // 320*C for 150 ms
/* start to do readings here, like shown in async example
* https://github.com/adafruit/Adafruit_BME680/blob/master/examples/bme680async/bme680async.ino */
Sensor_03_BME680_BeginReading(AddrId);
Sensor_03_BME680_Update(AddrId);
returnCode = true;
} else {
Log.error(F("%s FAILED! Not found at addr 0x%x" CR), LogLoc, Sensor_03_BME680_Addr[AddrId]);
returnCode = false;
}
return returnCode;
}