Browse Source

don't publish if bme280 not set up

main
Hendrik Langer 7 years ago
parent
commit
2151d06ad7
  1. 8
      src/BME280.cpp
  2. 2
      src/BME280.h
  3. 3
      src/main.cpp

8
src/BME280.cpp

@ -5,7 +5,9 @@
#include <Adafruit_Sensor.h> #include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h> #include <Adafruit_BME280.h>
BME280::BME280() {} BME280::BME280() {
valid = false;
}
bool BME280::begin(void) { bool BME280::begin(void) {
// SPI.begin(BME_SCK, BME_MISO, BME_MOSI, BME_CS); // SPI.begin(BME_SCK, BME_MISO, BME_MOSI, BME_CS);
@ -16,14 +18,14 @@ bool BME280::begin(void) {
delay(50); delay(50);
bme = new Adafruit_BME280(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI bme = new Adafruit_BME280(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
int retries = 0; int retries = 0;
while (!(status = bme->begin())) { while (!(valid = bme->begin())) {
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(); // bme->reset();
delay(500); delay(500);
} }
if (!status) return false; if (!valid) return false;
return true; return true;
} }

2
src/BME280.h

@ -22,9 +22,9 @@ class BME280 {
float readTemperature(void); float readTemperature(void);
float readPressure(void); float readPressure(void);
float readHumidity(void); float readHumidity(void);
bool valid;
private: private:
Adafruit_BME280 *bme; Adafruit_BME280 *bme;
bool status;
}; };
#endif /* _BME280_H */ #endif /* _BME280_H */

3
src/main.cpp

@ -202,7 +202,8 @@ void transmitStatus() {
char sensorBuf[root.measureLength()+1]; char sensorBuf[root.measureLength()+1];
root.printTo(sensorBuf, sizeof(sensorBuf)); 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); // statusPacketIdSub = iot.mqtt.publish(bme280Topic.c_str(), 1, true, sensorBuf);
int voltage = rom_phy_get_vdd33(); int voltage = rom_phy_get_vdd33();
char batteryBuf[9]; char batteryBuf[9];

Loading…
Cancel
Save