Check the BME280 in every loop

Hello,

I need to add a sensor check in every loop to be sure that the printed values actually are read from the sensor.
Because if I disconnect the sensor while looping, it still loops and sends the latest read values.
I tried what I thought was logical, but it didn’t really help, just generated an error code.
I would still like the Loop to continue, but is should be clear that the values aren’t from the sensor.

#include <Wire.h>
#include "cactus_io_BME280_I2C.h"

// Create BME280 object
BME280_I2C bme; // I2C using address 0x77
// or BME280_I2C bme(0x76); // I2C using address 0x76

void setup() {

Serial.begin(9600);
Serial.println("Bosch BME280 Pressure - Humidity - Temp Sensor | cactus.io");

if (!bme.begin()) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}

bme.setTempCal(-1);// Temp was reading high so subtract 1 degree

Serial.println("Pressure\tHumdity\t\tTemp\ttTemp");
}

void loop() {

bme.readSensor();

if (!bme.readSensor()) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
}
Else {

//int BaseLog = 2.71828;
float Temperature = bme.getTemperature_C();
float Humidity = bme.getHumidity();
float AbsoluteHumidity = (6.112 * pow(2.71828,((17.67 * Temperature)/(Temperature + 243.5))) * Humidity * 2.1674) / (273.15 + Temperature);
Serial.print(AbsoluteHumidity); Serial.print("\t"); Serial.print(Temperature); Serial.print("\t"); Serial.println(Humidity); Serial.print("\t"); Serial.println(bme.getPressure_MB());


// Add a 2 second delay.
delay(20000); //just here to slow down the output.
}
}

Errors are:

exit status 1
could not convert ‘bme.BME280_I2C::readSensor()’ from ‘void’ to ‘bool’

The error message would seem to indicate that bme.readSensor() is a void function and so does not return a value.

What value do you get from bme.getTemperature_C() if the sensor is not connected ?

Thanks for your reply. It totally depends on how Iinterupt the sensor. Can I call using the my float Temperature, or must it be exactly bme.getTemperature_C()?

Below are using the code above without the !bme.readSensor code snippet.

Theese are reference values if the sensor is working as should: 9.61 g/m3 24.96 C 41.82 % RH 1016.33 millaBar

  • If I disconnect the VCC; 12.92 g/m3 21.16 C 69.90 % RH 568.77 milliBar The loop continue and shows theese results.

  • If I disconnect the GND; 0.00 g/m3 -142.32 C 100.00 % RH 1073.76 milliBar The loop continue and shows theese results.

  • If I disconnect I2C pin A4, it gives the same result as disconnecting the GND

  • If I disconnect I2C pin A5, same as above

  • If I disconnect all pins, it loops the last known value until eternity.

The values don't get normal when I reconnect VCC or GND, I must reset/reboot the Arduino. But the I2C, the values get to normal again.