supla-device
Loading...
Searching...
No Matches
SCD4x.h
1// SPDX-FileCopyrightText: malarz
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_SENSOR_SCD4X_H_
5#define SRC_SUPLA_SENSOR_SCD4X_H_
6
7// Dependency: SparkFun_SCD4x_Arduino_Library
8// use library manager to install it
9// https://github.com/sparkfun/SparkFun_SCD4x_Arduino_Library
10
11#include "SparkFun_SCD4x_Arduino_Library.h"
12
13#include "therm_hygro_meter.h"
14#include "general_purpose_measurement.h"
15
16namespace Supla {
17namespace Sensor {
18class SCD4x : public ThermHygroMeter {
19 public:
20 SCD4x() {
21 co2channel = new GeneralPurposeMeasurement();
22 co2channel->setDefaultUnitAfterValue("ppm");
23 co2channel->setInitialCaption("CO₂");
24 co2channel->getChannel()->setDefaultIcon(8);
25 co2channel->setDefaultValuePrecision(1);
26 }
27
28 double getTemp() override {
29 readValuesFromDevice();
30 return temperature;
31 }
32
33 double getHumi() override {
34 return humidity;
35 }
36
37 double getCO2() {
38 return co2;
39 }
40
41 void onInit() override {
42 if (scd.begin() == false) {
43 SUPLA_LOG_DEBUG("SCD4x Sensor not detected. Please check wiring.");
44 } else {
45 SUPLA_LOG_DEBUG("SCD4x Sensor detected.");
46 }
47 }
48
49 GeneralPurposeMeasurement* getCO2channel() {
50 return co2channel;
51 }
52
53 private:
54 void readValuesFromDevice() {
55 if (scd.readMeasurement()) {
56 retryCount = 0;
57 temperature = scd.getTemperature();
58 humidity = scd.getHumidity();
59 co2 = scd.getCO2();
60 co2channel->setValue(co2);
61 } else {
62 SUPLA_LOG_DEBUG("SCD4x read error");
63 retryCount++;
64 if (retryCount > 3) {
65 retryCount = 0;
66 temperature = TEMPERATURE_NOT_AVAILABLE;
67 humidity = HUMIDITY_NOT_AVAILABLE;
68 co2 = NAN;
69 }
70 }
71 }
72
73 protected:
74 double temperature = TEMPERATURE_NOT_AVAILABLE;
75 double humidity = HUMIDITY_NOT_AVAILABLE;
76 double co2 = NAN;
77 int8_t retryCount = 0;
78 ::SCD4x scd;
79 GeneralPurposeMeasurement *co2channel = nullptr;
80};
81
82}; // namespace Sensor
83}; // namespace Supla
84
85#endif // SRC_SUPLA_SENSOR_SCD4X_H_
void setDefaultIcon(uint8_t iconId)
Sets default icon.
Definition channel.cpp:1562
void setInitialCaption(const char *caption, bool secondaryChannel=false)
Sets initial caption.
Definition element.cpp:390
static Element * begin()
Returns first Element (based on creation order)
Definition element.cpp:51
void setDefaultUnitAfterValue(const char *unit)
Sets default unit which is displayed after value.
Definition general_purpose_channel_base.cpp:197
virtual void setValue(const double &value)
Method used to set new value for channel with driver which accepts MeasurementDriver::setValue() meth...
Definition general_purpose_channel_base.cpp:560
void setDefaultValuePrecision(uint8_t precision)
Sets default precision (number of decimal places)
Definition general_purpose_channel_base.cpp:182
Definition general_purpose_measurement.h:11
Definition SCD4x.h:18
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition SCD4x.h:41
Definition therm_hygro_meter.h:14