Created
February 5, 2023 18:20
-
-
Save dennisdebel/b8d17fe8ec333ccdcc1243920ed0e467 to your computer and use it in GitHub Desktop.
Snippet to reset bme280 sensor on erroneous readings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void BME280_Reset() | |
{ | |
if(DEBUG) Serial.println(F("BME280 Reset")); | |
Wire.beginTransmission(BME280_I2C_ADDR); | |
Wire.write((uint8_t)BME280_REGISTER_SOFTRESET); | |
Wire.write((uint8_t)0xB6); | |
Wire.endTransmission(); | |
delay(1000); | |
} | |
########################################################## | |
// Start with temperature, as that data is needed for accurate compensation. | |
// Reading the temperature updates the compensators of the other functions in the background. | |
scratchfloat = bme.readTemperature(); | |
if(isnan(scratchfloat) || (scratchfloat == 0)) | |
{ | |
scratchfloat = temperature; // Just use last value instead to prevent perpetual NaN | |
sendStatus(LoRaDevice " Temperature Reboot."); | |
resetNeeded = true; | |
} | |
temperature += scratchfloat; | |
temperature = temperature / 2; | |
delay(1); | |
scratchfloat = bme.readPressure() / 100.0F; | |
if(isnan(scratchfloat) || (scratchfloat == 0)) | |
{ | |
scratchfloat = pressure; // Just use last value instead | |
sendStatus(LoRaDevice " Pressure Reboot."); | |
resetNeeded = true; | |
} | |
pressure += scratchfloat; | |
pressure = pressure / 2; | |
delay(1); | |
scratchfloat = bme.readAltitude(SEALEVELPRESSURE_HPA); | |
if(isnan(scratchfloat) || (scratchfloat == 0)) | |
{ | |
scratchfloat = altitude; // Just use last value instead | |
sendStatus(LoRaDevice " Altitude Reboot."); | |
resetNeeded = true; | |
} | |
altitude += scratchfloat; | |
altitude = altitude / 2; | |
delay(1); | |
scratchfloat = bme.readHumidity(); | |
if(isnan(scratchfloat) || (scratchfloat == 0)) | |
{ | |
scratchfloat = humidity; // Just use last value instead | |
sendStatus(LoRaDevice " Humidity Reboot."); | |
resetNeeded = true; | |
} | |
humidity += scratchfloat; | |
humidity = humidity / 2; | |
if(resetNeeded) | |
{ | |
BME280_Reset(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By user 'Riva' on the arduino forums. Original post here.