0.1.2 DHT20

This commit is contained in:
rob tillaart 2022-09-16 19:27:01 +02:00
parent cb1107e950
commit 7b4cd9a259
11 changed files with 113 additions and 26 deletions

View File

@ -1,18 +1,26 @@
//
// FILE: DHT20.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// VERSION: 0.1.2
// PURPOSE: Arduino library for DHT20 I2C temperature and humidity sensor.
//
// HISTORY:
// 0.1.0 2022-01-11 initial version (based upon DHT20 datasheet)
// 0.1.1 2022-09-10 add hardware schema to readme.md.
// fix async interface (first version)
// 0.1.2 2022-09-16 fix #4 DHT20_ERROR_BYTES_ALL_ZERO error condition.
// fix keywords
// add readStatus() fix _readStatus()
// add setWireTimeout(250000, true); // in comments
#include "DHT20.h"
#define DHT20_ACQUISITION_TIME 85
#define DHT20_ACQUISITION_TIME 85 // milliseconds
// set DHT20_WIRE_TIME_OUT to 0 to disable.
#define DHT20_WIRE_TIME_OUT 250000 // microseconds
const uint8_t DHT20_ADDRESS = 0x38;
@ -34,6 +42,7 @@ DHT20::DHT20(TwoWire *wire)
bool DHT20::begin()
{
_wire->begin();
// _wire->setWireTimeout(DHT20_WIRE_TIME_OUT, true);
return isConnected();
}
@ -47,6 +56,7 @@ bool DHT20::begin(const uint8_t dataPin, const uint8_t clockPin)
} else {
_wire->begin();
}
// _wire->setWireTimeout(DHT20_WIRE_TIME_OUT, true);
return isConnected();
}
#endif
@ -66,13 +76,17 @@ int DHT20::read()
// check lastRead!
int status = _requestData();
if (status < 0) return status;
// wait for measurement ready
while ((millis() - _lastRequest) <= DHT20_ACQUISITION_TIME)
{
yield();
delay(1);
}
_readData();
// read the measurement
status = _readData();
if (status < 0) return status;
// convert it to meaningfull data
return convert();
}
@ -129,30 +143,31 @@ int DHT20::_readData()
if (bytes == 0) return DHT20_ERROR_CONNECT;
if (bytes < length) return DHT20_MISSING_BYTES;
bool allZero = true;
for (int i = 0; i < bytes; i++)
{
_bits[i] = _wire->read();
// if (_bits[i] < 0x10) Serial.print(0);
// Serial.print(_bits[i], HEX);
// Serial.print(" ");
allZero = allZero && (_bits[i] == 0);
}
// Serial.println();
if (allZero) return DHT20_ERROR_BYTES_ALL_ZERO;
_lastRead = millis();
return bytes;
}
int DHT20::_readStatus()
uint8_t DHT20::_readStatus()
{
// GET CONNECTION
_wire->beginTransmission(DHT20_ADDRESS);
_wire->write(0xAC);
_wire->write(0x71);
_wire->write(0x00);
return _wire->endTransmission();
_wire->endTransmission();
_wire->requestFrom(DHT20_ADDRESS, (uint8_t)1);
return (uint8_t) _wire->read();
}

View File

@ -3,7 +3,7 @@
// FILE: DHT20.h
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for DHT20 I2C temperature and humidity sensor.
// VERSION: 0.1.1
// VERSION: 0.1.2
// HISTORY: See DHT20.cpp
// URL: https://github.com/RobTillaart/DHT20
//
@ -21,12 +21,13 @@
#include "Arduino.h"
#include "Wire.h"
#define DHT20_LIB_VERSION (F("0.1.1"))
#define DHT20_LIB_VERSION (F("0.1.2"))
#define DHT20_OK 0
#define DHT20_ERROR_CHECKSUM -10
#define DHT20_ERROR_CONNECT -11
#define DHT20_MISSING_BYTES -12
#define DHT20_ERROR_BYTES_ALL_ZERO -13
#define DHT20_ACQUISITION_TIME 85
@ -63,6 +64,8 @@ public:
uint32_t lastRead() { return _lastRead; };
uint32_t lastRequest() { return _lastRequest; };
int internalStatus() { return _status; };
// forced read status
uint8_t readStatus() { return _readStatus(); };
private:
float _humidity;
@ -75,7 +78,7 @@ private:
int _requestData();
int _readData();
int _readStatus();
uint8_t _readStatus();
uint8_t _bits[7];
uint8_t _crc8(uint8_t *ptr, uint8_t len);

View File

@ -105,19 +105,28 @@ This can be used to optimize performance a bit. Use with care.
- **uint32_t lastRead()** last time the sensor is read in milliseconds since start.
- **uint32_t lastRequest()** last time a request is made to make a measurement.
- **int internalStatus()** returns the internal status of the sensor. (debug ?).
- **int internalStatus()** returns the internal status of the sensor. (debug ).
- **uint8_t readStatus()** forced read of the status only.
| status bit | meaning |
|:------------:|:---------------------------|
| 7 | busy making measurement |
| 6 - 4 | unknown |
| 3 | 1 = calibrated, 0 is not |
| 2 - 0 | unknown |
### Return codes
TODO: fix incomplete list
| name | value |
|:----------------------|:-------:|
| DHT20_OK | 00 |
| DHT20_ERROR_CHECKSUM | -10 |
| DHT20_ERROR_CONNECT | -11 |
| DHT20_MISSING_BYTES | -12 |
| name | value | notes |
|:----------------------------|:-------:|:--------|
| DHT20_OK | 00 | OK
| DHT20_ERROR_CHECKSUM | -10 | values might be OK if they are like recent previous ones.
| DHT20_ERROR_CONNECT | -11 | check connection
| DHT20_MISSING_BYTES | -12 | check connection
| DHT20_ERROR_BYTES_ALL_ZERO | -13 | check connection
## Operation
@ -134,11 +143,14 @@ See examples
- check return codes etc.
- add missing error codes
- **read()** should check lastRead() and return ERROR_LASTREAD
- add status shortcuts
- add **bool isCalibrated()**
- add **bool isMeasuring()**
- add **bool isIdle()**
#### should
- test more in detail
- test on ESP32
- add examples
- asynchronous
@ -146,7 +158,6 @@ See examples
- improve unit tests.
- investigate
- status register bits
- sensor calibration (website aosong?)
- check for optimizations.
- mainly for asynchronous

View File

@ -77,6 +77,9 @@ void loop()
case DHT20_MISSING_BYTES:
Serial.print("Missing bytes");
break;
case DHT20_ERROR_BYTES_ALL_ZERO:
Serial.print("All bytes read zero");
break;
default:
Serial.print("Unknown error");
break;

View File

@ -1,5 +1,5 @@
//
// FILE: DHT20 _plotter.ino
// FILE: DHT20_plotter.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
//

View File

@ -0,0 +1,47 @@
//
// FILE: DHT20_read_status.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
//
// Always check datasheet - front view
//
// +--------------+
// VDD ----| 1 |
// SDA ----| 2 DHT20 |
// GND ----| 3 |
// SCL ----| 4 |
// +--------------+
#include "DHT20.h"
DHT20 DHT;
uint8_t count = 0;
void setup()
{
DHT.begin(); // ESP32 default pins 21 22
Wire.setClock(400000);
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DHT20 LIBRARY VERSION: ");
Serial.println(DHT20_LIB_VERSION);
Serial.println();
delay(2000);
}
void loop()
{
int status = DHT.readStatus();
Serial.println(status);
delay(1000);
}
// -- END OF FILE --

View File

@ -57,6 +57,9 @@ void loop()
case DHT20_MISSING_BYTES:
Serial.print("Missing bytes,\t");
break;
case DHT20_ERROR_BYTES_ALL_ZERO:
Serial.print("All bytes read zero");
break;
default:
Serial.print("Unknown error,\t");
break;

View File

@ -22,12 +22,16 @@ getTempOffset KEYWORD2
lastRead KEYWORD2
lastRequest KEYWORD2
internalStatus() KEYWORD2
internalStatus KEYWORD2
readStatus KEYWORD2
# Constants (LITERAL1)
DHT20_LIB_VERSION LITERAL2
DHT20_OK LITERAL2
DHT20_ERROR_CHECKSUM LITERAL1
DHT20_ERROR_CONNECT LITERAL1
DHT20_MISSING_BYTES LITERAL1
DHT20_ERROR_BYTES_ALL_ZERO LITERAL1

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/DHT20.git"
},
"version": "0.1.1",
"version": "0.1.2",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=DHT20
version=0.1.1
version=0.1.2
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for I2C DHT20 temperature and humidity sensor.

View File

@ -45,6 +45,7 @@ unittest(test_constants)
assertEqual(-10, DHT20_ERROR_CHECKSUM);
assertEqual(-11, DHT20_ERROR_CONNECT);
assertEqual(-12, DHT20_MISSING_BYTES);
assertEqual(-13, DHT20_ERROR_BYTES_ALL_ZERO);
}
@ -58,7 +59,7 @@ unittest(test_constructor)
assertEqualFloat(0, DHT.getHumOffset(), 0.001);
DHT.begin();
assertEqual(DHT20_ERROR_CHECKSUM, DHT.read());
assertEqual(DHT20_ERROR_CONNECT, DHT.read());
// assertEqualFloat(0, DHT.getTemperature(), 0.001);
// assertEqualFloat(0, DHT.getHumidity(), 0.001);