Skip to content

Commit

Permalink
add reset() + add lastRead() (#49)
Browse files Browse the repository at this point in the history
* add reset()
* add lastRead()
  • Loading branch information
RobTillaart committed Jan 13, 2021
1 parent b7105e9 commit 20bd7dd
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 56 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017-2020 Rob Tillaart
Copyright (c) 2017-2021 Rob Tillaart

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
39 changes: 23 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ Added Arduino-CI support + **gettype()** now tries to determine type if not know
18. (0.4.2)
Fix negative temperatures. Tested with DHTNew_debug.ino and hexdump in .cpp and a freezer.
Note: testing in a freezer is not so good for humidity readings.
19. (0.4.3)
Added **reset()** to reset internal variables when a sensor blocks this might help.
Added **lastRead()** to return time the sensor is last read. (in millis).


## DHT PIN layout from left to right
Expand All @@ -88,36 +91,36 @@ Note: testing in a freezer is not so good for humidity readings.

## Specification DHT22

| | | |
| Model | DHT22 | Notes |
|:----|:----|:----|
| Model | DHT22 |
| Power supply | 3.3 - 6 V DC |
| Output signal | digital signal via single-bus |
| Sensing element | Polymer capacitor |
| Operating range | humidity 0-100% RH | temperature -40~80° Celsius |
| Accuracy humidity | ±2% RH(Max ±5% RH) | temperature < ±0.5° Celsius |
| Resolution or sensitivity | humidity 0.1% RH | temperature 0.1° Celsius |
| Repeatability humidity | ±1% RH | temperature ±0.2° Celsius |
| Humidity hysteresis | ±0.3% RH |
| Long-term Stability | ±0.5% RH/year |
| Sensing period | Average: 2s |
| Interchangeability | fully interchangeable |
| Dimensions | small size 14 x 18 x 5.5 mm; | big size 22 x 28 x 5 mm |
| Power supply | 3.3 - 6 V DC |
| Output signal | digital signal via single-bus |
| Sensing element | Polymer capacitor |
| Operating range | humidity 0-100% RH | temperature -40~80° Celsius |
| Accuracy humidity | ±2% RH(Max ±5% RH) | temperature < ±0.5° Celsius |
| Resolution or sensitivity | humidity 0.1% RH | temperature 0.1° Celsius |
| Repeatability humidity | ±1% RH | temperature ±0.2° Celsius |
| Humidity hysteresis | ±0.3% RH |
| Long-term Stability | ±0.5% RH/year |
| Sensing period | Average: 2s |
| Interchangeability | fully interchangeable |
| Dimensions | small 14 x 18 x 5.5 mm | big 22 x 28 x 5 mm |


## Interface

To elaborated


### Constructor

- **DHTNEW(uint8_t pin)** defines the datapin of the sensor.
- **reset()** might help to reset a sensor behaving badly.
- **getType()** 0 = unknown, 11 or 22.
In case of 0, **getType()** will try to determine type.
- **setType(uint8_t type = 0)** allows to force the type of the sensor.


### Base interface

- **read()** reads a new temperature and humidity from the sensor
- **lastRead()** returns milliseconds since last **read()**
- **getHumidity()** returns last read value (float) or -999 in case of error.
Expand All @@ -126,15 +129,19 @@ Note this error value can be suppressed by **setSuppressError(bool)**.


### Offset

Adding offsets works well in normal range however they might introduce under- or overflow at the ends of the sensor range.

- **setHumOffset(float offset)** typical < ±5% RH.
- **setTempOffset(float offset)** typical < ±2°C.
- **getHumOffset()** idem.
- **getTempOffset()** idem.


### Control

Functions to adjust the communication with the sensor.

- **setDisableIRQ(bool b )** allows or suppresses interrupts during core read function to keep timing as correct as possible. **Note AVR only**
- **getDisableIRQ()** returns the above setting. Default **false**
- **setWaitForReading(bool b )** flag to enforce a blocking wait.
Expand Down
99 changes: 64 additions & 35 deletions dhtnew.cpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
//
// FILE: dhtnew.cpp
// AUTHOR: Rob.Tillaart@gmail.com
// VERSION: 0.4.2
// VERSION: 0.4.3
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
// URL: https://github.com/RobTillaart/DHTNEW
//
// HISTORY:
// 0.1.0 2017-07-24 initial version based upon DHTStable
// 0.1.1 2017-07-29 add begin() to determine type once and for all instead of every call + refactor
// 0.1.2 2018-01-08 improved begin() + refactor()
// 0.1.3 2018-01-08 removed begin() + moved detection to read() function
// 0.1.4 2018-04-03 add get-/setDisableIRQ(bool b)
// 0.1.5 2019-01-20 fix negative temperature DHT22 - issue #120
// 0.1.6 2020-04-09 #pragma once, readme.md, own repo
// 0.1.7 2020-05-01 prevent premature read; add waitForReading flag (Kudo's to Mr-HaleYa),
// 0.2.0 2020-05-02 made temperature and humidity private (Kudo's to Mr-HaleYa),
// 0.2.1 2020-05-27 Fix #11 - Adjust bit timing threshold
// 0.2.2 2020-06-08 added ERROR_SENSOR_NOT_READY and differentiate timeout errors
// 0.3.0 2020-06-12 added getReadDelay & setReadDelay to tune reading interval
// removed get/setDisableIRQ; adjusted wakeup timing; refactor
// 0.3.1 2020-07-08 added powerUp() powerDown();
// 0.3.2 2020-07-17 fix #23 added get/setSuppressError(); overrulable DHTLIB_INVALID_VALUE
// 0.3.3 2020-08-18 fix #29, create explicit delay between pulling line HIGH and
// waiting for LOW in handshake to trigger the sensor.
// On fast ESP32 this fails because the capacity / voltage of the long wire
// cannot rise fast enough to be read back as HIGH.
// 0.3.4 2020-09-23 Added **waitFor(state, timeout)** to follow timing from datasheet.
// Restored disableIRQ flag as problems occured on AVR. The default of
// this flag on AVR is false so interrupts are allowed.
// This need some investigation
// Fix wake up timing for DHT11 as it does not behave according datasheet.
// fix wakeupDelay bug in setType();
// 0.4.0 2020-11-10 added DHTLIB_WAITING_FOR_READ as return value of read (minor break of interface)
// 0.4.1 2020-11-11 getType() attempts to detect sensor type
// 2020-12-12 add arduino -CI + readme
// 0.4.2 2020-12-15 fix negative temperatures
// HISTORY:
// 0.1.0 2017-07-24 initial version based upon DHTStable
// 0.1.1 2017-07-29 add begin() to determine type once and for all instead of every call + refactor
// 0.1.2 2018-01-08 improved begin() + refactor()
// 0.1.3 2018-01-08 removed begin() + moved detection to read() function
// 0.1.4 2018-04-03 add get-/setDisableIRQ(bool b)
// 0.1.5 2019-01-20 fix negative temperature DHT22 - issue #120
// 0.1.6 2020-04-09 #pragma once, readme.md, own repo
// 0.1.7 2020-05-01 prevent premature read; add waitForReading flag (Kudo's to Mr-HaleYa),
// 0.2.0 2020-05-02 made temperature and humidity private (Kudo's to Mr-HaleYa),
// 0.2.1 2020-05-27 Fix #11 - Adjust bit timing threshold
// 0.2.2 2020-06-08 added ERROR_SENSOR_NOT_READY and differentiate timeout errors
// 0.3.0 2020-06-12 added getReadDelay & setReadDelay to tune reading interval
// removed get/setDisableIRQ; adjusted wakeup timing; refactor
// 0.3.1 2020-07-08 added powerUp() powerDown();
// 0.3.2 2020-07-17 fix #23 added get/setSuppressError(); overrulable DHTLIB_INVALID_VALUE
// 0.3.3 2020-08-18 fix #29, create explicit delay between pulling line HIGH and
// waiting for LOW in handshake to trigger the sensor.
// On fast ESP32 this fails because the capacity / voltage of the long wire
// cannot rise fast enough to be read back as HIGH.
// 0.3.4 2020-09-23 Added **waitFor(state, timeout)** to follow timing from datasheet.
// Restored disableIRQ flag as problems occured on AVR. The default of
// this flag on AVR is false so interrupts are allowed.
// This need some investigation
// Fix wake up timing for DHT11 as it does not behave according datasheet.
// fix wakeupDelay bug in setType();
// 0.4.0 2020-11-10 added DHTLIB_WAITING_FOR_READ as return value of read (minor break of interface)
// 0.4.1 2020-11-11 getType() attempts to detect sensor type
// 2020-12-12 add arduino -CI + readme
// 0.4.2 2020-12-15 fix negative temperatures
// 0.4.3 2021-01-13 add reset(), add lastRead()


#include "dhtnew.h"
#include <stdint.h>


// these defines are not for user to adjust
#define DHTLIB_DHT11_WAKEUP 18
#define DHTLIB_DHT_WAKEUP 1


// READ_DELAY for blocking read
// datasheet: DHT11 = 1000 and DHT22 = 2000
// use setReadDelay() to overrule (at own risk)
Expand All @@ -51,28 +55,48 @@
#define DHTLIB_DHT11_READ_DELAY 1000
#define DHTLIB_DHT22_READ_DELAY 2000


/////////////////////////////////////////////////////
//
// PUBLIC
//
DHTNEW::DHTNEW(uint8_t pin)
{
_dataPin = pin;
reset();
};


void DHTNEW::reset()
{
// Data-bus's free status is high voltage level.
pinMode(_dataPin, OUTPUT);
digitalWrite(_dataPin, HIGH);
_readDelay = 0;
#if defined(__AVR__)

_wakeupDelay = 0;
_type = 0;
_humOffset = 0.0;
_tempOffset = 0.0;
_humidity = 0.0;
_temperature = 0.0;
_lastRead = 0;
_disableIRQ = true;
_waitForRead = false;
_suppressError = false;
_readDelay = 0;
#if defined(__AVR__)
_disableIRQ = false;
#endif
};
#endif
}


uint8_t DHTNEW::getType()
{
if (_type == 0) read();
return _type;
}


void DHTNEW::setType(uint8_t type)
{
if ((type == 0) || (type == 11))
Expand All @@ -87,6 +111,7 @@ void DHTNEW::setType(uint8_t type)
}
}


// return values:
// DHTLIB_OK
// DHTLIB_WAITING_FOR_READ
Expand Down Expand Up @@ -128,6 +153,7 @@ int DHTNEW::read()
return rv;
}


// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
Expand Down Expand Up @@ -209,18 +235,21 @@ int DHTNEW::_read()
return DHTLIB_OK;
}


void DHTNEW::powerUp()
{
digitalWrite(_dataPin, HIGH);
// do a dummy read to sync the sensor
read();
};


void DHTNEW::powerDown()
{
digitalWrite(_dataPin, LOW);
}


/////////////////////////////////////////////////////
//
// PRIVATE
Expand Down
12 changes: 10 additions & 2 deletions dhtnew.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
//
// FILE: dhtnew.h
// AUTHOR: Rob Tillaart
// VERSION: 0.4.2
// VERSION: 0.4.3
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
// URL: https://github.com/RobTillaart/DHTNEW
//
// HISTORY:
// see dhtnew.cpp file


// DHT PIN layout from left to right
// =================================
// FRONT : DESCRIPTION
Expand All @@ -17,9 +18,11 @@
// pin 3 : Not Connected
// pin 4 : GND


#include "Arduino.h"

#define DHTNEW_LIB_VERSION "0.4.2"
#define DHTNEW_LIB_VERSION (F("0.4.2"))


#define DHTLIB_OK 0
#define DHTLIB_ERROR_CHECKSUM -1
Expand Down Expand Up @@ -51,6 +54,10 @@ class DHTNEW

DHTNEW(uint8_t pin);

// resets all internals to construction time
// might help to reset a sensor behaving badly..
void reset();

// 0 = unknown, 11 or 22
uint8_t getType();
void setType(uint8_t type = 0);
Expand Down Expand Up @@ -89,6 +96,7 @@ class DHTNEW
bool getSuppressError() { return _suppressError; };
void setSuppressError(bool b) { _suppressError = b; };


private:
uint8_t _dataPin = 0;
uint8_t _wakeupDelay = 0;
Expand Down
6 changes: 6 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
DHTNEW KEYWORD1

# Methods and Functions (KEYWORD2)
reset KEYWORD2
getType KEYWORD2
setType() KEYWORD2
read KEYWORD2
Expand All @@ -17,6 +18,9 @@ setTempOffset KEYWORD2
getHumOffset KEYWORD2
getTempOffset KEYWORD2

getDisableIRQ KEYWORD2
setDisableIRQ KEYWORD2

getWaitForReading KEYWORD2
setWaitForReading KEYWORD2
getSuppressError KEYWORD2
Expand All @@ -43,3 +47,5 @@ DHTLIB_ERROR_TIMEOUT_C LITERAL1
DHTLIB_ERROR_TIMEOUT_D LITERAL1
DHTLIB_ERROR_SENSOR_NOT_READY LITERAL1
DHTLIB_WAITING_FOR_READ LITERAL1
DHTLIB_BIT_THRESHOLD LITERAL1

2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/DHTNEW.git"
},
"version":"0.4.2",
"version":"0.4.3",
"frameworks": "arduino",
"platforms": "*"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=DHTNEW
version=0.4.2
version=0.4.3
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for DHT temperature and humidity sensor, with automatic sensortype recognition.
Expand Down

0 comments on commit 20bd7dd

Please sign in to comment.