Browse Source

bme280

main
Hendrik Langer 7 years ago
parent
commit
fabe921cda
  1. 43
      src/BME280.cpp
  2. 2
      src/BME280.h
  3. 14
      src/main.cpp

43
src/BME280.cpp

@ -22,13 +22,33 @@ bool BME280::begin(void) {
if (retries > 5) break; if (retries > 5) break;
retries++; retries++;
Serial.println("Could not find a valid BME280 sensor, check wiring!"); Serial.println("Could not find a valid BME280 sensor, check wiring!");
// bme->reset();
delay(500); delay(500);
} }
if (!valid) return false; if (!valid) return false;
bme->setSampling(Adafruit_BME280::sensor_mode::MODE_NORMAL,
Adafruit_BME280::sensor_sampling::SAMPLING_X16,
Adafruit_BME280::sensor_sampling::SAMPLING_X16,
Adafruit_BME280::sensor_sampling::SAMPLING_X16,
Adafruit_BME280::sensor_filter::FILTER_OFF,
Adafruit_BME280::standby_duration::STANDBY_MS_250);
return true; return true;
} }
void BME280::sleep() {
bme->setSampling(Adafruit_BME280::sensor_mode::MODE_SLEEP,
Adafruit_BME280::sensor_sampling::SAMPLING_X16,
Adafruit_BME280::sensor_sampling::SAMPLING_X16,
Adafruit_BME280::sensor_sampling::SAMPLING_X16,
Adafruit_BME280::sensor_filter::FILTER_OFF,
Adafruit_BME280::standby_duration::STANDBY_MS_1000);
}
void BME280::init() {
bme->init();
}
void BME280::printValues() { void BME280::printValues() {
Serial.print("Temperature = "); Serial.print("Temperature = ");
Serial.print(bme->readTemperature()); Serial.print(bme->readTemperature());
@ -50,6 +70,21 @@ void BME280::printValues() {
Serial.println(); Serial.println();
} }
float BME280::readTemperature(void) { return bme->readTemperature(); } float BME280::readTemperature(void) {
float BME280::readPressure(void) { return bme->readPressure() / 100.0F; } float temp = bme->readTemperature();
float BME280::readHumidity(void) { return bme->readHumidity(); } if (temp < -40 || temp > 85) valid = false;
else valid = true;
return temp;
}
float BME280::readPressure(void) {
float p = bme->readPressure() / 100.0F;
if (p < 300 || p > 1100) valid = false;
else valid = true;
return p;
}
float BME280::readHumidity(void) {
float h = bme->readHumidity();
if (h < 0 || h > 100) valid = false;
else valid = true;
return h;
}

2
src/BME280.h

@ -18,10 +18,12 @@ class BME280 {
public: public:
BME280(); BME280();
bool begin(void); bool begin(void);
void init(void);
void printValues(void); void printValues(void);
float readTemperature(void); float readTemperature(void);
float readPressure(void); float readPressure(void);
float readHumidity(void); float readHumidity(void);
void sleep(void);
bool valid; bool valid;
private: private:
Adafruit_BME280 *bme; Adafruit_BME280 *bme;

14
src/main.cpp

@ -194,6 +194,19 @@ void onMqttConnect(bool sessionPresent) {
void transmitStatus() { void transmitStatus() {
DEBUG_PRINTLN(__func__); DEBUG_PRINTLN(__func__);
float temp = bme280.readTemperature();
float humi = bme280.readHumidity();
float pres = bme280.readPressure();
for (int retry=0; retry<3 && !bme280.valid; retry++) {
bme280.init();
delay(250);
humi = bme280.readHumidity();
bme280.readPressure();
temp = bme280.readTemperature();
}
StaticJsonBuffer<200> jsonBuffer; StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject(); JsonObject& root = jsonBuffer.createObject();
root["temperature"] = bme280.readTemperature(); root["temperature"] = bme280.readTemperature();
@ -229,6 +242,7 @@ void suspend(uint32_t secondsToSleep) {
mp3.stop(); mp3.stop();
led.stop(); led.stop();
bme280.sleep();
delay(1000); delay(1000);
u8g2.setPowerSave(1); u8g2.setPowerSave(1);

Loading…
Cancel
Save