From a29656a3cbfae1ac6586362275bfbceb4e6d95de Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Mon, 11 Jun 2018 14:31:05 +0200 Subject: [PATCH] cleanup bme280 retry --- src/BME280.cpp | 14 ++++++++------ src/BME280.h | 1 + src/main.cpp | 14 +++++++------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/BME280.cpp b/src/BME280.cpp index b13865e..9273e7e 100644 --- a/src/BME280.cpp +++ b/src/BME280.cpp @@ -48,9 +48,8 @@ bool BME280::reinit() { pinMode(BME_CS, OUTPUT); // digitalWrite(BME_CS, LOW); // enable bme280 CS - int retries = 0; valid = false; - while (!valid) { - if (retries > 9) break; + valid = false; + for (int retries=0; !valid && retries<5; retries++) { /* reset chip */ Serial.println("resetting BME280"); SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); @@ -65,9 +64,7 @@ bool BME280::reinit() { valid = bme->init(); retries++; - float p = bme->readPressure() / 100.0F; - if (p < 300 || p > 1100) valid = false; - + checkValid(); } if (!valid) { Serial.println("Could not find a valid BME280 sensor, check wiring!"); @@ -114,3 +111,8 @@ float BME280::readHumidity(void) { else valid = true; return h; } + +bool BME280::checkValid(void) { + readPressure(); + return valid; +} diff --git a/src/BME280.h b/src/BME280.h index fe48037..4f0f346 100644 --- a/src/BME280.h +++ b/src/BME280.h @@ -23,6 +23,7 @@ class BME280 { float readTemperature(void); float readPressure(void); float readHumidity(void); + bool checkValid(void); void sleep(void); bool valid; private: diff --git a/src/main.cpp b/src/main.cpp index 7a95205..4f0b484 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -86,6 +86,8 @@ String sensorTopic; const IPAddress udpMulticastAddress(239,255,255,245); const uint16_t udpMulticastPort = 5555; + + void setup() { // gpio configuration // rtc_gpio_deinit((gpio_num_t)buttonPin); @@ -282,14 +284,12 @@ void transmitStatus() { float internal_temp = temperatureRead(); int internal_hall = hallRead(); - if (!bme280.valid) return; // TODO - - for (int retry=0; retry<3 && !bme280.valid; retry++) { - bme280.reinit(); - delay(250); - humi = bme280.readHumidity(); - bme280.readPressure(); + if (!bme280.valid) { + bme280.reinit(); // TODO + if (!bme280.valid) return; temp = bme280.readTemperature(); + humi = bme280.readHumidity(); + pres = bme280.readPressure(); } StaticJsonBuffer<200> jsonBuffer;