supla-device
Loading...
Searching...
No Matches
BMP280.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O., malarz
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_BMP280_H_
20#define SRC_SUPLA_SENSOR_BMP280_H_
21
22// Dependency: Adafruid BMP280 library - use library manager to install it
23#include <Adafruit_BMP280.h>
24
25#include "therm_press_meter.h"
26
27namespace Supla {
28namespace Sensor {
29class BMP280 : public ThermPressMeter {
30 public:
31 explicit BMP280(int8_t address = 0x76, 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 = bmp.begin(address);
41 retryDone = true;
42 }
43 value = TEMPERATURE_NOT_AVAILABLE;
44 if (sensorStatus) {
45 value = bmp.readTemperature();
46 }
47 } while (isnan(value) && !retryDone);
48 return value;
49 }
50
51double getPressure() {
52 float value = PRESSURE_NOT_AVAILABLE;
53 bool retryDone = false;
54 do {
55 if (!sensorStatus || isnan(value)) {
56 sensorStatus = bmp.begin(address);
57 retryDone = true;
58 }
59 value = PRESSURE_NOT_AVAILABLE;
60 if (sensorStatus) {
61 value = bmp.readPressure() / 100.0;
62 }
63 } while (isnan(value) && !retryDone);
64 if (!isnan(altitude)) {
65 value = bmp.seaLevelForAltitude(altitude, value);
66 }
67 return value;
68 }
69
70 void onInit() {
71 sensorStatus = bmp.begin(address);
72
73 channel.setNewValue(getTemp());
74 pressureChannel.setNewValue(getPressure());
75 }
76
77 void setAltitude(float newAltitude) {
78 altitude = newAltitude;
79 }
80
81 protected:
82 int8_t address;
83 bool sensorStatus;
84 float altitude;
85 Adafruit_BMP280 bmp; // I2C
86};
87
88}; // namespace Sensor
89}; // namespace Supla
90
91#endif // SRC_SUPLA_SENSOR_BMP280_H_
void onInit()
Third method called on element in SuplaDevice.begin()
Definition BMP280.h:70