supla-device
Loading...
Searching...
No Matches
SHT3x.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_SENSOR_SHT3X_H_
5#define SRC_SUPLA_SENSOR_SHT3X_H_
6
7// Dependency: ClosedCube SHT3x library - use library manager to install it
8// https://github.com/closedcube/ClosedCube_SHT31D_Arduino (currently
9// unavailable)
10// https://github.com/malarz-supla/ClosedCube_SHT31D_Arduino (fork with fixes)
11
12#include <ClosedCube_SHT31D.h>
13#include <supla/log_wrapper.h>
14
15#include "therm_hygro_meter.h"
16
17namespace Supla {
18namespace Sensor {
19class SHT3x : public ThermHygroMeter {
20 public:
21 explicit SHT3x(int8_t address = 0x44) : address(address) {
22 }
23
24 double getTemp() override {
25 readValuesFromDevice();
26 return temperature;
27 }
28
29 double getHumi() override {
30 return humidity;
31 }
32
33 void onInit() override {
34 sht.begin(address);
35 channel.setNewValue(getTemp(), getHumi());
36 }
37
38 private:
39 void readValuesFromDevice() {
40 SHT31D result = sht.readTempAndHumidity(
41 SHT3XD_REPEATABILITY_LOW, SHT3XD_MODE_CLOCK_STRETCH, 50);
42
43 if (result.error != SHT3XD_NO_ERROR) {
44 SUPLA_LOG_ERROR("SHT [ERROR] Code #%d", result.error);
45 retryCount++;
46 if (retryCount > 3) {
47 retryCount = 0;
48 temperature = TEMPERATURE_NOT_AVAILABLE;
49 humidity = HUMIDITY_NOT_AVAILABLE;
50 }
51 } else {
52 retryCount = 0;
53 temperature = result.t;
54 humidity = result.rh;
55 }
56 }
57
58 protected:
59 int8_t address;
60 double temperature = TEMPERATURE_NOT_AVAILABLE;
61 double humidity = HUMIDITY_NOT_AVAILABLE;
62 int8_t retryCount = 0;
63 ::ClosedCube_SHT31D sht; // I2C
64};
65
66}; // namespace Sensor
67}; // namespace Supla
68
69#endif // SRC_SUPLA_SENSOR_SHT3X_H_
Definition SHT3x.h:19
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition SHT3x.h:33
Definition therm_hygro_meter.h:14