forked from Risele/SHT3x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSHT3x.cpp
605 lines (564 loc) · 12.4 KB
/
SHT3x.cpp
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#include "SHT3x.h"
SHT3x::SHT3x(int Address, ValueIfError Value, uint8_t HardResetPin, SHT3xSensor SensorType, SHT3xMode Mode)
{
_Error = noError;
SetAddress((uint8_t)Address);
_ValueIfError = Value;
SetMode(Mode);
_SensorType = SensorType;
_HardResetPin = HardResetPin ;
}
void SHT3x::Begin(int SDA, int SCL)
{
if (SDA==0) Wire.begin();
// else Wire1.begin(SDA, SCL);
_OperationEnabled = true;
}
void SHT3x::UpdateData(int SDA)
{
_Error = noError;
if ((_LastUpdateMillisec == 0) || ((millis()-_LastUpdateMillisec)>=_UpdateIntervalMillisec))
{
SendCommand(_MeasMSB,_MeasLSB,SDA);
if (_Error == noError)
{
if (SDA==0)
{
Wire.requestFrom(_Address, (uint8_t)6);
uint32_t WaitingBeginTime = millis();
while ((Wire.available()<6) && ((millis() - WaitingBeginTime) < _TimeoutMillisec))
{
//Do nothing, just wait
}
if ((millis() - WaitingBeginTime) < _TimeoutMillisec)
{
_LastUpdateMillisec = WaitingBeginTime;
uint8_t data[6];
for (uint8_t i = 0; i<6; i++)
{
data[i] = Wire.read();
}
if ((CRC8(data[0],data[1],data[2])) && (CRC8(data[3],data[4],data[5])))
{
uint16_t TemperatureRaw = (data[0]<<8)+(data[1]<<0);
uint16_t RelHumidityRaw = (data[3]<<8)+(data[4]<<0);
_TemperatureCeil = ((float) TemperatureRaw) * 0.00267033 - 45.;
_TemperatureCeil = _TemperatureCeil*_TemperatureCalibration.Factor +
_TemperatureCalibration.Shift;
_RelHumidity = ((float) RelHumidityRaw) * 0.0015259;
_RelHumidity = _RelHumidity * _RelHumidityCalibration.Factor +
_RelHumidityCalibration.Shift;
_Error = noError;
}
else
{
_Error = DataCorrupted;
}
}
else //Timeout
{
_Error = Timeout;
}
}
// else
// {
// Wire1.requestFrom(_Address, (uint8_t)6);
// uint32_t WaitingBeginTime = millis();
// while ((Wire1.available()<6) && ((millis() - WaitingBeginTime) < _TimeoutMillisec))
// {
// //Do nothing, just wait
// }
// if ((millis() - WaitingBeginTime) < _TimeoutMillisec)
// {
// _LastUpdateMillisec = WaitingBeginTime;
// uint8_t data[6];
// for (uint8_t i = 0; i<6; i++)
// {
// data[i] = Wire1.read();
// }
// if ((CRC8(data[0],data[1],data[2])) && (CRC8(data[3],data[4],data[5])))
// {
// uint16_t TemperatureRaw = (data[0]<<8)+(data[1]<<0);
// uint16_t RelHumidityRaw = (data[3]<<8)+(data[4]<<0);
// _TemperatureCeil = ((float) TemperatureRaw) * 0.00267033 - 45.;
// _TemperatureCeil = _TemperatureCeil*_TemperatureCalibration.Factor +
// _TemperatureCalibration.Shift;
// _RelHumidity = ((float) RelHumidityRaw) * 0.0015259;
// _RelHumidity = _RelHumidity * _RelHumidityCalibration.Factor +
// _RelHumidityCalibration.Shift;
// _Error = noError;
// }
// else
// {
// _Error = DataCorrupted;
// }
// }
// else //Timeout
// {
// _Error = Timeout;
// }
// }
}
else //Error after message send
{
// Nothing to do, measurement commands will return NULL because of Error != noError
}
}
else //LastUpdate was too recently
{
//Nothing to do, wait for next call
}
}
float SHT3x::GetTemperature(TemperatureScale Degree)
{
//default scale is Celsius
//At first, calculate in Celsius, than, if need, recalculate to Farenheit or Kelvin and than adjust according to calibration;
float Temperature = _TemperatureCeil;
if (Degree == Kel)
{
Temperature+=273.15;
}
else if (Degree == Far)
{
Temperature = Temperature*1.8+32.;
}
return ReturnValueIfError(Temperature);
}
float SHT3x::GetRelHumidity()
{
return ReturnValueIfError(_RelHumidity);
}
float SHT3x::GetAbsHumidity(AbsHumidityScale Scale)
{
float millikelvins = GetTemperature(Kel) / 1000.;
float Pressure = 0.;
for (uint8_t i = 0; i<6; i++)
{
float term = 1.;
for (uint8_t j = 0; j<i; j++)
{
term *= millikelvins;
}
Pressure += term*_AbsHumPoly[i];
}
Pressure *= GetRelHumidity();
switch (Scale)
{
case mmHg:
{
//Already in mm Hg
break;
}
case Torr:
{
//Already in Torr
break;
}
case Pa:
{
Pressure *= 133.322;
break;
}
case Bar:
{
Pressure *= 0.0013332;
break;
}
case At:
{
Pressure *= 0.0013595;
break;
}
case Atm:
{
Pressure *= 0.0013158;
break;
}
case mH2O:
{
Pressure *= 0.013595;
break;
}
case psi:
{
Pressure *= 0.019337;
break;
}
default:
{
break;
}
}
return ReturnValueIfError(Pressure);
}
float SHT3x::GetTempTolerance(TemperatureScale Degree, SHT3xSensor SensorType)
{
//Temperature tolerance is similar for both SHT30 and SHT31
//At first, calculate at Celsius (similar to Kelvins), than, if need, recalculate to Farenheit
float Temperature = GetTemperature();
float Tolerance;
switch (SensorType)
{
case SHT30:
{
if ((0. <= Temperature) && (Temperature <= 65.))
{
Tolerance = 0.2;
}
else if (Temperature > 65.)
{
//Linear from 0.2 at 65 C to 0.6 at 125 C.
Tolerance = 0.0067 * Temperature - 0.2333;
}
else //if (Temperature < 0.)
{
//Linear from 0.6 at -40 C to 0.2 at 0 C.
Tolerance = -0.01 * Temperature + 0.2;
}
break;
}
case SHT31:
{
if ((0. <= Temperature) && (Temperature <= 90.))
{
Tolerance = 0.2;
}
else if (Temperature > 65.)
{
//Linear from 0.2 at 90 C to 0.5 at 125 C.
Tolerance = 0.0086 * Temperature - 0.5714;
}
else //if (Temperature < 0.)
{
//Linear from 0.3 at -40 C to 0.2 at 0 C.
Tolerance = -0.0025 * Temperature + 0.2;
}
break;
}
case SHT35:
{
if (Temperature <= 0.)
{
Tolerance = 0.2;
}
else if ((0. < Temperature) && (Temperature <= 20.))
{
//Linear from 0.2 at 0 C to 0.1 at 20 C.
Tolerance = -0.005 * Temperature + 0.2;
}
else if ((20. < Temperature) && (Temperature <= 60.))
{
Tolerance = 0.1;
}
else if ((60. < Temperature) && (Temperature <= 90.))
{
//Linear from 0.1 at 60 C to 0.2 at 90 C.
Tolerance = -0.0033 * Temperature - 0.1;
}
else //if (90. < Temperature)
{
//Linear from 0.2 at 90 C to 0.4 at 125 C.
Tolerance = 0.0057 * Temperature - 0.3143;
}
break;
}
}
if (Degree == Far)
{
Tolerance *= 1.8;
}
return ReturnValueIfError(Tolerance);
}
float SHT3x::GetRelHumTolerance(SHT3xSensor SensorType)
{
float RelHumidity = GetRelHumidity();
float Tolerance;
switch (SensorType)
{
case SHT30:
{
if ((10.<= RelHumidity) && (RelHumidity <= 90.))
{
Tolerance = 2.;
}
else if (RelHumidity < 10.)
{
//Linear from 4 at 0% to 2 at 10%
Tolerance = -0.2 * RelHumidity + 4.;
}
else
{
//Linear from 2 at 90% to 4 at 100%
Tolerance = 0.2 * RelHumidity - 16.;
}
break;
}
case SHT31:
{
Tolerance = 2.;
break;
}
case SHT35:
{
if (RelHumidity <= 80.)
{
Tolerance = 1.5;
}
else //if (80 < RelHumidity)
{
//Linear from 0.5 at 80% to 2 at 100%
Tolerance = 0.025 * RelHumidity - 0.5;
}
break;
}
}
return ReturnValueIfError(Tolerance);
}
float SHT3x::GetAbsHumTolerance(AbsHumidityScale Scale, SHT3xSensor SensorType)
{
/* Dependence of absolute humidity is similar (from 0 to 80C) to P = H*a*exp(b*T),
* where P is absolute humidity, H is relative humidity, T is temperature (Celsius),
* a ~= 0.0396, b~=0.0575.
* So its relative tolerance dP/P = square root [ (dH/H)^2 + (b*dT)^2 ].
*/
float RelHumidityRelTolerance = GetRelHumTolerance(SensorType)/GetRelHumidity();
float TemperatureRelTolerance = 0.0575*GetTempTolerance(Cel, SensorType);
RelHumidityRelTolerance*=RelHumidityRelTolerance;
TemperatureRelTolerance*=TemperatureRelTolerance;
return ReturnValueIfError(
GetAbsHumidity(Scale) * sqrt(RelHumidityRelTolerance+TemperatureRelTolerance)
);
}
void SHT3x::SetMode(SHT3xMode Mode)
{
switch (Mode)
{
case Single_HighRep_ClockStretch:
{
_MeasMSB=0x2C;
_MeasLSB=0x06;
break;
}
case Single_MediumRep_ClockStretch:
{
_MeasMSB=0x2C;
_MeasLSB=0x0D;
break;
}
case Single_LowRep_ClockStretch:
{
_MeasMSB=0x2C;
_MeasLSB=0x10;
break;
}
case Single_HighRep_NoClockStretch:
{
_MeasMSB=0x24;
_MeasLSB=0x00;
break;
}
case Single_MediumRep_NoClockStretch:
{
_MeasMSB=0x24;
_MeasLSB=0x0B;
break;
}
case Single_LowRep_NoClockStretch:
{
_MeasMSB=0x24;
_MeasLSB=0x16;
break;
}
default:
{
_MeasMSB=0x2C;
_MeasLSB=0x06;
break;
}
}
}
void SHT3x::SetTemperatureCalibrationFactors(CalibrationFactors TemperatureCalibration)
{
_TemperatureCalibration = TemperatureCalibration;
}
void SHT3x::SetRelHumidityCalibrationFactors(CalibrationFactors RelHumidityCalibration)
{
_RelHumidityCalibration = RelHumidityCalibration;
}
void SHT3x::SetTemperatureCalibrationPoints(CalibrationPoints SensorValues, CalibrationPoints Reference)
{
_TemperatureCalibration.Factor = (Reference.Second - Reference.First) / (SensorValues.Second - SensorValues.First);
_TemperatureCalibration.Shift = Reference.First - _TemperatureCalibration.Factor * SensorValues.First;
}
void SHT3x::SetRelHumidityCalibrationPoints(CalibrationPoints SensorValues, CalibrationPoints Reference)
{
_RelHumidityCalibration.Factor = (Reference.Second - Reference.First) / (SensorValues.Second - SensorValues.First);
_RelHumidityCalibration.Shift = Reference.First - _RelHumidityCalibration.Factor * SensorValues.First;
}
void SHT3x::SoftReset(int SDA)
{
SendCommand(0x30, 0xA2, SDA);
}
void SHT3x::HardReset()
{
if (_HardResetPin<100) //MEGA2560 have only 68 pins
{
pinMode(_HardResetPin,OUTPUT);
digitalWrite(_HardResetPin,LOW);
delayMicroseconds(1); //Reset pin have to be LOW at least 350ns, so 1 usec is enough. I think, there is no need for micros() for 1 usec.
digitalWrite(_HardResetPin,HIGH);
}
}
void SHT3x::HeaterOn(int SDA)
{
SendCommand(0x30, 0x6D, SDA);
}
void SHT3x::HeaterOff(int SDA)
{
SendCommand(0x30, 0x66, SDA);
}
void SHT3x::SetAddress(uint8_t Address)
{
if ((Address == 0x44) || (Address == 0x45))
{
_Address = Address;
}
else
{
_Error = WrongAddress;
}
}
void SHT3x::SetUpdateInterval(uint32_t UpdateIntervalMillisec)
{
if (UpdateIntervalMillisec > 0)
{
_UpdateIntervalMillisec = UpdateIntervalMillisec;
}
}
void SHT3x::SetTimeout(uint32_t TimeoutMillisec)
{
if (TimeoutMillisec > 0)
{
_TimeoutMillisec = _TimeoutMillisec;
}
}
void SHT3x::I2CError(uint8_t I2Canswer)
{
switch (I2Canswer)
{
case 0:
{
_Error = noError;
break;
}
case 1:
{
_Error = TooMuchData;
break;
}
case 2:
{
_Error = AddressNACK;
break;
}
case 3:
{
_Error = DataNACK;
break;
}
case 4:
{
_Error = OtherI2CError;
break;
}
default:
{
_Error = UnexpectedError;
break;
}
}
}
void SHT3x::SendCommand(uint8_t MSB, uint8_t LSB, int SDA)
{
if (_OperationEnabled)
{
//Everything is OK, nothing to do
}
else
{
if (SDA==0) Wire.begin();
// else Wire1.begin();
_OperationEnabled = true;
}
if (SDA==0)
{
Wire.beginTransmission(_Address);
// Send Soft Reset command
Wire.write(MSB);
Wire.write(LSB);
// Stop I2C transmission
uint8_t success = Wire.endTransmission();
}
// else
// {
// Wire1.beginTransmission(_Address);
// // Send Soft Reset command
// Wire1.write(MSB);
// Wire1.write(LSB);
// // Stop I2C transmission
// uint8_t success = Wire1.endTransmission();
// }
}
bool SHT3x::CRC8(uint8_t MSB, uint8_t LSB, uint8_t CRC)
{
/*
* Name : CRC-8
* Poly : 0x31 x^8 + x^5 + x^4 + 1
* Init : 0xFF
* Revert: false
* XorOut: 0x00
* Check : for 0xBE,0xEF CRC is 0x92
*/
uint8_t crc = 0xFF;
uint8_t i;
crc ^=MSB;
for (i = 0; i < 8; i++)
crc = crc & 0x80 ? (crc << 1) ^ 0x31 : crc << 1;
crc ^= LSB;
for (i = 0; i < 8; i++)
crc = crc & 0x80 ? (crc << 1) ^ 0x31 : crc << 1;
if (crc==CRC)
{
return true;
}
else
{
return false;
}
}
uint8_t SHT3x::GetError()
{
return _Error;
}
float SHT3x::ReturnValueIfError(float Value)
{
if (_Error == noError)
{
return Value;
}
else
{
if (_ValueIfError == PrevValue)
{
return Value;
}
else
{
return 0.;
}
}
}
void SHT3x::ToReturnIfError(ValueIfError Value)
{
_ValueIfError = Value;
}