Skip to content

Commit

Permalink
Add DHT20_ERROR_BYTES_ALL_ZERO error message and more (#5)
Browse files Browse the repository at this point in the history
* fix #4 DHT20_ERROR_BYTES_ALL_ZERO error condition.
* fix keywords
* add readStatus()  fix _readStatus()
* add setWireTimeout(250000, true);  // in comments
  • Loading branch information
RobTillaart authored Sep 16, 2022
1 parent 8081424 commit 6232655
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 26 deletions.
31 changes: 23 additions & 8 deletions DHT20.cpp
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -34,6 +42,7 @@ DHT20::DHT20(TwoWire *wire)
bool DHT20::begin()
{
_wire->begin();
// _wire->setWireTimeout(DHT20_WIRE_TIME_OUT, true);
return isConnected();
}

Expand All @@ -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
Expand All @@ -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();
}

Expand Down Expand Up @@ -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();
}


Expand Down
9 changes: 6 additions & 3 deletions DHT20.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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
Expand Down Expand Up @@ -63,6 +64,8 @@ class DHT20
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;
Expand All @@ -75,7 +78,7 @@ class DHT20

int _requestData();
int _readData();
int _readStatus();
uint8_t _readStatus();
uint8_t _bits[7];

uint8_t _crc8(uint8_t *ptr, uint8_t len);
Expand Down
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -134,19 +143,21 @@ 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

#### could

- improve unit tests.
- investigate
- status register bits
- sensor calibration (website aosong?)
- check for optimizations.
- mainly for asynchronous
Expand Down
3 changes: 3 additions & 0 deletions examples/DHT20/DHT20.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/DHT20_plotter/DHT20_plotter.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// FILE: DHT20 _plotter.ino
// FILE: DHT20_plotter.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
//
Expand Down
47 changes: 47 additions & 0 deletions examples/DHT20_read_status/DHT20_read_status.ino
Original file line number Diff line number Diff line change
@@ -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 --
3 changes: 3 additions & 0 deletions examples/DHT20_test_esp/DHT20_test_esp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=DHT20
version=0.1.1
version=0.1.2
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for I2C DHT20 temperature and humidity sensor.
Expand Down
3 changes: 2 additions & 1 deletion test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand All @@ -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);
Expand Down

0 comments on commit 6232655

Please sign in to comment.