0.3.1 SHT2x

This commit is contained in:
Rob Tillaart 2023-09-11 10:13:31 +02:00
parent 710c148906
commit 87bbbfd8c4
8 changed files with 196 additions and 63 deletions

View File

@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.3.1] - 2023-09-10
- fix #21 example for elaborated async.
- improve async interface
- add **getRequestType()** to async interface
- add three request constants to .h file
- add **bool requestReady()** checks both.
- update readme.md
- minor edits
## [0.3.0] - 2023-03-26
- fix setResolution #13, #18
- fix heater settings
@ -41,16 +51,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fix getEIDB() bug.
## [0.1.3] - 2021-12-28
- update library.json
- update library.json
- update license
- minor edits
## [0.1.2] - 2021-09-29
## [0.1.2] - 2021-09-29
- Add Si70xx derived classes
- add getEIDA(), getEIDB(), getFirmwareVersion()
- update build-CI, readme.md badges
## [0.1.1] - 2021-09-28
## [0.1.1] - 2021-09-28
- Add HTU2x derived classes,
- update readme.md + add some status info

View File

@ -1,9 +1,12 @@
[![Arduino CI](https://github.com/robtillaart/SHT2x/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![JSON check](https://github.com/RobTillaart/SHT2x/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/SHT2x/actions/workflows/jsoncheck.yml)
[![Arduino CI](https://github.com/RobTillaart/SHT2x/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/SHT2x/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/SHT2x/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/SHT2x/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/SHT2x/actions/workflows/jsoncheck.yml)
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/SHT2x.svg)](https://github.com/RobTillaart/SHT2x/issues)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/SHT2x/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/SHT2x.svg?maxAge=3600)](https://github.com/RobTillaart/SHT2x/releases)
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/SHT2x.svg)](https://registry.platformio.org/libraries/robtillaart/SHT2x)
# SHT2x
@ -39,6 +42,13 @@ If you want to use more on one I2C bus one needs either an I2C multiplexer
or one should switch sensors on/off like the select in SPI communication.
#### Related
- https://github.com/RobTillaart/SHT31
- https://github.com/RobTillaart/SHT85
- https://github.com/RobTillaart/tinySHT2x
## Interface
```cpp
@ -89,13 +99,24 @@ Note: The raw temperature and raw humidity are ideal to minimize storage or to m
Experimental since 0.2.2 this interface can change in the future
Discussion see https://github.com/RobTillaart/SHT2x/issues/16
- **bool requestTemperature()**
- **bool requestHumidity()**
- **bool reqTempReady()**
- **bool reqHumReady()**
- **bool readTemperature()**
- **bool readHumidity()**
- **uint32_t lastRequest()**
- **bool requestTemperature()** starts new temperature request.
Overrules optional existing / running request.
- **bool requestHumidity()** starts new humidity request.
Overrules optional existing / running request.
- **bool reqTempReady()** returns true if temperature request is ready.
- **bool reqHumReady()** returns true if humidity request is ready.
- **bool requestReady()** checks if either temperature or humidity is ready.
- **bool readTemperature()** calculates the temperature from raw measurement.
- **bool readHumidity()** calculates the humidity from raw measurement.
- **uint32_t lastRequest()** timestamp of last request.
- **uint8_t getRequestType()** get current request type.
| Value | Symbolic | Description | Notes |
|:-------:|:----------------------------|:------------------------------|:-----------|
| 0x00 | SHT2x_REQ_NONE | no request pending | |
| 0x01 | SHT2x_REQ_TEMPERATURE | temperature request pending | |
| 0x02 | SHT2x_REQ_HUMIDITY | humidity request pending | |
TODO elaborate documentation.
@ -157,20 +178,20 @@ To be tested.
#### Status fields
From HTU20 datasheet
From HTU20 datasheet (read for details).
| bits | value | meaning |
|:------:|:------:|:--------------------|
| 00 | 0 | open circuit |
| 01 | 1 | temperature reading |
| 10 | 2 | humidity reading |
| 11 | 3 | closed circuit |
| bits | value | Symbolic | Description |
|:------:|:-------:|:------------------------------|:----------------------|
| 00 | 0 | SHT2x_STATUS_OPEN_CIRCUIT | open circuit |
| 01 | 1 | SHT2x_STATUS_TEMPERATURE | temperature reading |
| 10 | 2 | SHT2x_STATUS_HUMIDITY | humidity reading |
| 11 | 3 | SHT2x_STATUS_CLOSED_CIRCUIT | closed circuit |
#### Resolution
**Warning experimental**
- needs more testing as results are not in line with the datasheet.
- needs more testing as the results are not in line with the datasheet.
- only tested on a HTUxx sensor.
- tested with **SHT2X_resolution.ino**
@ -178,7 +199,7 @@ From HTU20 datasheet
- **uint8_t getResolution()** returns resolution set 0..3.
Datasheet SHT20 Table 8: (resolution)
Datasheet SHT20 Table 8: resolution
| RES | Humidity | Temperature |
|:-----:|:----------:|:-------------:|
@ -187,7 +208,8 @@ Datasheet SHT20 Table 8: (resolution)
| 2 | 10 bit | 13 bit |
| 3 | 11 bit | 11 bit |
Datasheet SHT20 Table 7: (timing) and results of real measurements.
Datasheet SHT20 Table 7: timing versus results of real measurements.
( https://github.com/RobTillaart/SHT2x/pull/11 )
| RES | HUM | TEMP | TOTAL | REAL |
@ -209,28 +231,21 @@ Timing in milliseconds.
## Future
#### Must
- improve documentation
- reorganize interface
- async documentation
- clean up code.
#### 0.4.0
- add crc8 check (need sensor to test)
- improve error handling (all code paths)
- investigate blocking delay() in read
- add offset for temperature and humidity
#### ASYNC 0.4.0
improvements for interface.
- **bool requestReady()** checks both.
- **bool requestPending()** checks.
- **uint8_t getRequestType()** returns 0, 1, 2.
- async documentation
#### Should
- test test test
- get hardware
- add examples
@ -238,15 +253,23 @@ improvements for interface.
- performance different resolutions
- test battery
#### Could
- fix TODO in code (.cpp and .h) and documentation
- update unit tests
- add type info in derived classes?
#### Wont
- add **getSerialNumber()**
**getEIDA()** and **getEIDB()** covers this
## Support
If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.
Thank you,

View File

@ -1,7 +1,7 @@
//
// FILE: SHT2x.cpp
// AUTHOR: Rob Tillaart, Viktor Balint
// VERSION: 0.3.0
// VERSION: 0.3.1
// DATE: 2021-09-25
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
// URL: https://github.com/RobTillaart/SHT2x
@ -26,10 +26,6 @@
#define SHT2x_USRREG_BATTERY 0x20
#define SHT2x_USRREG_HEATER 0x04
#define SHT2x_REQ_NONE 0x00
#define SHT2x_REQ_TEMPERATURE 0x01
#define SHT2x_REQ_HUMIDITY 0x02
//////////////////////////////////////////////////////////////
//
@ -39,7 +35,7 @@ SHT2x::SHT2x()
{
_lastRead = 0;
_lastRequest = 0;
_requestType = 0;
_requestType = SHT2x_REQ_NONE;
_rawTemperature = 0;
_rawHumidity = 0;
_heatTimeout = 0;
@ -165,6 +161,11 @@ bool SHT2x::reqHumReady()
}
bool SHT2x::requestReady()
{
return (reqTempReady() || reqHumReady());
}
bool SHT2x::readTemperature()
{
uint8_t buffer[3];
@ -177,7 +178,7 @@ bool SHT2x::readTemperature()
if (crc8(buffer, 2) != buffer[2])
{
_error = SHT2x_ERR_CRC_TEMP;
// return false; // do not fail yet
// return false; // do not fail yet
}
_rawTemperature = buffer[0] << 8;
_rawTemperature += buffer[1];
@ -187,7 +188,7 @@ bool SHT2x::readTemperature()
_requestType = SHT2x_REQ_NONE;
_status = buffer[1] & 0x0003;
if (_status == 0xFF) // TODO != 0x01 (need HW to test)
if (_status == 0xFF) // TODO != 0x01 (need HW to test)
{
_error = SHT2x_ERR_READBYTES;
return false;
@ -207,7 +208,7 @@ bool SHT2x::readHumidity()
if (crc8(buffer, 2) != buffer[2])
{
_error = SHT2x_ERR_CRC_HUM;
// return false; // do not fail yet
// return false; // do not fail yet
}
_rawHumidity = buffer[0] << 8;
_rawHumidity += buffer[1];
@ -235,20 +236,26 @@ uint32_t SHT2x::lastRequest()
}
uint8_t SHT2x::getRequestType()
{
return _requestType;
}
/////////////////////////////////////////////////////////
//
// TEMPERATURE AND HUMIDTY
//
float SHT2x::getTemperature()
{
// par 6.2
// par 6.2
return -46.85 + (175.72 / 65536.0) * _rawTemperature;
}
float SHT2x::getHumidity()
{
// par 6.1
// par 6.1
return -6.0 + (125.0 / 65536.0) * _rawHumidity;
}
@ -347,7 +354,7 @@ bool SHT2x::heatOff()
if (writeCmd(SHT2x_WRITE_USER_REGISTER, userReg) == false)
{
_error = SHT2x_ERR_HEATER_OFF; // can be serious!
_error = SHT2x_ERR_HEATER_OFF; // can be serious!
return false;
}
_heaterStop = millis();
@ -362,7 +369,7 @@ bool SHT2x::isHeaterOn()
{
return false;
}
// did not exceed time out
// did not exceed time out
if (millis() - _heaterStart < (_heatTimeout * 1000UL))
{
return true;
@ -380,7 +387,7 @@ bool SHT2x::setHeaterLevel(uint8_t level)
}
uint8_t heaterReg = 0;
writeCmd(0x11); // Read Heater Control Register
writeCmd(0x11); // Read Heater Control Register
if (readBytes(1, (uint8_t *) &heaterReg, 5) == false)
{
_error = SHT2x_ERR_READBYTES;
@ -400,7 +407,7 @@ bool SHT2x::setHeaterLevel(uint8_t level)
bool SHT2x::getHeaterLevel(uint8_t & level)
{
uint8_t heaterReg = 0;
writeCmd(0x11); // Read Heater Control Register
writeCmd(0x11); // Read Heater Control Register
if (readBytes(1, (uint8_t *) &heaterReg, 5) == false)
{
_error = SHT2x_ERR_READBYTES;
@ -541,8 +548,8 @@ bool SHT2x::batteryOK()
//
uint8_t SHT2x::crc8(const uint8_t *data, uint8_t len)
{
// CRC-8 formula from page 14 of SHT spec pdf
// Sensirion_Humidity_Sensors_SHT2x_CRC_Calculation.pdf
// CRC-8 formula from page 14 of SHT spec pdf
// Sensirion_Humidity_Sensors_SHT2x_CRC_Calculation.pdf
const uint8_t POLY = 0x31;
uint8_t crc = 0x00;

View File

@ -2,7 +2,7 @@
//
// FILE: SHT2x.h
// AUTHOR: Rob Tillaart, Viktor Balint
// VERSION: 0.3.0
// VERSION: 0.3.1
// DATE: 2021-09-25
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
// URL: https://github.com/RobTillaart/SHT2x
@ -13,7 +13,7 @@
#include "Wire.h"
#define SHT2x_LIB_VERSION (F("0.3.0"))
#define SHT2x_LIB_VERSION (F("0.3.1"))
// fields getStatus
@ -32,12 +32,17 @@
#define SHT2x_ERR_NOT_CONNECT 0x84
#define SHT2x_ERR_CRC_TEMP 0x85
#define SHT2x_ERR_CRC_HUM 0x86
#define SHT2x_ERR_CRC_STATUS 0x87 // not used
#define SHT2x_ERR_CRC_STATUS 0x87 // not used
#define SHT2x_ERR_HEATER_COOLDOWN 0x88
#define SHT2x_ERR_HEATER_ON 0x89
// 0.2.0
#define SHT2x_ERR_RESOLUTION 0x8A
// Request types
#define SHT2x_REQ_NONE 0x00
#define SHT2x_REQ_TEMPERATURE 0x01
#define SHT2x_REQ_HUMIDITY 0x02
class SHT2x
@ -80,7 +85,7 @@ public:
//
uint8_t getStatus();
// lastRead is in milliSeconds since start
// lastRead is in milliSeconds since start
uint32_t lastRead();
@ -96,13 +101,14 @@ public:
bool heatOn();
bool heatOff();
bool isHeaterOn(); // is the sensor still heating up?
bool isHeaterOn(); // is the sensor still heating up?
bool setHeaterLevel(uint8_t level); // level = 0..15
bool getHeaterLevel(uint8_t & level); // 0..15
bool setHeaterLevel(uint8_t level); // level = 0..15
bool getHeaterLevel(uint8_t & level); // 0..15
int getError(); // clears error flag
// reading clears error flag
int getError();
/////////////////////////////////////////////////////////
@ -143,9 +149,11 @@ public:
bool requestHumidity();
bool reqTempReady();
bool reqHumReady();
bool requestReady();
bool readTemperature();
bool readHumidity();
uint32_t lastRequest();
uint8_t getRequestType();
/////////////////////////////////////////////////////////

View File

@ -0,0 +1,75 @@
//
// FILE: SHT2x_demo_async_HT.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo asynchronous interface
// URL: https://github.com/RobTillaart/SHT2x
//
// needs version 0.3.1 or higher.
#include "Wire.h"
#include "SHT2x.h"
SHT2x sht;
// TIMING SCHEDULE
uint32_t lastTemp = 0;
uint32_t intervalTemp = 2000;
uint32_t lastHum = 0;
uint32_t intervalHum = 5000;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION);
sht.begin();
uint8_t stat = sht.getStatus();
Serial.print(stat, HEX);
Serial.println();
sht.requestTemperature();
}
void loop()
{
uint32_t now = millis();
// schedule and handle temperature
if ((now - lastTemp > intervalTemp) && (sht.getRequestType() == 0x00))
{
sht.requestTemperature();
}
if (sht.reqTempReady())
{
lastTemp = now;
sht.readTemperature();
Serial.print("TEMP:\t");
Serial.println(sht.getTemperature(), 1);
}
// schedule and handle humidity
if ((now - lastHum > intervalHum) && (sht.getRequestType() == 0x00))
{
sht.requestHumidity();
}
if (sht.reqHumReady())
{
lastTemp = now;
sht.readHumidity();
Serial.print("TEMP:\t");
Serial.println(sht.getHumidity(), 1);
}
// do other things here
delay(1000);
}
// -- END OF FILE --

View File

@ -8,6 +8,10 @@ SHT21 KEYWORD1
SHT25 KEYWORD1
HTU20 KEYWORD1
HTU21 KEYWORD1
Si7013 KEYWORD1
Si7020 KEYWORD1
Si7021 KEYWORD1
GY21 KEYWORD1
# Methods and Functions (KEYWORD2)
@ -42,9 +46,11 @@ requestTemperatureta KEYWORD2
requestHumidity KEYWORD2
reqTempReady KEYWORD2
reqHumReady KEYWORD2
requestReady KEYWORD2
readTemperature KEYWORD2
readHumidity KEYWORD2
lastRequest KEYWORD2
getRequestType KEYWORD2
# Instances (KEYWORD2)
@ -69,3 +75,7 @@ SHT2x_STATUS_TEMPERATURE LITERAL1
SHT2x_STATUS_HUMIDITY LITERAL1
SHT2x_STATUS_CLOSED_CIRCUIT LITERAL1
SHT2x_REQ_NONE LITERAL1
SHT2x_REQ_TEMPERATURE LITERAL1
SHT2x_REQ_HUMIDITY LITERAL1

View File

@ -18,9 +18,9 @@
"type": "git",
"url": "https://github.com/RobTillaart/SHT2x.git"
},
"version": "0.3.0",
"version": "0.3.1",
"license": "MIT",
"frameworks": "arduino",
"frameworks": "*",
"platforms": "*",
"headers": "SHT2x.h"
}

View File

@ -1,5 +1,5 @@
name=SHT2x
version=0.3.0
version=0.3.1
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for the I2C SHT20 SHT21 SHT25 series temperature and humidity sensor.