float variable getting low value with BME280

I have built a home thermostat with a BME280 sensor module.
I make use of an LCD to print the current temp and change my target temp with a rotary encoder.
I take temp measurements every 2 seconds and update the lcd characters every 200ms.

This is the code that reads the bme280 temp.

void readBME280(){
	bme.takeForcedMeasurement();
	temperature = bme.readTemperature();
	if (!((temperature >= 0) && (temperature <= 35))) error = 1;
}

If the temperature is out of the minimum and maximum acceptable values I set “error = 1”. If so I stop heating process and I print the “problematic” measurement on the lcd.
Here are parts of my loop() and updateLCD() functions.

loop()

currentMillis = millis();

if (!error){
	if ((unsigned long)(currentMillis - previousMillis) >= 2000) {
		previousMillis = currentMillis;
		readBME280();
	}
}
else // stop heating
.
.
.
if ((unsigned long)(currentMillis - previousLCDMillis) >= 200) {
	previousLCDMillis = currentMillis;
	updateLCD();
}
.
.
.

updateLCD()

void updateLCD(){
	if (!error){
		//normal operation
		.
		.
		.
	}
	else{
		//print "error:"
		//print temperature
	}
}

I use these libraries

#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <U8g2lib.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <EEPROM.h>

After a few days of operation I got an error (error = 1) which means it got an unacceptable temp measurement. On the screen I saw a measurement = -214748368.0
That used to happen more often while I was testing my code and I noticed I had set the temperature variable as double. I changed it to float (like it was in Adafruit_BME280 examples) and I thought it got fixed but the problem appeared again after ~1week continuous operation.

What could cause this? What could -214748368.0 mean?
Could this mean it wasn’t able to communicate with the sensor?

Post all of your code - too hard to deduce anything from snippets. 214748368 is a suspicious looking number - looks like a maxed out (or perhaps minned out) 32 bit int.

I didn’t post my complete code because it’s over 1000 lines and the most of it is unrelevant to my problem.
I removed everything unrelevant to temp measurements from my code and tried this minimized code which appears to have the same issue.

#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <U8g2lib.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <EEPROM.h>

#define ON 1
#define OFF 0

float temperature = 0;

float target = 22.0;

int error = 0;

int heating = OFF;

unsigned long currentMillis;
unsigned long previousMillis;
unsigned long previousLCDMillis;

Adafruit_BME280 bme;

const int relay = 5;
const int backlightPin = 6;

U8G2_ST7565_64128N_F_4W_SW_SPI u8g2(
	U8G2_MIRROR,
	/* clock=*/ 13,
	/* data=*/ 11,
	/* cs=*/ 10,
	/* dc=*/ 9,
	/* reset=*/ 8);

void setup(void) {

	pinMode(relay, OUTPUT);
	digitalWrite(relay, HIGH);


	pinMode(backlightPin, OUTPUT);
	digitalWrite(backlightPin, HIGH);

	u8g2.begin();
	u8g2.setContrast(170);


	bool status;
	status = bme.begin(0x76);  //The I2C address of the sensor I use is 0x76
	bme.setSampling(Adafruit_BME280::MODE_FORCED,
		Adafruit_BME280::SAMPLING_X1, // temperature
		Adafruit_BME280::SAMPLING_X1, // pressure
		Adafruit_BME280::SAMPLING_X1, // humidity
		Adafruit_BME280::FILTER_OFF,
		Adafruit_BME280::STANDBY_MS_1000);

	currentMillis = millis();
}

void loop() {

	currentMillis = millis();


	if (!error){
		if ( ! heating ){
			if (target > temperature){
				heatingON();
			}
		}
		else{
			if (target < temperature){
				heatingOFF();
			}
		}

		if ((unsigned long)(currentMillis - previousMillis) >= 2000) {
			previousMillis = currentMillis;
			readBME280();
		}

	}
	else heatingOFF();

	if ((unsigned long)(currentMillis - previousLCDMillis) >= 200) {
		previousLCDMillis = currentMillis;
		updateLCD();
	}
}

void heatingON(){
	heating = ON;
	digitalWrite(relay, LOW);
}

void heatingOFF(){
	heating = OFF;
	digitalWrite(relay, HIGH);
}


void readBME280(){
	bme.takeForcedMeasurement();
	temperature = bme.readTemperature();
	if (!((temperature >= 0) && (temperature <= 35))) error = 1;
}

void updateLCD(){

	u8g2.clearBuffer();	
	u8g2.setFont(u8g2_font_freedoomr25_tn);
	u8g2.setFontMode(1);
	u8g2.setDrawColor(1);
	if (!error){
		u8g2.setCursor(2,29);
		u8g2.print(temperature,1);
		u8g2.setCursor(2,63);
		u8g2.print(target,1);
	}
	else{
		u8g2.setCursor(2,29);
		u8g2.print(temperature,1);
		u8g2.drawStr(2,63,"ERROR!");
	}

	u8g2.sendBuffer();
}

I’m pretty sure that -214748368.0 is a minned out value but I don’t know how the Adafruit_BME280 library reads data from my sensor and why it returns that minned out value.
I’d like to know if it could be a hardware related issue. Could this mean that it doesn’t find a connected sensor?

Please, be specific -- BME280 is a Pressure/Temperature/Humidity sensor. Do you have problem to read the values of this sensor using Adafruit_BME280.h Library?

There is a possibility that readTemperature will return NAN. You should probably check for it.

GolamMostafa:
Please, be specific -- BME280 is a Pressure/Temperature/Humidity sensor. Do you have problem to read the values of this sensor using Adafruit_BME280.h Library?

Yes. It was happening very often when temperature variable was double. I set it to float and it happened again after 1 week continuous operation. I don't know if the change from double to float did any difference.

wildbill:
There is a possibility that readTemperature will return NAN. You should probably check for it.

What would it print if it returns NAN? How could I check it since I don't know how to trigger that issue?

You can set a float to NAN and try printing it on your display. I'm wondering if that's where the 214748368 comes from although on my LED display, it just shows as "nan". To check it, use isnan().

rojoiko:
Yes. It was happening very often when temperature variable was double. I set it to float and it happened again after 1 week continuous operation. I don't know if the change from double to float did any difference.

Are you using UNO or NANO or MEGA or DUE? For DUE, double is 64-bit floating point format (binary64) and float is 32-bit format (binary32). For UNO/NANO/MEGA double and float are the same, and it is 32-bit format (binary32).

wildbill:
You can set a float to NAN and try printing it on your display. I’m wondering if that’s where the 214748368 comes from although on my LED display, it just shows as “nan”. To check it, use isnan().

Thanks for your suggestions.
I set another variable to NAN and printed it. It shows “nan” on the screen.

The readTemperature() in the library is this

float Adafruit_BME280::readTemperature(void)
{
    int32_t var1, var2;

    int32_t adc_T = read24(BME280_REGISTER_TEMPDATA);
    if (adc_T == 0x800000) // value in case temp measurement was disabled
        return NAN;
    adc_T >>= 4;

    var1 = ((((adc_T>>3) - ((int32_t)_bme280_calib.dig_T1 <<1))) *
            ((int32_t)_bme280_calib.dig_T2)) >> 11;
             
    var2 = (((((adc_T>>4) - ((int32_t)_bme280_calib.dig_T1)) *
              ((adc_T>>4) - ((int32_t)_bme280_calib.dig_T1))) >> 12) *
            ((int32_t)_bme280_calib.dig_T3)) >> 14;

    t_fine = var1 + var2;

    float T = (t_fine * 5 + 128) >> 8;
    return T/100;
}

I removed everything in this function and kept the “return NAN” so it will always return NAN.
It actually printed -214748368.0 on the screen.

I don’t know why it prints NAN and -214748368.0 in these two cases.

I will check the readings with isnan() and I will discard them if they are not acceptable. I will also count how many of the readings where discarded and print that value on the screen to see how often it happens. It’s so rare (1 time in the last week) that I cannot replicate it for now.

(forgot to mention I’m using a nano board)