My problem is that BMP280 starts to show wrong barometer readings after running one day. After rebooting Nano it is working fine again but around 16 to 24 hours it starts again to show wrong readings.
Readings are higher than they should be. The right values are normally between 1000-1030 hPa and BMP280 starts give wrong readings around from 1045 to 1080 hPa.
The sensor is read in every 10th second.
I think I should somehow initialiaze the sensor between readings in the code but trying to figure it out from the library code was over my understanding. I use I2C-Sensor-Lib_iLib library to read the sensor.
Can someone give any hint how to try solve this problem?
There is also DS3231 Real time clock, DHT22 temperature sensor and SSD1306 Oled display connected to this, but as far as I understand they should not have any effect on how the BMP280 is working.
Code for the project.
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
// Credits for rtclib to : Code by JeeLabs http://news.jeelabs.org/code/ Released to the public domain! Enjoy!
// Converted so that RTC uses UTC time instead of the local time
// Scion Riverson 2018
// I2C
#include <Wire.h>
//Clock
#include "RTClib.h"
RTC_DS3231 rtc; // create RTC object
//Display
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(-1);
#if (SSD1306_LCDHEIGHT != 64)
#error ("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//BMP280 sensor
#include "i2c.h"
#include "i2c_BMP280.h"
BMP280 bmp280;
//DHT22 anturi
#include <SimpleDHT.h>
// for DHT22,
// VCC: 5V or 3V
// GND: GND
// DATA: 2
int pinDHT22 = 2;
SimpleDHT22 dht22(pinDHT22);
// CLOCK MUST BE SET IN CORRECT TIME SEPARATELY
// timezone offsets in seconds for West European Time / West European Summer Time WET/WEST
// change these to your own timezone
// more information available in linux with command: zdump -v timezone
// for example : zdump -v Atlantic/Canary
// Check functions is_Dls_On() (Is Daylight Saving On), getUtcTime() and getLocalTime() later on this code
// these timezone settings must be set correctly for your timezone when you want to setup RTC time to UTC automatically from computer
// 1 hour = 3600 seconds
// timezone offset for Atlantic/Canary
unsigned long dls_On_Offset = 3600; // Summer time offset in seconds to UTC time when Day light saving is On
unsigned long dls_Off_Offset = 0; // Winter time offset in seconds to UTC time when Day light saving is Off
char strDate[11]; //date string
char strTime[9]; //time string
char strTemp[9]; //temp & press strings
char strPres[9]; //
char strHumi[9];
void setup() {
Serial.begin(9600); // open serial connection
delay(500); // wait for console opening
if (! rtc.begin()) { // rtc.begin() - initialize clock routines for use
Serial.println("Couldn't find RTC");
while (1); // stop here
}
//setup sensor
bmp280.initialize();
bmp280.setEnabled(0);
bmp280.triggerMeasurement();
// initialize and clear display
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.display();
display.setTextSize(2);
display.setTextColor(WHITE);
}
void loop() {
float tempBMP280;
float tempDHT22;
float pres;
float humi;
DateTime nyt;
nyt = rtc.now();
// read and show sensors every 10th second
if (nyt.second() % 10 == 0) {
//read BMP280
bmp280.triggerMeasurement();
bmp280.awaitMeasurement();
//bmp280.getTemperature(tempBMP280); // BMP280 temperature is not used in this application
bmp280.getPressure(pres);
pres /= 100;
//dtostrf(tempBMP280, 8, 1, strTemp); // BMP280 temperature is not used in this application
dtostrf(pres, 6, 1, strPres);
//read DHT22
int err = SimpleDHTErrSuccess;
err = dht22.read2(&tempDHT22, &humi, NULL);
dtostrf(tempDHT22, 6, 1, strTemp);
dtostrf(humi, 6, 1, strHumi);
}
//hae paikallinen aika
getTimeStr(getLocalTime());
display.clearDisplay();
display.setCursor(18, 48);
display.print(strTime);
display.setCursor(0, 0);
display.print(strTemp);
display.print(" C");
display.setCursor(0, 16);
display.print(strPres);
display.print(" hPa");
display.setCursor(0, 32);
display.print(strHumi);
display.print(" %");
display.display();
delay(100);
}
boolean is_Dls_On(unsigned long ut) {
// ut = unix timestamp for current UTC time
// Unix timestamps in the following list are UTC timestamps for daylight saving changing moments for the timezone.
// They are intentionally one second before the full hour when the change happens
// This list is for the Canary Islands (WET/WEST timezone). For other timezones new timestamps needs to be calculated.
// In linux you can get these changing dates and times for a timezone with command : zdump -v timezone
// You still need to convert the dates and times to unix timestamps but there are programs and web sites to do that.
// Note. This list seems to be valid for whole Europe
// check if daylight saving is on
if (ut > 1521939599 && ut < 1540688399 || //25.03.18 00:59:59 28.10.18 00:59:59
ut > 1553993999 && ut < 1572137999 || //31.03.19 00:59:59 27.10.19 00:59:59
ut > 1585443599 && ut < 1603587599 || //29.03.20 00:59:59 25.10.20 00:59:59
ut > 1616893199 && ut < 1635641999 || //28.03.21 00:59:59 31.10.21 00:59:59
ut > 1648342799 && ut < 1667091599 || //27.03.22 00:59:59 30.10.22 00:59:59
ut > 1648342799 && ut < 1667091599 || //27.03.22 00:59:59 30.10.22 00:59:59
ut > 1679792399 && ut < 1698541199 || //26.03.23 00:59:59 29.10.23 00:59:59
ut > 1711846799 && ut < 1729990799 || //31.03.24 00:59:59 27.10.24 00:59:59
ut > 1743296399 && ut < 1761440399 || //30.03.25 00:59:59 26.10.25 00:59:59
ut > 1774745999 && ut < 1792889999 //29.03.26 00:59:59 25.10.26 00:59:59
)
{
return (true);
}
else {
return (false);
}
}
DateTime getUtcTime() {
// expects that RTC time is set to UTC
return (rtc.now());
}
DateTime getLocalTime() {
// expects that RTC time is set to UTC
if (is_Dls_On(rtc.now().unixtime())) {
return (rtc.now() + TimeSpan(dls_On_Offset)); // return local summertime
}
else {
return (rtc.now() + TimeSpan(dls_Off_Offset)); // return local winter time
}
}
void getTimeStr(DateTime dt) {
// strDate and strTime are global variables
sprintf(strDate, "%02d.%02d.%02d", dt.day(), dt.month(), dt.year() - 2000);
sprintf(strTime, "%02d:%02d:%02d", dt.hour(), dt.minute(), dt.second());
}