supla-device
Loading...
Searching...
No Matches
BME280.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (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, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#ifndef SRC_SUPLA_SENSOR_BME280_H_
20#define SRC_SUPLA_SENSOR_BME280_H_
21
22// Dependency: Adafruid BME280 library - use library manager to install it
23#include <Adafruit_BME280.h>
24
25#include "therm_hygro_press_meter.h"
26
27namespace Supla {
28namespace Sensor {
29class BME280 : public ThermHygroPressMeter {
30 public:
31 explicit BME280(int8_t address = 0x77, float altitude = NAN)
32 : address(address), sensorStatus(false), altitude(altitude) {
33 }
34
35 double getTemp() {
36 float value = TEMPERATURE_NOT_AVAILABLE;
37 bool retryDone = false;
38 do {
39 if (!sensorStatus || isnan(value)) {
40 sensorStatus = bme.begin(address);
41 retryDone = true;
42 }
43 value = TEMPERATURE_NOT_AVAILABLE;
44 if (sensorStatus) {
45 value = bme.readTemperature();
46 }
47 } while (isnan(value) && !retryDone);
48 return value;
49 }
50
51 double getHumi() {
52 float value = HUMIDITY_NOT_AVAILABLE;
53 bool retryDone = false;
54 do {
55 if (!sensorStatus || isnan(value)) {
56 sensorStatus = bme.begin(address);
57 retryDone = true;
58 }
59 value = HUMIDITY_NOT_AVAILABLE;
60 if (sensorStatus) {
61 value = bme.readHumidity();
62 }
63 } while (isnan(value) && !retryDone);
64 return value;
65 }
66
67 double getPressure() {
68 float value = PRESSURE_NOT_AVAILABLE;
69 bool retryDone = false;
70 do {
71 if (!sensorStatus || isnan(value)) {
72 sensorStatus = bme.begin(address);
73 retryDone = true;
74 }
75 value = PRESSURE_NOT_AVAILABLE;
76 if (sensorStatus) {
77 value = bme.readPressure() / 100.0;
78 }
79 } while (isnan(value) && !retryDone);
80 if (!isnan(altitude)) {
81 value = bme.seaLevelForAltitude(altitude, value);
82 }
83 return value;
84 }
85
86 void onInit() {
87 sensorStatus = bme.begin(address);
88
89 pressureChannel.setNewValue(getPressure());
90 channel.setNewValue(getTemp(), getHumi());
91 }
92
93 void setAltitude(float newAltitude) {
94 altitude = newAltitude;
95 }
96
97 protected:
98 int8_t address;
99 bool sensorStatus;
100 float altitude;
101 Adafruit_BME280 bme; // I2C
102};
103
104}; // namespace Sensor
105}; // namespace Supla
106
107#endif // SRC_SUPLA_SENSOR_BME280_H_
void onInit()
Third method called on element in SuplaDevice.begin()
Definition BME280.h:86