Snorktracker
 All Data Structures Files Functions Variables Macros Pages
BME280.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018 SFini
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
24 #include <Adafruit_BME280.h>
25 
26 #define BARO_CORR_HPA 34.5879
27 #define TEMP_CORR_DEGREE -2.0
28 
29 
32 class MyBME280
33 {
34 protected:
37  int pinGrnd;
38  uint8_t portAddr;
39  Adafruit_BME280 bme280;
40 
41 public:
42  MyBME280(MyOptions &options, MyData &data, int pin, uint8_t addr);
43 
44  bool begin();
45 
46  bool readValues();
47 };
48 
49 /* ******************************************** */
50 
52 MyBME280::MyBME280(MyOptions &options, MyData &data, int pin, uint8_t addr)
53  : pinGrnd(pin)
54  , myOptions(options)
55  , myData(data)
56  , portAddr(addr)
57 {
58 }
59 
62 {
63  pinMode(D1, INPUT); // I2C SCL Open state to safe power
64  pinMode(D2, INPUT); // I2C SDA Open state to safe power
65  pinMode(pinGrnd, OUTPUT);
66  digitalWrite(pinGrnd, HIGH);
67 }
68 
74 {
76  digitalWrite(pinGrnd, LOW);
77  delay(100); // Short delay after power on
78  if (!bme280.begin(portAddr)) {
79  myData.temperature = 0;
80  myData.humidity = 0;
81  myData.pressure = 0;
82  MyDbg("No valid BME280 sensor, check wiring!");
83  } else {
84  myData.temperature = bme280.readTemperature() + TEMP_CORR_DEGREE;
85  myData.humidity = bme280.readHumidity();
86  myData.pressure = (bme280.readPressure() / 100.0F) + BARO_CORR_HPA;
87  MyDbg("Temperature: " + String(myData.temperature) + "°C");
88  MyDbg("Humidity: " + String(myData.humidity) + "%");
89  MyDbg("Pressure: " + String(myData.pressure) + "hPa");
90  }
91  digitalWrite(pinGrnd, HIGH);
92  pinMode(D1, INPUT); // I2C SCL Open state to safe power
93  pinMode(D2, INPUT); // I2C SDA Open state to safe power
94  }
95 }
bool readValues()
Definition: BME280.h:73
bool begin()
Definition: BME280.h:61
class MyData::RtcData rtcData
Data to store in the RTC memory.
MyOptions myOptions
The global options.
Definition: tracker.ino:57
#define TEMP_CORR_DEGREE
The BME280 measure 2 degrees too high.
Definition: BME280.h:27
Definition: Data.h:27
void MyDbg(String info, bool fromWebServer=false, bool newline=true)
Definition: Utils.h:94
Adafruit_BME280 bme280
Adafruit BME280 helper interface.
Definition: BME280.h:39
int pinGrnd
Ground-Pin connection to switch on the BME280 module.
Definition: BME280.h:37
double temperature
Current BME280 temperature.
Definition: Data.h:75
MyBME280(MyOptions &options, MyData &data, int pin, uint8_t addr)
Definition: BME280.h:52
double pressure
Current BME280 pressure.
Definition: Data.h:77
MyOptions & myOptions
Reference to global options.
Definition: BME280.h:35
uint8_t portAddr
Port address of the bme280.
Definition: BME280.h:38
long bme280CheckIntervalSec
Time interval to read the temp, hum and pressure.
Definition: Options.h:40
#define BARO_CORR_HPA
Correction for 289m above sea level.
Definition: BME280.h:26
MyData & myData
Reference to global data.
Definition: BME280.h:36
double humidity
Current BME280 humidity.
Definition: Data.h:76
MyData myData
The global collected data.
Definition: tracker.ino:58
long lastBme280ReadSec
Timestamp of the last BME280 read.
Definition: Data.h:45
bool secondsElapsedAndUpdate(long &lastCheckSec, const long &intervalSec)
Definition: Utils.h:63