2013-08-28 07:57:45 -04:00
|
|
|
//
|
|
|
|
// FILE: dht.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2021-12-16 10:44:08 -05:00
|
|
|
// VERSION: 0.1.35
|
2020-11-27 05:10:47 -05:00
|
|
|
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino, AVR optimized
|
|
|
|
// URL: https://github.com/RobTillaart/DHTlib
|
|
|
|
// http://arduino.cc/playground/Main/DHTLib
|
2013-08-28 07:57:45 -04:00
|
|
|
//
|
2021-12-16 10:44:08 -05:00
|
|
|
//
|
|
|
|
// HISTORY:
|
|
|
|
// 0.1.35 2021-12-16 update library.json, license, #pragma once,
|
|
|
|
// move private defines to .cpp file
|
|
|
|
// 0.1.34 2021-11-13 fix negative temperature DHT22 again (code from DHTNew)
|
|
|
|
// 0.1.33 2021-02-16 fix #6 T-GO signal in handshake. (needed for long wires)
|
|
|
|
// 0.1.32 2021-02-01 fix negative temperature DHT22 again (code from DHTNew)
|
|
|
|
// 0.1.31 2020-12-15 fix negative temperature DHT22 (code from DHTNew)
|
|
|
|
// 0.1.30 2020-06-30 own repo;
|
|
|
|
// 0.1.29 2018-09-02 fix negative temperature DHT12 - issue #111
|
|
|
|
// 0.1.28 2018-04-03 refactor
|
|
|
|
// 0.1.27 2018-03-26 added _disableIRQ flag
|
|
|
|
// 0.1.26 2017-12-12 explicit support for AM23XX series and DHT12
|
|
|
|
// 0.1.25 2017-09-20 FIX https://github.com/RobTillaart/Arduino/issues/80
|
|
|
|
// 0.1.24 2017-07-27 FIX https://github.com/RobTillaart/Arduino/issues/33 double -> float
|
|
|
|
// 0.1.23 2017-07-24 FIX https://github.com/RobTillaart/Arduino/issues/31
|
|
|
|
// 0.1.22 undo delayMicroseconds() for wakeups larger than 8
|
|
|
|
// 0.1.21 replace delay with delayMicroseconds() + small fix
|
|
|
|
// 0.1.20 Reduce footprint by using uint8_t as error codes. (thanks to chaveiro)
|
|
|
|
// 0.1.19 masking error for DHT11 - FIXED (thanks Richard for noticing)
|
|
|
|
// 0.1.18 version 1.16/17 broke the DHT11 - FIXED
|
|
|
|
// 0.1.17 replaced micros() with adaptive loopcount
|
|
|
|
// removed DHTLIB_INVALID_VALUE
|
|
|
|
// added DHTLIB_ERROR_CONNECT
|
|
|
|
// added DHTLIB_ERROR_ACK_L DHTLIB_ERROR_ACK_H
|
|
|
|
// 0.1.16 masking unused bits (less errors); refactored bits[]
|
|
|
|
// 0.1.15 reduced # micros calls 2->1 in inner loop.
|
|
|
|
// 0.1.14 replace digital read with faster (~3x) code => more robust low MHz machines.
|
|
|
|
//
|
|
|
|
// 0.1.13 fix negative temperature
|
|
|
|
// 0.1.12 support DHT33 and DHT44 initial version
|
|
|
|
// 0.1.11 renamed DHTLIB_TIMEOUT
|
|
|
|
// 0.1.10 optimized faster WAKEUP + TIMEOUT
|
|
|
|
// 0.1.09 optimize size: timeout check + use of mask
|
|
|
|
// 0.1.08 added formula for timeout based upon clockspeed
|
|
|
|
// 0.1.07 added support for DHT21
|
|
|
|
// 0.1.06 minimize footprint (2012-12-27)
|
|
|
|
// 0.1.05 fixed negative temperature bug (thanks to Roseman)
|
|
|
|
// 0.1.04 improved readability of code using DHTLIB_OK in code
|
|
|
|
// 0.1.03 added error values for temp and humidity when read failed
|
|
|
|
// 0.1.02 added error codes
|
|
|
|
// 0.1.01 added support for Arduino 1.0, fixed typos (31/12/2011)
|
|
|
|
// 0.1.00 by Rob Tillaart (01/04/2011)
|
2013-08-28 07:57:45 -04:00
|
|
|
//
|
|
|
|
// inspired by DHT11 library
|
|
|
|
|
2021-12-16 10:44:08 -05:00
|
|
|
|
2013-08-28 07:57:45 -04:00
|
|
|
#include "dht.h"
|
|
|
|
|
2021-12-16 10:44:08 -05:00
|
|
|
|
|
|
|
#define DHTLIB_DHT11_WAKEUP 18
|
|
|
|
#define DHTLIB_DHT_WAKEUP 1
|
|
|
|
|
|
|
|
#define DHTLIB_DHT11_LEADING_ZEROS 1
|
|
|
|
#define DHTLIB_DHT_LEADING_ZEROS 6
|
|
|
|
|
|
|
|
|
|
|
|
// max timeout is 100 microseconds.
|
|
|
|
// For a 16 MHz processor 100 microseconds is 1600 clock cycles
|
|
|
|
// loops using DHTLIB_TIMEOUT use at least 4 clock cycles
|
|
|
|
// so 100 us takes max 400 loops
|
|
|
|
// so by dividing F_CPU by 40000 we "fail" as fast as possible
|
|
|
|
#ifndef F_CPU
|
|
|
|
#define DHTLIB_TIMEOUT 1000 // should be approx. clock/40000
|
|
|
|
#else
|
|
|
|
#define DHTLIB_TIMEOUT (F_CPU/40000)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-08-28 07:57:45 -04:00
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// PUBLIC
|
|
|
|
//
|
|
|
|
|
2015-03-01 05:53:47 -05:00
|
|
|
int8_t dht::read11(uint8_t pin)
|
2013-08-28 07:57:45 -04:00
|
|
|
{
|
2014-02-08 07:53:52 -05:00
|
|
|
// READ VALUES
|
2018-04-03 13:08:27 -04:00
|
|
|
if (_disableIRQ) noInterrupts();
|
2015-03-01 05:53:47 -05:00
|
|
|
int8_t result = _readSensor(pin, DHTLIB_DHT11_WAKEUP, DHTLIB_DHT11_LEADING_ZEROS);
|
2018-04-03 13:08:27 -04:00
|
|
|
if (_disableIRQ) interrupts();
|
2014-10-15 02:00:59 -04:00
|
|
|
|
|
|
|
// these bits are always zero, masking them reduces errors.
|
2015-01-21 15:46:04 -05:00
|
|
|
bits[0] &= 0x7F;
|
|
|
|
bits[2] &= 0x7F;
|
2013-08-28 07:57:45 -04:00
|
|
|
|
2014-02-08 07:53:52 -05:00
|
|
|
// CONVERT AND STORE
|
|
|
|
humidity = bits[0]; // bits[1] == 0;
|
|
|
|
temperature = bits[2]; // bits[3] == 0;
|
2013-08-28 07:57:45 -04:00
|
|
|
|
2014-02-08 07:53:52 -05:00
|
|
|
// TEST CHECKSUM
|
2017-09-20 08:47:53 -04:00
|
|
|
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
|
2014-10-08 10:37:30 -04:00
|
|
|
if (bits[4] != sum)
|
|
|
|
{
|
|
|
|
return DHTLIB_ERROR_CHECKSUM;
|
|
|
|
}
|
2014-10-15 02:00:59 -04:00
|
|
|
return result;
|
2013-08-28 07:57:45 -04:00
|
|
|
}
|
|
|
|
|
2021-12-16 10:44:08 -05:00
|
|
|
|
2017-12-12 14:58:58 -05:00
|
|
|
int8_t dht::read12(uint8_t pin)
|
|
|
|
{
|
|
|
|
// READ VALUES
|
2018-04-03 13:08:27 -04:00
|
|
|
if (_disableIRQ) noInterrupts();
|
2017-12-12 14:58:58 -05:00
|
|
|
int8_t result = _readSensor(pin, DHTLIB_DHT11_WAKEUP, DHTLIB_DHT11_LEADING_ZEROS);
|
2018-04-03 13:08:27 -04:00
|
|
|
if (_disableIRQ) interrupts();
|
2017-12-12 14:58:58 -05:00
|
|
|
|
|
|
|
// CONVERT AND STORE
|
|
|
|
humidity = bits[0] + bits[1] * 0.1;
|
2018-09-02 10:26:19 -04:00
|
|
|
temperature = bits[2] + (bits[3] & 0x7F) * 0.1;
|
|
|
|
if (bits[3] & 0x80) // negative temperature
|
2017-12-12 14:58:58 -05:00
|
|
|
{
|
|
|
|
temperature = -temperature;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TEST CHECKSUM
|
|
|
|
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
|
|
|
|
if (bits[4] != sum)
|
|
|
|
{
|
|
|
|
return DHTLIB_ERROR_CHECKSUM;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-12-16 10:44:08 -05:00
|
|
|
|
2015-03-01 05:53:47 -05:00
|
|
|
int8_t dht::read(uint8_t pin)
|
2013-08-28 07:57:45 -04:00
|
|
|
{
|
2014-02-08 07:53:52 -05:00
|
|
|
// READ VALUES
|
2018-04-03 13:08:27 -04:00
|
|
|
if (_disableIRQ) noInterrupts();
|
2015-03-01 05:53:47 -05:00
|
|
|
int8_t result = _readSensor(pin, DHTLIB_DHT_WAKEUP, DHTLIB_DHT_LEADING_ZEROS);
|
2018-04-03 13:08:27 -04:00
|
|
|
if (_disableIRQ) interrupts();
|
2014-10-15 02:00:59 -04:00
|
|
|
|
|
|
|
// these bits are always zero, masking them reduces errors.
|
2014-10-15 01:48:27 -04:00
|
|
|
bits[0] &= 0x03;
|
|
|
|
bits[2] &= 0x83;
|
2014-02-08 07:53:52 -05:00
|
|
|
|
|
|
|
// CONVERT AND STORE
|
2021-11-13 13:57:17 -05:00
|
|
|
humidity = (bits[0] * 256 + bits[1]) * 0.1;
|
|
|
|
int16_t t = ((bits[2] & 0x7F) * 256 + bits[3]);
|
|
|
|
if (t == 0)
|
|
|
|
{
|
|
|
|
temperature = 0.0; // prevent -0.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
temperature = t * 0.1;
|
|
|
|
if((bits[2] & 0x80) == 0x80 )
|
|
|
|
{
|
|
|
|
temperature = -temperature;
|
|
|
|
}
|
|
|
|
}
|
2014-02-08 07:53:52 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
// HEXDUMP DEBUG
|
|
|
|
/*
|
|
|
|
Serial.println();
|
|
|
|
// CHECKSUM
|
|
|
|
if (_bits[4] < 0x10) Serial.print(0);
|
|
|
|
Serial.print(_bits[4], HEX);
|
|
|
|
Serial.print(" ");
|
|
|
|
// TEMPERATURE
|
|
|
|
if (_bits[2] < 0x10) Serial.print(0);
|
|
|
|
Serial.print(_bits[2], HEX);
|
|
|
|
if (_bits[3] < 0x10) Serial.print(0);
|
|
|
|
Serial.print(_bits[3], HEX);
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print(temperature, 1);
|
|
|
|
Serial.print(" ");
|
|
|
|
// HUMIDITY
|
|
|
|
if (_bits[0] < 0x10) Serial.print(0);
|
|
|
|
Serial.print(_bits[0], HEX);
|
|
|
|
if (_bits[1] < 0x10) Serial.print(0);
|
|
|
|
Serial.print(_bits[1], HEX);
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print(humidity, 1);
|
|
|
|
*/
|
|
|
|
|
2014-02-08 07:53:52 -05:00
|
|
|
// TEST CHECKSUM
|
|
|
|
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
|
2014-06-26 14:48:20 -04:00
|
|
|
if (bits[4] != sum)
|
|
|
|
{
|
|
|
|
return DHTLIB_ERROR_CHECKSUM;
|
|
|
|
}
|
2014-10-15 02:00:59 -04:00
|
|
|
return result;
|
2013-08-28 07:57:45 -04:00
|
|
|
}
|
|
|
|
|
2021-12-16 10:44:08 -05:00
|
|
|
|
2013-08-28 07:57:45 -04:00
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// PRIVATE
|
|
|
|
//
|
2015-03-01 05:53:47 -05:00
|
|
|
int8_t dht::_readSensor(uint8_t pin, uint8_t wakeupDelay, uint8_t leadingZeroBits)
|
2015-01-21 15:46:04 -05:00
|
|
|
{
|
2014-02-08 07:53:52 -05:00
|
|
|
// INIT BUFFERVAR TO RECEIVE DATA
|
|
|
|
uint8_t mask = 128;
|
|
|
|
uint8_t idx = 0;
|
|
|
|
|
2014-11-25 13:40:25 -05:00
|
|
|
uint8_t data = 0;
|
|
|
|
uint8_t state = LOW;
|
|
|
|
uint8_t pstate = LOW;
|
|
|
|
uint16_t zeroLoop = DHTLIB_TIMEOUT;
|
|
|
|
uint16_t delta = 0;
|
|
|
|
|
|
|
|
leadingZeroBits = 40 - leadingZeroBits; // reverse counting...
|
|
|
|
|
2014-10-05 04:52:52 -04:00
|
|
|
// replace digitalRead() with Direct Port Reads.
|
|
|
|
// reduces footprint ~100 bytes => portability issue?
|
|
|
|
// direct port read is about 3x faster
|
2014-10-08 10:37:30 -04:00
|
|
|
uint8_t bit = digitalPinToBitMask(pin);
|
|
|
|
uint8_t port = digitalPinToPort(pin);
|
2014-10-05 04:52:52 -04:00
|
|
|
volatile uint8_t *PIR = portInputRegister(port);
|
|
|
|
|
2014-02-08 07:53:52 -05:00
|
|
|
// REQUEST SAMPLE
|
|
|
|
pinMode(pin, OUTPUT);
|
2014-10-08 10:37:30 -04:00
|
|
|
digitalWrite(pin, LOW); // T-be
|
2017-03-06 15:10:50 -05:00
|
|
|
if (wakeupDelay > 8) delay(wakeupDelay);
|
|
|
|
else delayMicroseconds(wakeupDelay * 1000UL);
|
2021-03-01 04:11:54 -05:00
|
|
|
digitalWrite(pin, HIGH); // T-go
|
2014-02-08 07:53:52 -05:00
|
|
|
pinMode(pin, INPUT);
|
|
|
|
|
2014-10-15 02:00:59 -04:00
|
|
|
uint16_t loopCount = DHTLIB_TIMEOUT * 2; // 200uSec max
|
2014-10-08 10:37:30 -04:00
|
|
|
// while(digitalRead(pin) == HIGH)
|
|
|
|
while ((*PIR & bit) != LOW )
|
|
|
|
{
|
2018-04-03 13:08:27 -04:00
|
|
|
if (--loopCount == 0)
|
2018-03-26 16:30:03 -04:00
|
|
|
{
|
|
|
|
return DHTLIB_ERROR_CONNECT;
|
|
|
|
}
|
2014-10-08 10:37:30 -04:00
|
|
|
}
|
|
|
|
|
2014-02-08 07:53:52 -05:00
|
|
|
// GET ACKNOWLEDGE or TIMEOUT
|
2014-10-08 10:37:30 -04:00
|
|
|
loopCount = DHTLIB_TIMEOUT;
|
|
|
|
// while(digitalRead(pin) == LOW)
|
2014-10-05 04:52:52 -04:00
|
|
|
while ((*PIR & bit) == LOW ) // T-rel
|
2014-06-01 03:48:06 -04:00
|
|
|
{
|
2018-04-03 13:08:27 -04:00
|
|
|
if (--loopCount == 0)
|
2018-03-26 16:30:03 -04:00
|
|
|
{
|
|
|
|
return DHTLIB_ERROR_ACK_L;
|
|
|
|
}
|
2014-06-01 03:48:06 -04:00
|
|
|
}
|
2014-02-08 07:53:52 -05:00
|
|
|
|
2014-10-08 10:37:30 -04:00
|
|
|
loopCount = DHTLIB_TIMEOUT;
|
|
|
|
// while(digitalRead(pin) == HIGH)
|
2014-10-05 04:52:52 -04:00
|
|
|
while ((*PIR & bit) != LOW ) // T-reh
|
2014-06-01 03:48:06 -04:00
|
|
|
{
|
2018-03-26 16:30:03 -04:00
|
|
|
if (--loopCount == 0)
|
|
|
|
{
|
|
|
|
return DHTLIB_ERROR_ACK_H;
|
|
|
|
}
|
2014-06-01 03:48:06 -04:00
|
|
|
}
|
2014-02-08 07:53:52 -05:00
|
|
|
|
2014-10-08 10:37:30 -04:00
|
|
|
loopCount = DHTLIB_TIMEOUT;
|
|
|
|
|
2014-02-08 07:53:52 -05:00
|
|
|
// READ THE OUTPUT - 40 BITS => 5 BYTES
|
2014-10-08 10:37:30 -04:00
|
|
|
for (uint8_t i = 40; i != 0; )
|
2014-02-08 07:53:52 -05:00
|
|
|
{
|
2014-10-08 10:37:30 -04:00
|
|
|
// WAIT FOR FALLING EDGE
|
|
|
|
state = (*PIR & bit);
|
|
|
|
if (state == LOW && pstate != LOW)
|
2014-02-08 07:53:52 -05:00
|
|
|
{
|
2014-11-25 13:40:25 -05:00
|
|
|
if (i > leadingZeroBits) // DHT22 first 6 bits are all zero !! DHT11 only 1
|
2014-10-15 02:00:59 -04:00
|
|
|
{
|
|
|
|
zeroLoop = min(zeroLoop, loopCount);
|
|
|
|
delta = (DHTLIB_TIMEOUT - zeroLoop)/4;
|
|
|
|
}
|
|
|
|
else if ( loopCount <= (zeroLoop - delta) ) // long -> one
|
2014-10-08 10:37:30 -04:00
|
|
|
{
|
2014-10-15 01:48:27 -04:00
|
|
|
data |= mask;
|
2014-10-08 10:37:30 -04:00
|
|
|
}
|
|
|
|
mask >>= 1;
|
|
|
|
if (mask == 0) // next byte
|
|
|
|
{
|
|
|
|
mask = 128;
|
2014-10-15 01:48:27 -04:00
|
|
|
bits[idx] = data;
|
2014-10-08 10:37:30 -04:00
|
|
|
idx++;
|
2014-10-15 01:48:27 -04:00
|
|
|
data = 0;
|
2014-10-08 10:37:30 -04:00
|
|
|
}
|
|
|
|
// next bit
|
|
|
|
--i;
|
2014-10-15 02:00:59 -04:00
|
|
|
|
2014-10-08 10:37:30 -04:00
|
|
|
// reset timeout flag
|
|
|
|
loopCount = DHTLIB_TIMEOUT;
|
2014-02-08 07:53:52 -05:00
|
|
|
}
|
2014-10-08 10:37:30 -04:00
|
|
|
pstate = state;
|
|
|
|
// Check timeout
|
2014-10-15 01:48:27 -04:00
|
|
|
if (--loopCount == 0)
|
|
|
|
{
|
2018-03-26 16:30:03 -04:00
|
|
|
return DHTLIB_ERROR_TIMEOUT;
|
2014-10-15 01:48:27 -04:00
|
|
|
}
|
2014-10-15 02:00:59 -04:00
|
|
|
|
2014-02-08 07:53:52 -05:00
|
|
|
}
|
2017-07-26 19:18:02 -04:00
|
|
|
// pinMode(pin, OUTPUT);
|
|
|
|
// digitalWrite(pin, HIGH);
|
2014-02-08 07:53:52 -05:00
|
|
|
return DHTLIB_OK;
|
2013-08-28 07:57:45 -04:00
|
|
|
}
|
2021-12-16 10:44:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|