From 2151d06ad77bcee604c1636f6320ad96beddf18e Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Wed, 7 Feb 2018 22:57:53 +0100 Subject: [PATCH] don't publish if bme280 not set up --- src/BME280.cpp | 8 +++++--- src/BME280.h | 2 +- src/main.cpp | 3 ++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/BME280.cpp b/src/BME280.cpp index 0b4f556..7445ebc 100644 --- a/src/BME280.cpp +++ b/src/BME280.cpp @@ -5,7 +5,9 @@ #include #include -BME280::BME280() {} +BME280::BME280() { + valid = false; +} bool BME280::begin(void) { // SPI.begin(BME_SCK, BME_MISO, BME_MOSI, BME_CS); @@ -16,14 +18,14 @@ bool BME280::begin(void) { delay(50); bme = new Adafruit_BME280(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI int retries = 0; - while (!(status = bme->begin())) { + while (!(valid = bme->begin())) { if (retries > 5) break; retries++; Serial.println("Could not find a valid BME280 sensor, check wiring!"); // bme->reset(); delay(500); } - if (!status) return false; + if (!valid) return false; return true; } diff --git a/src/BME280.h b/src/BME280.h index 31e77e0..acb89cf 100644 --- a/src/BME280.h +++ b/src/BME280.h @@ -22,9 +22,9 @@ class BME280 { float readTemperature(void); float readPressure(void); float readHumidity(void); + bool valid; private: Adafruit_BME280 *bme; - bool status; }; #endif /* _BME280_H */ diff --git a/src/main.cpp b/src/main.cpp index e7e4065..7601004 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -202,7 +202,8 @@ void transmitStatus() { char sensorBuf[root.measureLength()+1]; root.printTo(sensorBuf, sizeof(sensorBuf)); - uint16_t statusPacketIdSub = iot.mqtt.publish(bme280Topic.c_str(), 0, false, sensorBuf); + uint16_t statusPacketIdSub; + if (bme280.valid) statusPacketIdSub = iot.mqtt.publish(bme280Topic.c_str(), 0, false, sensorBuf); // statusPacketIdSub = iot.mqtt.publish(bme280Topic.c_str(), 1, true, sensorBuf); int voltage = rom_phy_get_vdd33(); char batteryBuf[9];