little modification for best utilization
Fork of Sht31 by
Diff: Sht31.cpp
- Revision:
- 3:c4bd8cd5d566
- Parent:
- 2:c84a60326ecf
diff -r c84a60326ecf -r c4bd8cd5d566 Sht31.cpp --- a/Sht31.cpp Mon Sep 11 13:51:03 2017 +0000 +++ b/Sht31.cpp Wed Oct 03 13:51:56 2018 +0000 @@ -17,8 +17,8 @@ #include "Sht31.h" #include "mbed.h" -Sht31::Sht31(PinName sda, PinName scl) : _i2c(sda, scl) { - _i2caddr = (0x44 << 1); +Sht31::Sht31(PinName sda, PinName scl, bool i2caddr) : _i2c(sda, scl) { + _i2caddr = !i2caddr ? (0x44 << 1) : (0x45 << 1); reset(); readStatus(); } @@ -97,6 +97,50 @@ return true; } + +bool Sht31::readTempHum(float &_temp, float &_humidity) { + char readbuffer[6]; + + writeCommand(SHT31_MEAS_HIGHREP); + + wait_ms(500); + _i2c.read(_i2caddr, readbuffer, 6); + + uint16_t ST, SRH; + ST = readbuffer[0]; + ST <<= 8; + ST |= readbuffer[1]; + + if (readbuffer[2] != crc8((uint8_t *) readbuffer, 2)) { + return false; + } + + SRH = readbuffer[3]; + SRH <<= 8; + SRH |= readbuffer[4]; + + if (readbuffer[5] != crc8((uint8_t *) readbuffer+3, 2)) { + return false; + } + + double stemp = ST; + stemp *= 175; + stemp /= 0xffff; + stemp = -45 + stemp; + temp = stemp; + _temp = temp; + + double shum = SRH; + shum *= 100; + shum /= 0xFFFF; + + humidity = shum; + _humidity = humidity; + + return true; +} + + uint8_t Sht31::crc8(const uint8_t *data, int len) { const uint8_t POLYNOMIAL(0x31); uint8_t crc(0xFF);