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
29#include "therm_hygro_meter.h"
30
31namespace Supla {
32namespace Sensor {
33class SHT3x : public ThermHygroMeter {
34 public:
35 explicit SHT3x(int8_t address = 0x44) : address(address) {
36 }
37
38 double getTemp() override {
39 readValuesFromDevice();
40 return temperature;
41 }
42
43 double getHumi() override {
44 return humidity;
45 }
46
47 void onInit() override {
48 sht.begin(address);
49 channel.setNewValue(getTemp(), getHumi());
50 }
51
52 private:
53 void readValuesFromDevice() {
54 SHT31D result = sht.readTempAndHumidity(
55 SHT3XD_REPEATABILITY_LOW, SHT3XD_MODE_CLOCK_STRETCH, 50);
56
57 if (result.error != SHT3XD_NO_ERROR) {
58 Serial.print(F("SHT [ERROR] Code #"));
59 Serial.println(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:47