mirror of
https://github.com/adafruit/DHT-sensor-library.git
synced 2023-10-23 22:20:38 +03:00
Compare commits
33 Commits
Author | SHA1 | Date | |
---|---|---|---|
b781f5998d | |||
01490aa8de | |||
261e997a90 | |||
bdd2756ed1 | |||
07da1cd001 | |||
b3b0b08d6d | |||
be8859e28c | |||
bf86a4068c | |||
f8d3497fac | |||
4bc4ec3fc4 | |||
efcca31a7f | |||
fa129747e8 | |||
dff65098bb | |||
73409c143c | |||
5550a040c2 | |||
dc71b006b6 | |||
562a4878a9 | |||
b429942ce8 | |||
7787bdd186 | |||
2dfa77993d | |||
f771939337 | |||
6b8b1ccdb9 | |||
a1169d20b4 | |||
3bba771f8c | |||
5211b2d701 | |||
a607a50475 | |||
b07db5af21 | |||
761f4f4fb9 | |||
16e61f1084 | |||
519393f42e | |||
cad6977ba8 | |||
53375d8d70 | |||
89937d5d25 |
94
DHT.cpp
94
DHT.cpp
@ -7,6 +7,7 @@ written by Adafruit Industries
|
||||
#include "DHT.h"
|
||||
|
||||
#define MIN_INTERVAL 2000
|
||||
#define TIMEOUT -1
|
||||
|
||||
DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
|
||||
_pin = pin;
|
||||
@ -18,7 +19,7 @@ DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
|
||||
_maxcycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for
|
||||
// reading pulses from DHT sensor.
|
||||
// Note that count is now ignored as the DHT reading algorithm adjusts itself
|
||||
// basd on the speed of the processor.
|
||||
// based on the speed of the processor.
|
||||
}
|
||||
|
||||
void DHT::begin(void) {
|
||||
@ -27,8 +28,8 @@ void DHT::begin(void) {
|
||||
// Using this value makes sure that millis() - lastreadtime will be
|
||||
// >= MIN_INTERVAL right away. Note that this assignment wraps around,
|
||||
// but so will the subtraction.
|
||||
_lastreadtime = -MIN_INTERVAL;
|
||||
DEBUG_PRINT("Max clock cycles: "); DEBUG_PRINTLN(_maxcycles, DEC);
|
||||
_lastreadtime = millis() - MIN_INTERVAL;
|
||||
DEBUG_PRINT("DHT max clock cycles: "); DEBUG_PRINTLN(_maxcycles, DEC);
|
||||
}
|
||||
|
||||
//boolean S == Scale. True == Fahrenheit; False == Celcius
|
||||
@ -38,16 +39,19 @@ float DHT::readTemperature(bool S, bool force) {
|
||||
if (read(force)) {
|
||||
switch (_type) {
|
||||
case DHT11:
|
||||
case DHT12:
|
||||
f = data[2];
|
||||
f += (data[3] & 0x0f) * 0.1;
|
||||
if (data[2] & 0x80) {
|
||||
f *= -1;
|
||||
}
|
||||
if(S) {
|
||||
f = convertCtoF(f);
|
||||
}
|
||||
break;
|
||||
case DHT22:
|
||||
case DHT21:
|
||||
f = data[2] & 0x7F;
|
||||
f *= 256;
|
||||
f += data[3];
|
||||
f = ((word)(data[2] & 0x7F)) << 8 | data[3];
|
||||
f *= 0.1;
|
||||
if (data[2] & 0x80) {
|
||||
f *= -1;
|
||||
@ -71,16 +75,15 @@ float DHT::convertFtoC(float f) {
|
||||
|
||||
float DHT::readHumidity(bool force) {
|
||||
float f = NAN;
|
||||
if (read()) {
|
||||
if (read(force)) {
|
||||
switch (_type) {
|
||||
case DHT11:
|
||||
f = data[0];
|
||||
case DHT12:
|
||||
f = data[0] + data[1] * 0.1;
|
||||
break;
|
||||
case DHT22:
|
||||
case DHT21:
|
||||
f = data[0];
|
||||
f *= 256;
|
||||
f += data[1];
|
||||
f = ((word)data[0]) << 8 | data[1];
|
||||
f *= 0.1;
|
||||
break;
|
||||
}
|
||||
@ -89,7 +92,15 @@ float DHT::readHumidity(bool force) {
|
||||
}
|
||||
|
||||
//boolean isFahrenheit: True == Fahrenheit; False == Celcius
|
||||
float DHT::computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit) {
|
||||
float DHT::computeHeatIndex(bool isFahrenheit) {
|
||||
float hi = computeHeatIndex(readTemperature(isFahrenheit), readHumidity(),
|
||||
isFahrenheit);
|
||||
return isFahrenheit ? hi : convertFtoC(hi);
|
||||
}
|
||||
|
||||
//boolean isFahrenheit: True == Fahrenheit; False == Celcius
|
||||
float DHT::computeHeatIndex(float temperature, float percentHumidity,
|
||||
bool isFahrenheit) {
|
||||
// Using both Rothfusz and Steadman's equations
|
||||
// http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
|
||||
float hi;
|
||||
@ -120,11 +131,11 @@ float DHT::computeHeatIndex(float temperature, float percentHumidity, bool isFah
|
||||
return isFahrenheit ? hi : convertFtoC(hi);
|
||||
}
|
||||
|
||||
boolean DHT::read(bool force) {
|
||||
bool DHT::read(bool force) {
|
||||
// Check if sensor was read less than two seconds ago and return early
|
||||
// to use last reading.
|
||||
uint32_t currenttime = millis();
|
||||
if (!force && ((currenttime - _lastreadtime) < 2000)) {
|
||||
if (!force && ((currenttime - _lastreadtime) < MIN_INTERVAL)) {
|
||||
return _lastresult; // return last correct measurement
|
||||
}
|
||||
_lastreadtime = currenttime;
|
||||
@ -137,37 +148,44 @@ boolean DHT::read(bool force) {
|
||||
|
||||
// Go into high impedence state to let pull-up raise data line level and
|
||||
// start the reading process.
|
||||
digitalWrite(_pin, HIGH);
|
||||
delay(250);
|
||||
pinMode(_pin, INPUT_PULLUP);
|
||||
delay(1);
|
||||
|
||||
// First set data line low for 20 milliseconds.
|
||||
// First set data line low for a period according to sensor type
|
||||
pinMode(_pin, OUTPUT);
|
||||
digitalWrite(_pin, LOW);
|
||||
delay(20);
|
||||
switch(_type) {
|
||||
case DHT22:
|
||||
case DHT21:
|
||||
delayMicroseconds(1100); // data sheet says "at least 1ms"
|
||||
break;
|
||||
case DHT11:
|
||||
default:
|
||||
delay(20); //data sheet says at least 18ms, 20ms just to be safe
|
||||
break;
|
||||
}
|
||||
|
||||
uint32_t cycles[80];
|
||||
{
|
||||
// Turn off interrupts temporarily because the next sections are timing critical
|
||||
// and we don't want any interruptions.
|
||||
InterruptLock lock;
|
||||
|
||||
// End the start signal by setting data line high for 40 microseconds.
|
||||
digitalWrite(_pin, HIGH);
|
||||
delayMicroseconds(40);
|
||||
pinMode(_pin, INPUT_PULLUP);
|
||||
|
||||
// Now start reading the data line to get the value from the DHT sensor.
|
||||
pinMode(_pin, INPUT_PULLUP);
|
||||
delayMicroseconds(10); // Delay a bit to let sensor pull data line low.
|
||||
delayMicroseconds(60); // Delay a bit to let sensor pull data line low.
|
||||
|
||||
// Turn off interrupts temporarily because the next sections
|
||||
// are timing critical and we don't want any interruptions.
|
||||
InterruptLock lock;
|
||||
|
||||
// First expect a low signal for ~80 microseconds followed by a high signal
|
||||
// for ~80 microseconds again.
|
||||
if (expectPulse(LOW) == 0) {
|
||||
DEBUG_PRINTLN(F("Timeout waiting for start signal low pulse."));
|
||||
if (expectPulse(LOW) == TIMEOUT) {
|
||||
DEBUG_PRINTLN(F("DHT timeout waiting for start signal low pulse."));
|
||||
_lastresult = false;
|
||||
return _lastresult;
|
||||
}
|
||||
if (expectPulse(HIGH) == 0) {
|
||||
DEBUG_PRINTLN(F("Timeout waiting for start signal high pulse."));
|
||||
if (expectPulse(HIGH) == TIMEOUT) {
|
||||
DEBUG_PRINTLN(F("DHT timeout waiting for start signal high pulse."));
|
||||
_lastresult = false;
|
||||
return _lastresult;
|
||||
}
|
||||
@ -191,8 +209,8 @@ boolean DHT::read(bool force) {
|
||||
for (int i=0; i<40; ++i) {
|
||||
uint32_t lowCycles = cycles[2*i];
|
||||
uint32_t highCycles = cycles[2*i+1];
|
||||
if ((lowCycles == 0) || (highCycles == 0)) {
|
||||
DEBUG_PRINTLN(F("Timeout waiting for pulse."));
|
||||
if ((lowCycles == TIMEOUT) || (highCycles == TIMEOUT)) {
|
||||
DEBUG_PRINTLN(F("DHT timeout waiting for pulse."));
|
||||
_lastresult = false;
|
||||
return _lastresult;
|
||||
}
|
||||
@ -207,7 +225,7 @@ boolean DHT::read(bool force) {
|
||||
// stored data.
|
||||
}
|
||||
|
||||
DEBUG_PRINTLN(F("Received:"));
|
||||
DEBUG_PRINTLN(F("Received from DHT:"));
|
||||
DEBUG_PRINT(data[0], HEX); DEBUG_PRINT(F(", "));
|
||||
DEBUG_PRINT(data[1], HEX); DEBUG_PRINT(F(", "));
|
||||
DEBUG_PRINT(data[2], HEX); DEBUG_PRINT(F(", "));
|
||||
@ -221,7 +239,7 @@ boolean DHT::read(bool force) {
|
||||
return _lastresult;
|
||||
}
|
||||
else {
|
||||
DEBUG_PRINTLN(F("Checksum failure!"));
|
||||
DEBUG_PRINTLN(F("DHT checksum failure!"));
|
||||
_lastresult = false;
|
||||
return _lastresult;
|
||||
}
|
||||
@ -235,14 +253,18 @@ boolean DHT::read(bool force) {
|
||||
// in the very latest IDE versions):
|
||||
// https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/wiring_pulse.c
|
||||
uint32_t DHT::expectPulse(bool level) {
|
||||
#if (F_CPU > 16000000L)
|
||||
uint32_t count = 0;
|
||||
#else
|
||||
uint16_t count = 0; // To work fast enough on slower AVR boards
|
||||
#endif
|
||||
// On AVR platforms use direct GPIO port access as it's much faster and better
|
||||
// for catching pulses that are 10's of microseconds in length:
|
||||
#ifdef __AVR
|
||||
uint8_t portState = level ? _bit : 0;
|
||||
while ((*portInputRegister(_port) & _bit) == portState) {
|
||||
if (count++ >= _maxcycles) {
|
||||
return 0; // Exceeded timeout, fail.
|
||||
return TIMEOUT; // Exceeded timeout, fail.
|
||||
}
|
||||
}
|
||||
// Otherwise fall back to using digitalRead (this seems to be necessary on ESP8266
|
||||
@ -250,7 +272,7 @@ uint32_t DHT::expectPulse(bool level) {
|
||||
#else
|
||||
while (digitalRead(_pin) == level) {
|
||||
if (count++ >= _maxcycles) {
|
||||
return 0; // Exceeded timeout, fail.
|
||||
return TIMEOUT; // Exceeded timeout, fail.
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
8
DHT.h
8
DHT.h
@ -30,6 +30,7 @@ written by Adafruit Industries
|
||||
|
||||
// Define types of sensors.
|
||||
#define DHT11 11
|
||||
#define DHT12 12
|
||||
#define DHT22 22
|
||||
#define DHT21 21
|
||||
#define AM2301 21
|
||||
@ -42,9 +43,10 @@ class DHT {
|
||||
float readTemperature(bool S=false, bool force=false);
|
||||
float convertCtoF(float);
|
||||
float convertFtoC(float);
|
||||
float computeHeatIndex(bool isFahrenheit=true);
|
||||
float computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit=true);
|
||||
float readHumidity(bool force=false);
|
||||
boolean read(bool force=false);
|
||||
bool read(bool force=false);
|
||||
|
||||
private:
|
||||
uint8_t data[5];
|
||||
@ -64,10 +66,14 @@ class DHT {
|
||||
class InterruptLock {
|
||||
public:
|
||||
InterruptLock() {
|
||||
#if !defined(ARDUINO_ARCH_NRF52)
|
||||
noInterrupts();
|
||||
#endif
|
||||
}
|
||||
~InterruptLock() {
|
||||
#if !defined(ARDUINO_ARCH_NRF52)
|
||||
interrupts();
|
||||
#endif
|
||||
}
|
||||
|
||||
};
|
||||
|
16
DHT_U.cpp
16
DHT_U.cpp
@ -37,6 +37,9 @@ void DHT_Unified::setName(sensor_t* sensor) {
|
||||
case DHT11:
|
||||
strncpy(sensor->name, "DHT11", sizeof(sensor->name) - 1);
|
||||
break;
|
||||
case DHT12:
|
||||
strncpy(sensor->name, "DHT12", sizeof(sensor->name) - 1);
|
||||
break;
|
||||
case DHT21:
|
||||
strncpy(sensor->name, "DHT21", sizeof(sensor->name) - 1);
|
||||
break;
|
||||
@ -57,6 +60,9 @@ void DHT_Unified::setMinDelay(sensor_t* sensor) {
|
||||
case DHT11:
|
||||
sensor->min_delay = 1000000L; // 1 second (in microseconds)
|
||||
break;
|
||||
case DHT12:
|
||||
sensor->min_delay = 2000000L; // 2 second (in microseconds)
|
||||
break;
|
||||
case DHT21:
|
||||
sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
|
||||
break;
|
||||
@ -105,6 +111,11 @@ void DHT_Unified::Temperature::getSensor(sensor_t* sensor) {
|
||||
sensor->min_value = 0.0F;
|
||||
sensor->resolution = 2.0F;
|
||||
break;
|
||||
case DHT12:
|
||||
sensor->max_value = 60.0F;
|
||||
sensor->min_value = -20.0F;
|
||||
sensor->resolution = 0.5F;
|
||||
break;
|
||||
case DHT21:
|
||||
sensor->max_value = 80.0F;
|
||||
sensor->min_value = -40.0F;
|
||||
@ -159,6 +170,11 @@ void DHT_Unified::Humidity::getSensor(sensor_t* sensor) {
|
||||
sensor->min_value = 20.0F;
|
||||
sensor->resolution = 5.0F;
|
||||
break;
|
||||
case DHT12:
|
||||
sensor->max_value = 95.0F;
|
||||
sensor->min_value = 20.0F;
|
||||
sensor->resolution = 5.0F;
|
||||
break;
|
||||
case DHT21:
|
||||
sensor->max_value = 100.0F;
|
||||
sensor->min_value = 0.0F;
|
||||
|
@ -11,12 +11,12 @@
|
||||
#include <DHT.h>
|
||||
#include <DHT_U.h>
|
||||
|
||||
#define DHTPIN 2 // Pin which is connected to the DHT sensor.
|
||||
#define DHTPIN 2 // Pin connected to the DHT sensor.
|
||||
|
||||
// Uncomment the type of sensor in use:
|
||||
//#define DHTTYPE DHT11 // DHT 11
|
||||
#define DHTTYPE DHT22 // DHT 22 (AM2302)
|
||||
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
|
||||
//#define DHTTYPE DHT11 // DHT 11
|
||||
#define DHTTYPE DHT22 // DHT 22 (AM2302)
|
||||
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
|
||||
|
||||
// See guide for details on sensor wiring and usage:
|
||||
// https://learn.adafruit.com/dht/overview
|
||||
@ -26,33 +26,32 @@ DHT_Unified dht(DHTPIN, DHTTYPE);
|
||||
uint32_t delayMS;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.begin(9600);
|
||||
// Initialize device.
|
||||
dht.begin();
|
||||
Serial.println("DHTxx Unified Sensor Example");
|
||||
Serial.println(F("DHTxx Unified Sensor Example"));
|
||||
// Print temperature sensor details.
|
||||
sensor_t sensor;
|
||||
dht.temperature().getSensor(&sensor);
|
||||
Serial.println("------------------------------------");
|
||||
Serial.println("Temperature");
|
||||
Serial.print ("Sensor: "); Serial.println(sensor.name);
|
||||
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
|
||||
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
|
||||
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C");
|
||||
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C");
|
||||
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C");
|
||||
Serial.println("------------------------------------");
|
||||
Serial.println(F("------------------------------------"));
|
||||
Serial.println(F("Temperature Sensor"));
|
||||
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
|
||||
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
|
||||
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
|
||||
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
|
||||
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
|
||||
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
|
||||
Serial.println(F("------------------------------------"));
|
||||
// Print humidity sensor details.
|
||||
dht.humidity().getSensor(&sensor);
|
||||
Serial.println("------------------------------------");
|
||||
Serial.println("Humidity");
|
||||
Serial.print ("Sensor: "); Serial.println(sensor.name);
|
||||
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
|
||||
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
|
||||
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%");
|
||||
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%");
|
||||
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%");
|
||||
Serial.println("------------------------------------");
|
||||
Serial.println(F("Humidity Sensor"));
|
||||
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
|
||||
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
|
||||
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
|
||||
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%"));
|
||||
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%"));
|
||||
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%"));
|
||||
Serial.println(F("------------------------------------"));
|
||||
// Set delay between sensor readings based on sensor details.
|
||||
delayMS = sensor.min_delay / 1000;
|
||||
}
|
||||
@ -61,24 +60,24 @@ void loop() {
|
||||
// Delay between measurements.
|
||||
delay(delayMS);
|
||||
// Get temperature event and print its value.
|
||||
sensors_event_t event;
|
||||
sensors_event_t event;
|
||||
dht.temperature().getEvent(&event);
|
||||
if (isnan(event.temperature)) {
|
||||
Serial.println("Error reading temperature!");
|
||||
Serial.println(F("Error reading temperature!"));
|
||||
}
|
||||
else {
|
||||
Serial.print("Temperature: ");
|
||||
Serial.print(F("Temperature: "));
|
||||
Serial.print(event.temperature);
|
||||
Serial.println(" *C");
|
||||
Serial.println(F("°C"));
|
||||
}
|
||||
// Get humidity event and print its value.
|
||||
dht.humidity().getEvent(&event);
|
||||
if (isnan(event.relative_humidity)) {
|
||||
Serial.println("Error reading humidity!");
|
||||
Serial.println(F("Error reading humidity!"));
|
||||
}
|
||||
else {
|
||||
Serial.print("Humidity: ");
|
||||
Serial.print(F("Humidity: "));
|
||||
Serial.print(event.relative_humidity);
|
||||
Serial.println("%");
|
||||
Serial.println(F("%"));
|
||||
}
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ DHT dht(DHTPIN, DHTTYPE);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("DHTxx test!");
|
||||
Serial.println(F("DHTxx test!"));
|
||||
|
||||
dht.begin();
|
||||
}
|
||||
@ -44,7 +44,7 @@ void loop() {
|
||||
|
||||
// Check if any reads failed and exit early (to try again).
|
||||
if (isnan(h) || isnan(t) || isnan(f)) {
|
||||
Serial.println("Failed to read from DHT sensor!");
|
||||
Serial.println(F("Failed to read from DHT sensor!"));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -53,17 +53,15 @@ void loop() {
|
||||
// Compute heat index in Celsius (isFahreheit = false)
|
||||
float hic = dht.computeHeatIndex(t, h, false);
|
||||
|
||||
Serial.print("Humidity: ");
|
||||
Serial.print(F("Humidity: "));
|
||||
Serial.print(h);
|
||||
Serial.print(" %\t");
|
||||
Serial.print("Temperature: ");
|
||||
Serial.print(F("% Temperature: "));
|
||||
Serial.print(t);
|
||||
Serial.print(" *C ");
|
||||
Serial.print(F("°C "));
|
||||
Serial.print(f);
|
||||
Serial.print(" *F\t");
|
||||
Serial.print("Heat index: ");
|
||||
Serial.print(F("°F Heat index: "));
|
||||
Serial.print(hic);
|
||||
Serial.print(" *C ");
|
||||
Serial.print(F("°C "));
|
||||
Serial.print(hif);
|
||||
Serial.println(" *F");
|
||||
Serial.println(F("°F"));
|
||||
}
|
||||
|
14
keywords.txt
14
keywords.txt
@ -12,11 +12,11 @@ DHT KEYWORD1
|
||||
# Methods and Functions (KEYWORD2)
|
||||
###########################################
|
||||
|
||||
begin KEYWORD2
|
||||
readTemperature KEYWORD2
|
||||
convertCtoF KEYWORD2
|
||||
convertFtoC KEYWORD2
|
||||
computeHeatIndex KEYWORD2
|
||||
readHumidity KEYWORD2
|
||||
read KEYWORD2
|
||||
begin KEYWORD2
|
||||
readTemperature KEYWORD2
|
||||
convertCtoF KEYWORD2
|
||||
convertFtoC KEYWORD2
|
||||
computeHeatIndex KEYWORD2
|
||||
readHumidity KEYWORD2
|
||||
read KEYWORD2
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=DHT sensor library
|
||||
version=1.3.0
|
||||
version=1.3.1
|
||||
author=Adafruit
|
||||
maintainer=Adafruit <info@adafruit.com>
|
||||
sentence=Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors
|
||||
|
Reference in New Issue
Block a user