- fixed issue #123

This commit is contained in:
RobTillaart 2019-03-05 18:45:36 +01:00
parent 3eaf92ee24
commit 9d714913c7
6 changed files with 157 additions and 13 deletions

View File

@ -1,13 +1,17 @@
//
// FILE: SHT31.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.2
// DATE: 2019-02-08
// PURPOSE: Class for SHT31 I2C temperature humidity sensor
// https://www.adafruit.com/product/2857
//
// HISTORY:
// 0.1.0 - 2019-02-08 initial version
// 0.1.1 - 2019-02-18 add description readStatus(),
// async interface
// 0.1.2 - 2019-03-05 fix issue #123 - error in humidity
// stable version
//
// Released to the public domain
//
@ -16,7 +20,8 @@
#define SHT31_READ_STATUS 0xF32D
#define SHT31_CLEAR_STATUS 0x3041
#define SHT31_RESET 0x30A2
#define SHT31_SOFT_RESET 0x30A2
#define SHT31_HARD_RESET 0x0006
#define SHT31_MEASUREMENT_FAST 0x2416
#define SHT31_MEASUREMENT_SLOW 0x2400
#define SHT31_HEAT_ON 0x306D
@ -54,15 +59,16 @@ bool SHT31::read(bool fast)
writeCmd(SHT31_MEASUREMENT_SLOW);
delay(15); // table 4 datasheet
}
// TODO 5 read bytes would be sufficient when not fast / no CRC...
readBytes(6, (uint8_t*) &buffer[0]);
if (!fast)
{
// TODO 5 read bytes would be sufficient when not fast...
// TODO check CRC here
// TODO rv = false;
}
float raw = (buffer[0] << 8) + buffer[1];
uint16_t raw = (buffer[0] << 8) + buffer[1];
temperature = raw * (175.0 / 65535) - 45;
raw = (buffer[3] << 8) + buffer[4];
humidity = raw * (100.0 / 65535);
@ -76,16 +82,44 @@ uint16_t SHT31::readStatus()
{
uint32_t status = 0;
writeCmd(SHT31_READ_STATUS);
readBytes(3, (uint8_t*) &status);
writeCmd(SHT31_READ_STATUS); // page 13 datasheet
readBytes(3, (uint8_t*) &status); // 16 bit status + CRC
// TODO CRC check
return status;
// bit - description
// ==================
// 15 Alert pending status
// '0': no pending alerts
// '1': at least one pending alert - default
// 14 Reserved 0
// 13 Heater status
// '0 : Heater OFF - default
// '1 : Heater ON
// 12 Reserved '0
// 11 Humidity tracking alert
// '0 : no alert - default
// '1 : alert
// 10 Temp tracking alert
// '0 : no alert - default
// '1 : alert
// 9:5 Reserved '00000
// 4 System reset detected
// '0': no reset since last clear status register command
// '1': reset detected (hard or soft reset command or supply fail)
// 3:2 Reserved 00
// 1 Command status
// '0': last cmd executed successfully
// '1': last cmd not processed. Invalid or failed checksum
// 0 Write data checksum status
// '0': checksum of last write correct
// '1': checksum of last write transfer failed
}
// hard reset 0x0006 on addr 0x00 see datasheet
void SHT31::reset()
{
writeCmd(SHT31_RESET);
delay(1); // table 4 datasheet
writeCmd(SHT31_SOFT_RESET); // SHT31_HARD_RESET not implemented yet
delay(1); // table 4 datasheet // 100ms for hardreset
}
void SHT31::heatOn()
@ -98,6 +132,30 @@ void SHT31::heatOff()
writeCmd(SHT31_HEAT_OFF);
}
void SHT31::requestData()
{
writeCmd(SHT31_MEASUREMENT_SLOW);
_lastRequest = millis();
}
bool SHT31::dataReady()
{
return ((millis() - _lastRequest) > 15);
}
void SHT31::readData()
{
uint8_t buffer[6];
readBytes(6, (uint8_t*) &buffer[0]);
float raw = (buffer[0] << 8) + buffer[1];
temperature = raw * (175.0 / 65535) - 45;
raw = (buffer[3] << 8) + buffer[4];
humidity = raw * (100.0 / 65535);
_lastRead = millis();
}
//////////////////////////////////////////////////////////
void SHT31::writeCmd(uint16_t cmd)

View File

@ -1,7 +1,7 @@
//
// FILE: SHT31.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.2
// DATE: 2019-02-08
// PURPOSE: Class for SHT31 I2C temperature humidity sensor
// https://www.adafruit.com/product/2857
@ -16,7 +16,7 @@
#include "Arduino.h"
#include "Wire.h"
#define SHT31_LIB_VERSION "0.1.0"
#define SHT31_LIB_VERSION "0.1.2"
class SHT31
{
@ -33,18 +33,28 @@ public:
uint32_t lastRead() { return _lastRead; };
void reset();
// do not use heater for long periods,
// use it for max 3 minutes to heat up
// and let it cool down an equal period.
void heatOn();
void heatOff();
float humidity;
float temperature;
// ASYNC INTERFACE
void requestData();
bool dataReady();
void readData();
private:
void writeCmd(uint16_t cmd);
void readBytes(uint8_t n, uint8_t *val);
uint8_t _addr;
uint32_t _lastRead;
uint32_t _lastRequest; // for async interface
};
#endif

View File

@ -0,0 +1,54 @@
//
// FILE: SHT31_I2Cspeed
// AUTHOR: Rob Tillaart
// VERSION: 0.0.1
// PURPOSE: demo
//
// HISTORY:
#include "Wire.h"
#include "SHT31.h"
uint32_t start;
uint32_t stop;
SHT31 sht;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("SHT31_LIB_VERSION: \t");
Serial.println(SHT31_LIB_VERSION);
Wire.begin();
sht.begin(0x44);
Wire.setClock(100000);
uint16_t stat = sht.readStatus();
Serial.print(stat, HEX);
}
void loop()
{
for (uint32_t I2Cfreq = 100000; I2Cfreq < 900000; I2Cfreq += 50000)
{
Serial.print(I2Cfreq/1000);
Wire.setClock(I2Cfreq);
test();
}
}
void test()
{
start = micros();
sht.read(true); // default = true/fast slow = false
stop = micros();
Serial.print("\t");
Serial.print(stop - start);
Serial.print("\t");
Serial.print(sht.temperature, 1);
Serial.print("\t");
Serial.println(sht.humidity, 1);
delay(100);
}

View File

@ -0,0 +1,22 @@
#######################################
# Syntax Coloring Map For AnalogKeypad
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
SHT31 KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
#######################################
# Instances (KEYWORD2)
#######################################
#######################################
# Constants (LITERAL1)
#######################################

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/Arduino.git"
},
"version":"0.1.0",
"version":"0.1.2",
"frameworks": "arduino",
"platforms": "*",
"export": {

View File

@ -1,5 +1,5 @@
name=SHT31
version=0.1.0
version=0.1.2
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for SHT31 Temperature Humidity