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); pinMode(BME_CS, OUTPUT);
// digitalWrite(BME_CS, LOW); // enable bme280 CS // digitalWrite(BME_CS, LOW); // enable bme280 CS
int retries = 0; valid = false; valid = false;
while (!valid) { for (int retries=0; !valid && retries<5; retries++) {
if (retries > 9) break;
/* reset chip */ /* reset chip */
Serial.println("resetting BME280"); Serial.println("resetting BME280");
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
@ -65,9 +64,7 @@ bool BME280::reinit() {
valid = bme->init(); valid = bme->init();
retries++; retries++;
float p = bme->readPressure() / 100.0F; checkValid();
if (p < 300 || p > 1100) valid = false;
} }
if (!valid) { if (!valid) {
Serial.println("Could not find a valid BME280 sensor, check wiring!"); Serial.println("Could not find a valid BME280 sensor, check wiring!");
@ -114,3 +111,8 @@ float BME280::readHumidity(void) {
else valid = true; else valid = true;
return h; return h;
} }
bool BME280::checkValid(void) {
readPressure();
return valid;
}

1
src/BME280.h

@ -23,6 +23,7 @@ class BME280 {
float readTemperature(void); float readTemperature(void);
float readPressure(void); float readPressure(void);
float readHumidity(void); float readHumidity(void);
bool checkValid(void);
void sleep(void); void sleep(void);
bool valid; bool valid;
private: private:

14
src/main.cpp

@ -86,6 +86,8 @@ String sensorTopic;
const IPAddress udpMulticastAddress(239,255,255,245); const IPAddress udpMulticastAddress(239,255,255,245);
const uint16_t udpMulticastPort = 5555; const uint16_t udpMulticastPort = 5555;
void setup() { void setup() {
// gpio configuration // gpio configuration
// rtc_gpio_deinit((gpio_num_t)buttonPin); // rtc_gpio_deinit((gpio_num_t)buttonPin);
@ -282,14 +284,12 @@ void transmitStatus() {
float internal_temp = temperatureRead(); float internal_temp = temperatureRead();
int internal_hall = hallRead(); int internal_hall = hallRead();
if (!bme280.valid) return; // TODO if (!bme280.valid) {
bme280.reinit(); // TODO
for (int retry=0; retry<3 && !bme280.valid; retry++) { if (!bme280.valid) return;
bme280.reinit();
delay(250);
humi = bme280.readHumidity();
bme280.readPressure();
temp = bme280.readTemperature(); temp = bme280.readTemperature();
humi = bme280.readHumidity();
pres = bme280.readPressure();
} }
StaticJsonBuffer<200> jsonBuffer; StaticJsonBuffer<200> jsonBuffer;

Loading…
Cancel
Save