-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuFire_SHT3x.cpp
180 lines (158 loc) · 3.29 KB
/
uFire_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
#include "uFire_SHT3x.h"
float uFire::SHT3x::_tempC = 0;
float uFire::SHT3x::_tempF = 0;
float uFire::SHT3x::_vpd_kPa = 0;
float uFire::SHT3x::_dew_pointC = 0;
float uFire::SHT3x::_dew_pointF = 0;
float uFire::SHT3x::_RH = 0;
int uFire::SHT3x::_status = 0;
namespace uFire
{
bool SHT3x::begin(TwoWire &wirePort, uint8_t address)
{
_address = address;
_i2cPort = &wirePort;
return connected();
}
bool SHT3x::connected()
{
_i2cPort->beginTransmission(_address);
uint8_t retval = _i2cPort->endTransmission();
if (retval)
{
return false;
}
else
{
return true;
}
}
int SHT3x::measure()
{
_measure_all();
if (_status == STATUS_NO_ERROR)
{
_vpd();
_dew_point();
}
else
{
_tempC = 0;
_tempF = 0;
_vpd_kPa = 0;
_dew_pointC = 0;
_dew_pointF = 0;
_RH = 0;
}
return status;
}
float SHT3x::_dew_point()
{
float tem = -1.0 * _tempC;
float esdp = 6.112 * exp(-1.0 * 17.67 * tem / (243.5 - tem));
float ed = _RH / 100.0 * esdp;
float eln = log(ed / 6.112);
_dew_pointC = -243.5 * eln / (eln - 17.67);
_dew_pointF = (_dew_pointC * 1.8) + 32;
return _dew_pointC;
}
void SHT3x::_measure_all()
{
uint8_t buffer[6];
uint16_t raw;
_reset();
_send_command(SHT3x_MEASUREMENT);
delay(15);
_read_bytes(6, (uint8_t *)&buffer[0]);
if ((buffer[2] = _crc8(buffer, 2)))
{
raw = (buffer[0] << 8) + buffer[1];
_tempC = raw * (175.0 / 65535) - 45;
_tempF = (_tempC * 1.8) + 32;
}
else
{
_tempC = 0;
_tempF = 0;
_vpd_kPa = 0;
_dew_pointC = 0;
_dew_pointF = 0;
_RH = 0;
_status = STATUS_CRC_ERROR;
return;
}
if ((buffer[5] = _crc8(buffer + 3, 2)))
{
raw = (buffer[3] << 8) + buffer[4];
_RH = raw * (100.0 / 65535);
}
else
{
_tempC = 0;
_tempF = 0;
_vpd_kPa = 0;
_dew_pointC = 0;
_dew_pointF = 0;
_RH = 0;
_status = STATUS_CRC_ERROR;
return;
}
}
void SHT3x::_reset()
{
_send_command(SHT3x_SOFT_RESET);
delay(1);
}
float SHT3x::_vpd()
{
float es = 0.61078 * exp(17.2694 * _tempC / (_tempC + 238.3));
float ae = _RH / 100 * es;
_vpd_kPa = es - ae;
return _vpd_kPa;
}
bool SHT3x::_send_command(uint16_t command)
{
_i2cPort->beginTransmission(_address);
_i2cPort->write(command >> 8);
_i2cPort->write(command & 0xFF);
_i2cPort->endTransmission();
if (_i2cPort->endTransmission() != 0)
{
_status = STATUS_NOT_CONNECTED;
return false;
}
return true;
}
bool SHT3x::_read_bytes(uint8_t n, uint8_t *val)
{
int r = _i2cPort->requestFrom(_address, n);
if (r == n)
{
for (uint8_t i = 0; i < n; i++)
{
val[i] = _i2cPort->read();
}
_status = STATUS_NO_ERROR;
return true;
}
else
{
_status = STATUS_NOT_CONNECTED;
}
return false;
}
bool SHT3x::_crc8(const uint8_t *data, uint8_t len)
{
const uint8_t pol(0x31);
uint8_t crc(0xFF);
for (uint8_t j = len; j; --j)
{
crc ^= *data++;
for (uint8_t i = 8; i; --i)
{
crc = (crc & 0x80) ? (crc << 1) ^ pol : (crc << 1);
}
}
return crc;
}
}