diff --git a/src/BME280.cpp b/src/BME280.cpp index 7445ebc..80a5de2 100644 --- a/src/BME280.cpp +++ b/src/BME280.cpp @@ -22,13 +22,33 @@ bool BME280::begin(void) { if (retries > 5) break; retries++; Serial.println("Could not find a valid BME280 sensor, check wiring!"); -// bme->reset(); delay(500); } 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; } +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() { Serial.print("Temperature = "); Serial.print(bme->readTemperature()); @@ -50,6 +70,21 @@ void BME280::printValues() { Serial.println(); } -float BME280::readTemperature(void) { return bme->readTemperature(); } -float BME280::readPressure(void) { return bme->readPressure() / 100.0F; } -float BME280::readHumidity(void) { return bme->readHumidity(); } +float BME280::readTemperature(void) { + float temp = bme->readTemperature(); + 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; +} diff --git a/src/BME280.h b/src/BME280.h index acb89cf..4f1f61d 100644 --- a/src/BME280.h +++ b/src/BME280.h @@ -18,10 +18,12 @@ class BME280 { public: BME280(); bool begin(void); + void init(void); void printValues(void); float readTemperature(void); float readPressure(void); float readHumidity(void); + void sleep(void); bool valid; private: Adafruit_BME280 *bme; diff --git a/src/main.cpp b/src/main.cpp index 7601004..86f8747 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -194,6 +194,19 @@ void onMqttConnect(bool sessionPresent) { void transmitStatus() { 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; JsonObject& root = jsonBuffer.createObject(); root["temperature"] = bme280.readTemperature(); @@ -229,6 +242,7 @@ void suspend(uint32_t secondsToSleep) { mp3.stop(); led.stop(); + bme280.sleep(); delay(1000); u8g2.setPowerSave(1);