100 lines
2.7 KiB
C++
100 lines
2.7 KiB
C++
#pragma once
|
|
|
|
#if ADAFRUIT_BME
|
|
|
|
#include <Adafruit_BME280.h>
|
|
|
|
namespace Bme {
|
|
|
|
Adafruit_BME280 bme; // I2C Interface
|
|
|
|
struct {
|
|
float temp;
|
|
float humidity;
|
|
float pressure;
|
|
|
|
void readAll() {
|
|
bme.takeForcedMeasurement();
|
|
temp = roundFloat(bme.readTemperature());
|
|
humidity = roundFloat(bme.readHumidity());
|
|
pressure = bme.readPressure() / 100;
|
|
}
|
|
|
|
private:
|
|
float roundFloat(float value) {
|
|
char buf[10];
|
|
snprintf(buf, sizeof(buf), "%.1f", value);
|
|
return atof(buf);
|
|
}
|
|
} data;
|
|
|
|
void setup() {
|
|
Serial.println(F("BME280 setup"));
|
|
|
|
if (!bme.begin(BME280_ADDRESS_ALTERNATE, &Wire)) {
|
|
Serial.println(F("Could not find a valid BME280 sensor, check wiring!"));
|
|
return;
|
|
}
|
|
|
|
/* Settings from examples: advancedsettings.ino */
|
|
// -- Weather Station Scenario --
|
|
Serial.println("-- Weather Station Scenario --");
|
|
bme.setSampling(Adafruit_BME280::MODE_FORCED, /* Operating Mode. */
|
|
Adafruit_BME280::SAMPLING_X1, // temperature
|
|
Adafruit_BME280::SAMPLING_X1, // pressure
|
|
Adafruit_BME280::SAMPLING_X1, // humidity
|
|
Adafruit_BME280::FILTER_OFF);
|
|
}
|
|
}
|
|
#else
|
|
|
|
#include <SparkFunBME280.h>
|
|
|
|
namespace Bme {
|
|
|
|
BME280 bme; // I2C Interface
|
|
bool initialised = false;
|
|
|
|
struct {
|
|
float temp;
|
|
float humidity;
|
|
float pressure;
|
|
|
|
void readAll() {
|
|
if (!initialised) return;
|
|
// bme.takeForcedMeasurement();
|
|
temp = roundFloat(bme.readTempC());
|
|
humidity = roundFloat(bme.readFloatHumidity());
|
|
pressure = bme.readFloatPressure();
|
|
}
|
|
|
|
private:
|
|
float roundFloat(float value) {
|
|
char buf[10];
|
|
snprintf(buf, sizeof(buf), "%.1f", value);
|
|
return atof(buf);
|
|
}
|
|
} data;
|
|
|
|
void setup() {
|
|
Serial.println(F("BME280 setup"));
|
|
|
|
bme.setI2CAddress(0x76);
|
|
if (!bme.beginI2C()) {
|
|
Serial.println(F("Could not find a valid BME280 sensor, check wiring!"));
|
|
return;
|
|
}
|
|
initialised = true;
|
|
|
|
/* Settings from examples: advancedsettings.ino */
|
|
// -- Weather Station Scenario --
|
|
// Serial.println("-- Weather Station Scenario --");
|
|
// bme.setSampling(Adafruit_BME280::MODE_FORCED, /* Operating Mode. */
|
|
// Adafruit_BME280::SAMPLING_X1, // temperature
|
|
// Adafruit_BME280::SAMPLING_X1, // pressure
|
|
// Adafruit_BME280::SAMPLING_X1, // humidity
|
|
// Adafruit_BME280::FILTER_OFF);
|
|
}
|
|
}
|
|
#endif
|