Browse Source

cleanup bme280 retry

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

14
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;
}

1
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:

14
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;

Loading…
Cancel
Save