supla-device
Loading...
Searching...
No Matches
SHT3x.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_SHT3X_H_
20#define SRC_SUPLA_SENSOR_SHT3X_H_
21
22// Dependency: ClosedCube SHT3x library - use library manager to install it
23// https://github.com/closedcube/ClosedCube_SHT31D_Arduino (currently
24// unavailable)
25// https://github.com/malarz-supla/ClosedCube_SHT31D_Arduino (fork with fixes)
26
27#include <ClosedCube_SHT31D.h>
28#include <supla/log_wrapper.h>
29
30#include "therm_hygro_meter.h"
31
32namespace Supla {
33namespace Sensor {
34class SHT3x : public ThermHygroMeter {
35 public:
36 explicit SHT3x(int8_t address = 0x44) : address(address) {
37 }
38
39 double getTemp() override {
40 readValuesFromDevice();
41 return temperature;
42 }
43
44 double getHumi() override {
45 return humidity;
46 }
47
48 void onInit() override {
49 sht.begin(address);
50 channel.setNewValue(getTemp(), getHumi());
51 }
52
53 private:
54 void readValuesFromDevice() {
55 SHT31D result = sht.readTempAndHumidity(
56 SHT3XD_REPEATABILITY_LOW, SHT3XD_MODE_CLOCK_STRETCH, 50);
57
58 if (result.error != SHT3XD_NO_ERROR) {
59 SUPLA_LOG_ERROR("SHT [ERROR] Code #%d", result.error);
60 retryCount++;
61 if (retryCount > 3) {
62 retryCount = 0;
63 temperature = TEMPERATURE_NOT_AVAILABLE;
64 humidity = HUMIDITY_NOT_AVAILABLE;
65 }
66 } else {
67 retryCount = 0;
68 temperature = result.t;
69 humidity = result.rh;
70 }
71 }
72
73 protected:
74 int8_t address;
75 double temperature = TEMPERATURE_NOT_AVAILABLE;
76 double humidity = HUMIDITY_NOT_AVAILABLE;
77 int8_t retryCount = 0;
78 ::ClosedCube_SHT31D sht; // I2C
79};
80
81}; // namespace Sensor
82}; // namespace Supla
83
84#endif // SRC_SUPLA_SENSOR_SHT3X_H_
void onInit() override
Third method called on element in SuplaDevice.begin().
Definition SHT3x.h:48