-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSHT3x.cpp
More file actions
47 lines (35 loc) · 1.02 KB
/
SHT3x.cpp
File metadata and controls
47 lines (35 loc) · 1.02 KB
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
#include "SHT3x.h"
SHT3x::SHT3x(uint8_t i2c_addr, TwoWire *theWire)
{
this->i2c_addr = i2c_addr;
this->wire = theWire;
}
bool SHT3x::readTemperatureHumidity(float *temperature, float *humidity)
{
unsigned int data[6];
// Start I2C Transmission
this->wire->beginTransmission(i2c_addr);
// Send measurement command
this->wire->write(0x2C);
this->wire->write(0x06);
// Stop I2C transmission
if (this->wire->endTransmission()!=0) {
return false;
}
delay(200);
// Request 6 bytes of data
this->wire->requestFrom(i2c_addr, (uint8_t) 6);
// Read 6 bytes of data
// cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc
for (int i=0;i<6;i++) {
data[i]=this->wire->read();
};
delay(50);
if (this->wire->available()!=0) {
return false;
}
// Convert the data
if (temperature != NULL) *temperature = ((((data[0] * 256.0) + data[1]) * 175) / 65535.0) - 45;
if (humidity != NULL) *humidity = ((((data[3] * 256.0) + data[4]) * 100) / 65535.0);
return true;
}