working with mbed os 5.5

Dependents:   Mbed-Connect-Cloud-Demo Mbed-Connect-Cloud-Demo mbed-cloud-connect-sensor-laser-distance2 mbed-cloud-connect-sensor-laser-distance2 ... more

Fork of Sht31 by Robert Taylor

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) : _i2c(sda, scl) {
00021     _i2caddr = (0x44 << 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 uint8_t Sht31::crc8(const uint8_t *data, int len) {
00101     const uint8_t POLYNOMIAL(0x31);
00102     uint8_t crc(0xFF);
00103 
00104     for ( int j = len; j; --j ) {
00105         crc ^= *data++;
00106 
00107         for ( int i = 8; i; --i ) {
00108             crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1);
00109         }
00110     }
00111     return crc;
00112 }