1
1
//
2
2
// FILE: SHT31.cpp
3
3
// AUTHOR: Rob Tillaart
4
- // VERSION: 0.1.0
4
+ // VERSION: 0.1.2
5
5
// DATE: 2019-02-08
6
6
// PURPOSE: Class for SHT31 I2C temperature humidity sensor
7
7
// https://www.adafruit.com/product/2857
8
8
//
9
9
// HISTORY:
10
10
// 0.1.0 - 2019-02-08 initial version
11
+ // 0.1.1 - 2019-02-18 add description readStatus(),
12
+ // async interface
13
+ // 0.1.2 - 2019-03-05 fix issue #123 - error in humidity
14
+ // stable version
11
15
//
12
16
// Released to the public domain
13
17
//
16
20
17
21
#define SHT31_READ_STATUS 0xF32D
18
22
#define SHT31_CLEAR_STATUS 0x3041
19
- #define SHT31_RESET 0x30A2
23
+ #define SHT31_SOFT_RESET 0x30A2
24
+ #define SHT31_HARD_RESET 0x0006
20
25
#define SHT31_MEASUREMENT_FAST 0x2416
21
26
#define SHT31_MEASUREMENT_SLOW 0x2400
22
27
#define SHT31_HEAT_ON 0x306D
@@ -54,15 +59,16 @@ bool SHT31::read(bool fast)
54
59
writeCmd (SHT31_MEASUREMENT_SLOW);
55
60
delay (15 ); // table 4 datasheet
56
61
}
62
+
63
+ // TODO 5 read bytes would be sufficient when not fast / no CRC...
57
64
readBytes (6 , (uint8_t *) &buffer[0 ]);
58
65
59
66
if (!fast)
60
67
{
61
- // TODO 5 read bytes would be sufficient when not fast...
62
68
// TODO check CRC here
63
69
// TODO rv = false;
64
70
}
65
- float raw = (buffer[0 ] << 8 ) + buffer[1 ];
71
+ uint16_t raw = (buffer[0 ] << 8 ) + buffer[1 ];
66
72
temperature = raw * (175.0 / 65535 ) - 45 ;
67
73
raw = (buffer[3 ] << 8 ) + buffer[4 ];
68
74
humidity = raw * (100.0 / 65535 );
@@ -76,16 +82,44 @@ uint16_t SHT31::readStatus()
76
82
{
77
83
uint32_t status = 0 ;
78
84
79
- writeCmd (SHT31_READ_STATUS);
80
- readBytes (3 , (uint8_t *) &status);
85
+ writeCmd (SHT31_READ_STATUS); // page 13 datasheet
86
+ readBytes (3 , (uint8_t *) &status); // 16 bit status + CRC
87
+ // TODO CRC check
81
88
return status;
89
+
90
+ // bit - description
91
+ // ==================
92
+ // 15 Alert pending status
93
+ // '0': no pending alerts
94
+ // '1': at least one pending alert - default
95
+ // 14 Reserved ‘0’
96
+ // 13 Heater status
97
+ // '0’ : Heater OFF - default
98
+ // '1’ : Heater ON
99
+ // 12 Reserved '0’
100
+ // 11 Humidity tracking alert
101
+ // '0’ : no alert - default
102
+ // '1’ : alert
103
+ // 10 Temp tracking alert
104
+ // '0’ : no alert - default
105
+ // '1’ : alert
106
+ // 9:5 Reserved '00000’
107
+ // 4 System reset detected
108
+ // '0': no reset since last ‘clear status register’ command
109
+ // '1': reset detected (hard or soft reset command or supply fail)
110
+ // 3:2 Reserved ‘00’
111
+ // 1 Command status
112
+ // '0': last cmd executed successfully
113
+ // '1': last cmd not processed. Invalid or failed checksum
114
+ // 0 Write data checksum status
115
+ // '0': checksum of last write correct
116
+ // '1': checksum of last write transfer failed
82
117
}
83
118
84
- // hard reset 0x0006 on addr 0x00 see datasheet
85
119
void SHT31::reset ()
86
120
{
87
- writeCmd (SHT31_RESET);
88
- delay (1 ); // table 4 datasheet
121
+ writeCmd (SHT31_SOFT_RESET); // SHT31_HARD_RESET not implemented yet
122
+ delay (1 ); // table 4 datasheet // 100ms for hardreset
89
123
}
90
124
91
125
void SHT31::heatOn ()
@@ -98,6 +132,30 @@ void SHT31::heatOff()
98
132
writeCmd (SHT31_HEAT_OFF);
99
133
}
100
134
135
+ void SHT31::requestData ()
136
+ {
137
+ writeCmd (SHT31_MEASUREMENT_SLOW);
138
+ _lastRequest = millis ();
139
+ }
140
+
141
+ bool SHT31::dataReady ()
142
+ {
143
+ return ((millis () - _lastRequest) > 15 );
144
+ }
145
+
146
+ void SHT31::readData ()
147
+ {
148
+ uint8_t buffer[6 ];
149
+ readBytes (6 , (uint8_t *) &buffer[0 ]);
150
+
151
+ float raw = (buffer[0 ] << 8 ) + buffer[1 ];
152
+ temperature = raw * (175.0 / 65535 ) - 45 ;
153
+ raw = (buffer[3 ] << 8 ) + buffer[4 ];
154
+ humidity = raw * (100.0 / 65535 );
155
+
156
+ _lastRead = millis ();
157
+ }
158
+
101
159
// ////////////////////////////////////////////////////////
102
160
103
161
void SHT31::writeCmd (uint16_t cmd)
0 commit comments