AskSin++
Sht10.h
1 //- -----------------------------------------------------------------------------------------------------------------------
2 // AskSin++
3 // 2018-04-03 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
4 //- -----------------------------------------------------------------------------------------------------------------------
5 
6 #ifndef __SENSORS_SHT10_h__
7 #define __SENSORS_SHT10_h__
8 
9 #include <Sensors.h>
10 #include <Sensirion.h>
11 
12 namespace as {
13 
14 enum Sht10Mode { LOWRES_INT=0x01, LOWRES_FLOAT=0x02, HIGHRES=0x03 };
15 
16 // https://github.com/spease/Sensirion.git
17 template <uint8_t DATAPIN,uint8_t CLOCKPIN,Sht10Mode MODE=Sht10Mode::LOWRES_INT>
18 class Sht10 : public Temperature, public Humidity {
19  Sensirion _sht10;
20 
21  void stopWire () {
22 #if defined(TwoWire_h) || defined(_WIRE_H_)
23  if( DATAPIN == A4 && CLOCKPIN==A5 ) {
24  Wire.end();
25  }
26 #endif
27  }
28 
29  void startWire () {
30 #if defined(TwoWire_h) || defined(_WIRE_H_)
31  if( DATAPIN == A4 && CLOCKPIN==A5 ) {
32  Wire.begin();
33  }
34 #endif
35  }
36 
37  bool meas(uint16_t& tdata,uint16_t& hdata) {
38  bool success = false;
39  stopWire();
40  success = (_sht10.measTemp(&tdata) == 0) && (_sht10.measHumi(&hdata) == 0);
41  startWire();
42  return success;
43  }
44 
45 public:
46 
47  Sht10 () : _sht10(DATAPIN,CLOCKPIN) {}
48 
49  void init () {
50  stopWire();
51  uint8_t sr = MODE == Sht10Mode::HIGHRES ? 0 : LOW_RES;
52  _present = (_sht10.writeSR(sr) == 0);
53  startWire();
54  }
55 
56  void measure (__attribute__((unused)) bool async=false) {
57  if( present() == true ) {
58  uint16_t tdata,hdata;
59  if( meas(tdata,hdata) == true ) {
60  if( MODE != Sht10Mode::LOWRES_INT ) {
61  float t = _sht10.calcTemp(tdata);
62  _temperature = t * 10;
63  _humidity = _sht10.calcHumi(hdata,t);
64  }
65  else {
66  // LOW_RES: temp = -40.1 + 0.04 * (float) tdata;
67  // temp = ((4*tdata) - 4010) / 100
68  int32_t tt = ((4L * tdata) - 4010);
69  // DDEC(_temperature);DPRINT(" <> ");DDECLN(tt/10);
70  // humi = C1 + C2l * rawData + C3l * rawData * rawData;
71  // humi = (temp - 25.0) * (T1 + T2l * rawData) + humi;
72  // LOW_RES: humi = -2.0468 + 0.5872 * hdata + -4.0845E-4 * hdata * hdata;
73  // humi = (temp - 25.0) * (0.01 + 0.00128 * hdata) + humi;
74  // humi = -20468 + 5872 * hdata + -4.0845 * hdata * hdata;
75  // humi = ((temp*100 - 2500) * (100 + 12.8 * hata) / 100) + humi;
76  // humi = huni / 10000
77  int32_t hh = ((5872L * hdata) - 20468L) - (4L * hdata * hdata);
78  hh = (((tt - 2500) * (100L + (12L * hdata)) / 100) + hh) / 10000;
79  hh = hh > 100 ? 100 : hh;
80  hh = hh < 0 ? 0 : hh;
81  // DDEC(_humidity);DPRINT(" <> ");DDECLN(hh);
82  _temperature = tt / 10;
83  _humidity = hh;
84  }
85  }
86  }
87  }
88 
89 };
90 
91 }
92 
93 #endif
as::Humidity
Definition: Sensors.h:39
as::Temperature
Definition: Sensors.h:30
as::Sht10
Definition: Sht10.h:18