supla-device
Loading...
Searching...
No Matches
BMP280.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O., malarz
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_SENSOR_BMP280_H_
5#define SRC_SUPLA_SENSOR_BMP280_H_
6
7// Dependency: Adafruid BMP280 library - use library manager to install it
8#include <Adafruit_BMP280.h>
9
10#include "therm_press_meter.h"
11
12namespace Supla {
13namespace Sensor {
14class BMP280 : public ThermPressMeter {
15 public:
16 explicit BMP280(int8_t address = 0x76, float altitude = NAN)
17 : address(address), sensorStatus(false), altitude(altitude) {
18 }
19
20 double getTemp() {
21 float value = TEMPERATURE_NOT_AVAILABLE;
22 bool retryDone = false;
23 do {
24 if (!sensorStatus || isnan(value)) {
25 sensorStatus = bmp.begin(address);
26 retryDone = true;
27 }
28 value = TEMPERATURE_NOT_AVAILABLE;
29 if (sensorStatus) {
30 value = bmp.readTemperature();
31 }
32 } while (isnan(value) && !retryDone);
33 return value;
34 }
35
36double getPressure() {
37 float value = PRESSURE_NOT_AVAILABLE;
38 bool retryDone = false;
39 do {
40 if (!sensorStatus || isnan(value)) {
41 sensorStatus = bmp.begin(address);
42 retryDone = true;
43 }
44 value = PRESSURE_NOT_AVAILABLE;
45 if (sensorStatus) {
46 value = bmp.readPressure() / 100.0;
47 }
48 } while (isnan(value) && !retryDone);
49 if (!isnan(altitude)) {
50 value = bmp.seaLevelForAltitude(altitude, value);
51 }
52 return value;
53 }
54
55 void onInit() {
56 sensorStatus = bmp.begin(address);
57
58 channel.setNewValue(getTemp());
59 pressureChannel.setNewValue(getPressure());
60 }
61
62 void setAltitude(float newAltitude) {
63 altitude = newAltitude;
64 }
65
66 protected:
67 int8_t address;
68 bool sensorStatus;
69 float altitude;
70 Adafruit_BMP280 bmp; // I2C
71};
72
73}; // namespace Sensor
74}; // namespace Supla
75
76#endif // SRC_SUPLA_SENSOR_BMP280_H_
Definition BMP280.h:14
void onInit()
Third method called on element in SuplaDevice.begin()
Definition BMP280.h:55
Definition therm_press_meter.h:11