-
Notifications
You must be signed in to change notification settings - Fork 1
/
xsns_08_htu21.ino
executable file
·299 lines (256 loc) · 7.16 KB
/
xsns_08_htu21.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
xsns_08_htu21.ino - HTU21 temperature and humidity sensor support for Sonoff-Tasmota
*/
#ifdef USE_I2C
#ifdef USE_HTU
/*********************************************************************************************\
* HTU21 - Temperature and Humidy
*
* Source: Heiko Krupp
*
* I2C Address: 0x40
\*********************************************************************************************/
#define HTU21_ADDR 0x40
#define SI7013_CHIPID 0x0D
#define SI7020_CHIPID 0x14
#define SI7021_CHIPID 0x15
#define HTU21_CHIPID 0x32
#define HTU21_READTEMP 0xE3
#define HTU21_READHUM 0xE5
#define HTU21_WRITEREG 0xE6
#define HTU21_READREG 0xE7
#define HTU21_RESET 0xFE
#define HTU21_HEATER_WRITE 0x51
#define HTU21_HEATER_READ 0x11
#define HTU21_SERIAL2_READ1 0xFC /* Read 3rd two Serial bytes */
#define HTU21_SERIAL2_READ2 0xC9 /* Read 4th two Serial bytes */
#define HTU21_HEATER_ON 0x04
#define HTU21_HEATER_OFF 0xFB
#define HTU21_RES_RH12_T14 0x00 // Default
#define HTU21_RES_RH8_T12 0x01
#define HTU21_RES_RH10_T13 0x80
#define HTU21_RES_RH11_T11 0x81
#define HTU21_CRC8_POLYNOM 0x13100
const char kHtuTypes[] PROGMEM = "HTU21|SI7013|SI7020|SI7021|T/RH?";
uint8_t htu_address;
uint8_t htu_type = 0;
uint8_t delay_temp;
uint8_t delay_humidity = 50;
char htu_types[7];
uint8_t HtuCheckCrc8(uint16_t data)
{
for (uint8_t bit = 0; bit < 16; bit++) {
if (data & 0x8000) {
data = (data << 1) ^ HTU21_CRC8_POLYNOM;
} else {
data <<= 1;
}
}
return data >>= 8;
}
uint8_t HtuReadDeviceId(void)
{
uint16_t deviceID = 0;
uint8_t checksum = 0;
Wire.beginTransmission(HTU21_ADDR);
Wire.write(HTU21_SERIAL2_READ1);
Wire.write(HTU21_SERIAL2_READ2);
Wire.endTransmission();
Wire.requestFrom(HTU21_ADDR, 3);
deviceID = Wire.read() << 8;
deviceID |= Wire.read();
checksum = Wire.read();
if (HtuCheckCrc8(deviceID) == checksum) {
deviceID = deviceID >> 8;
} else {
deviceID = 0;
}
return (uint8_t)deviceID;
}
void HtuSetResolution(uint8_t resolution)
{
uint8_t current = I2cRead8(HTU21_ADDR, HTU21_READREG);
current &= 0x7E; // Replace current resolution bits with 0
current |= resolution; // Add new resolution bits to register
I2cWrite8(HTU21_ADDR, HTU21_WRITEREG, current);
}
void HtuReset(void)
{
Wire.beginTransmission(HTU21_ADDR);
Wire.write(HTU21_RESET);
Wire.endTransmission();
delay(15); // Reset takes 15ms
}
void HtuHeater(uint8_t heater)
{
uint8_t current = I2cRead8(HTU21_ADDR, HTU21_READREG);
switch(heater)
{
case HTU21_HEATER_ON : current |= heater;
break;
case HTU21_HEATER_OFF : current &= heater;
break;
default : current &= heater;
break;
}
I2cWrite8(HTU21_ADDR, HTU21_WRITEREG, current);
}
void HtuInit()
{
HtuReset();
HtuHeater(HTU21_HEATER_OFF);
HtuSetResolution(HTU21_RES_RH12_T14);
}
float HtuReadHumidity(void)
{
uint8_t checksum = 0;
uint16_t sensorval = 0;
float humidity = 0.0;
Wire.beginTransmission(HTU21_ADDR);
Wire.write(HTU21_READHUM);
if (Wire.endTransmission() != 0) {
return 0.0; // In case of error
}
delay(delay_humidity); // Sensor time at max resolution
Wire.requestFrom(HTU21_ADDR, 3);
if (3 <= Wire.available()) {
sensorval = Wire.read() << 8; // MSB
sensorval |= Wire.read(); // LSB
checksum = Wire.read();
}
if (HtuCheckCrc8(sensorval) != checksum) {
return 0.0; // Checksum mismatch
}
sensorval ^= 0x02; // clear status bits
humidity = 0.001907 * (float)sensorval - 6;
if (humidity > 100) {
return 100.0;
}
if (humidity < 0) {
return 0.01;
}
return humidity;
}
float HtuReadTemperature()
{
uint8_t checksum=0;
uint16_t sensorval=0;
float t;
Wire.beginTransmission(HTU21_ADDR);
Wire.write(HTU21_READTEMP);
if (Wire.endTransmission() != 0) {
return 0.0; // In case of error
}
delay(delay_temp); // Sensor time at max resolution
Wire.requestFrom(HTU21_ADDR, 3);
if (3 == Wire.available()) {
sensorval = Wire.read() << 8; // MSB
sensorval |= Wire.read(); // LSB
checksum = Wire.read();
}
if (HtuCheckCrc8(sensorval) != checksum) {
return 0.0; // Checksum mismatch
}
t = ConvertTemp(0.002681 * (float)sensorval - 46.85);
return t;
}
float HtuCompensatedHumidity(float humidity, float temperature)
{
if(humidity == 0.00 && temperature == 0.00) {
return 0.0;
}
if(temperature > 0.00 && temperature < 80.00) {
return (-0.15)*(25-temperature)+humidity;
}
return humidity;
}
/********************************************************************************************/
void HtuDetect()
{
if (htu_type) {
return;
}
htu_address = HTU21_ADDR;
htu_type = HtuReadDeviceId();
if (htu_type) {
uint8_t index = 0;
HtuInit();
switch (htu_type) {
case HTU21_CHIPID:
delay_temp = 50;
delay_humidity = 16;
break;
case SI7021_CHIPID:
index++; // 3
case SI7020_CHIPID:
index++; // 2
case SI7013_CHIPID:
index++; // 1
delay_temp = 12;
delay_humidity = 23;
break;
default:
index = 4;
delay_temp = 50;
delay_humidity = 23;
}
GetTextIndexed(htu_types, sizeof(htu_types), index, kHtuTypes);
snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, htu_types, htu_address);
AddLog(LOG_LEVEL_DEBUG);
}
}
void HtuShow(boolean json)
{
if (htu_type) {
char temperature[10];
char humidity[10];
float t = HtuReadTemperature();
float h = HtuReadHumidity();
h = HtuCompensatedHumidity(h, t);
dtostrfd(t, Settings.flag2.temperature_resolution, temperature);
dtostrfd(h, Settings.flag2.humidity_resolution, humidity);
if (json) {
snprintf_P(mqtt_data, sizeof(mqtt_data), JSON_SNS_TEMPHUM, mqtt_data, htu_types, temperature, humidity);
#ifdef USE_DOMOTICZ
if (0 == tele_period) DomoticzTempHumSensor(temperature, humidity);
#endif // USE_DOMOTICZ
#ifdef USE_KNX
if (0 == tele_period) {
KnxSensor(KNX_TEMPERATURE, t);
KnxSensor(KNX_HUMIDITY, h);
}
#endif // USE_KNX
#ifdef USE_WEBSERVER
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, htu_types, temperature, TempUnit());
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_HUM, mqtt_data, htu_types, humidity);
#endif // USE_WEBSERVER
}
}
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
#define XSNS_08
boolean Xsns08(byte function)
{
boolean result = false;
if (i2c_flg) {
switch (function) {
case FUNC_PREP_BEFORE_TELEPERIOD:
HtuDetect();
break;
case FUNC_JSON_APPEND:
HtuShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_WEB_APPEND:
HtuShow(0);
break;
#endif // USE_WEBSERVER
}
}
return result;
}
#endif // USE_HTU
#endif // USE_I2C