Oscar Silva / Sht31

Fork of Sht31 by Andrea Corrado

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Sht31.cpp Source File

Sht31.cpp

00001 /***************************************************
00002   This is a library for the SHT31 Digital Humidity & Temp Sht31
00003 
00004   Designed specifically to work with the SHT31 Digital Sht31 from Adafruit
00005   ----> https://www.adafruit.com/products/2857
00006 
00007   These displays use I2C to communicate, 2 pins are required to
00008   interface
00009   Adafruit invests time and resources providing this open source code,
00010   please support Adafruit and open-source hardware by purchasing
00011   products from Adafruit!
00012 
00013   Written by Limor Fried/Ladyada for Adafruit Industries.
00014   BSD license, all text above must be included in any redistribution
00015  ****************************************************/
00016 
00017 #include "Sht31.h"
00018 #include "mbed.h"
00019 
00020 Sht31::Sht31(PinName sda, PinName scl, bool i2caddr) : _i2c(sda, scl) {
00021     _i2caddr = !i2caddr ? (0x44 << 1) : (0x45 << 1);
00022     reset();
00023     readStatus();
00024 }
00025 
00026 float Sht31::readTemperature(void) {
00027     if (! readTempHum()) return NAN;
00028     return temp;
00029 }
00030 
00031 float Sht31::readHumidity(void) {
00032     if (! readTempHum()) return NAN;
00033     return humidity;
00034 }
00035 
00036 void Sht31::reset(void) {
00037     writeCommand(SHT31_SOFTRESET);
00038     wait_ms(10);
00039 }
00040 
00041 uint16_t Sht31::readStatus(void) {
00042     writeCommand(SHT31_READSTATUS);
00043     char val[1];
00044     _i2c.read(_i2caddr, val, 1);
00045     uint16_t stat = val[0];
00046     stat <<= 8;
00047     _i2c.read(_i2caddr, val, 1);
00048     stat |= val[0];
00049     // printf("0x%X\r\n", stat);
00050     return stat;
00051 }
00052 
00053 void Sht31::writeCommand(uint16_t cmd) {
00054     char buf[2];
00055     buf[0] = (cmd >> 8);
00056     buf[1] = (cmd & 0xFF);
00057     _i2c.write(_i2caddr, buf, 2);
00058 }
00059 
00060 bool Sht31::readTempHum(void) {
00061     char readbuffer[6];
00062 
00063     writeCommand(SHT31_MEAS_HIGHREP);
00064 
00065     wait_ms(500);
00066     _i2c.read(_i2caddr, readbuffer, 6);
00067     
00068     uint16_t ST, SRH;
00069     ST = readbuffer[0];
00070     ST <<= 8;
00071     ST |= readbuffer[1];
00072 
00073     if (readbuffer[2] != crc8((uint8_t *) readbuffer, 2)) {
00074         return false;
00075     }
00076 
00077     SRH = readbuffer[3];
00078     SRH <<= 8;
00079     SRH |= readbuffer[4];
00080 
00081     if (readbuffer[5] != crc8((uint8_t *) readbuffer+3, 2)) {
00082         return false;
00083     }
00084 
00085     double stemp = ST;
00086     stemp *= 175;
00087     stemp /= 0xffff;
00088     stemp = -45 + stemp;
00089     temp = stemp;
00090 
00091     double shum = SRH;
00092     shum *= 100;
00093     shum /= 0xFFFF;
00094 
00095     humidity = shum;
00096 
00097     return true;
00098 }
00099 
00100 
00101 bool Sht31::readTempHum(float &_temp, float &_humidity) {
00102     char readbuffer[6];
00103 
00104     writeCommand(SHT31_MEAS_HIGHREP);
00105 
00106     wait_ms(500);
00107     _i2c.read(_i2caddr, readbuffer, 6);
00108     
00109     uint16_t ST, SRH;
00110     ST = readbuffer[0];
00111     ST <<= 8;
00112     ST |= readbuffer[1];
00113 
00114     if (readbuffer[2] != crc8((uint8_t *) readbuffer, 2)) {
00115         return false;
00116     }
00117 
00118     SRH = readbuffer[3];
00119     SRH <<= 8;
00120     SRH |= readbuffer[4];
00121 
00122     if (readbuffer[5] != crc8((uint8_t *) readbuffer+3, 2)) {
00123         return false;
00124     }
00125 
00126     double stemp = ST;
00127     stemp *= 175;
00128     stemp /= 0xffff;
00129     stemp = -45 + stemp;
00130     temp = stemp;
00131     _temp = temp;
00132 
00133     double shum = SRH;
00134     shum *= 100;
00135     shum /= 0xFFFF;
00136 
00137     humidity = shum;
00138     _humidity = humidity;
00139 
00140     return true;
00141 }
00142 
00143 
00144 uint8_t Sht31::crc8(const uint8_t *data, int len) {
00145     const uint8_t POLYNOMIAL(0x31);
00146     uint8_t crc(0xFF);
00147 
00148     for ( int j = len; j; --j ) {
00149         crc ^= *data++;
00150 
00151         for ( int i = 8; i; --i ) {
00152             crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1);
00153         }
00154     }
00155     return crc;
00156 }