// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
#include "DHT.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define _mypin 2 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
uint8_t ourData[5];
uint8_t pullTime;
uint32_t maxcycles, _lastreadtime;
bool _lastresult;
// Define TIMEOUT constant if not already defined
#define MIN_INTERVAL 2000 /**< min interval value */
#define TIMEOUT UINT32_MAX
// Function to simulate expectPulse behavior
uint32_t expectPulse(bool level) {
uint32_t count = 0;
while (digitalRead(_mypin) == level) {
if (count++ >= maxcycles) {
return TIMEOUT; // Exceeded timeout, fail.
}
}
return count;
}
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
pinMode(_mypin, INPUT_PULLUP);
_lastreadtime = millis() - MIN_INTERVAL;
pullTime = 55;
maxcycles = microsecondsToClockCycles(1000);
dht.begin();
// Reset 40 bits of received data to zero.
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
uint32_t currenttime = millis();
if (((currenttime - _lastreadtime) < MIN_INTERVAL)) {
return _lastresult; // return last correct measurement
}
_lastreadtime = currenttime;
ourData[0] = ourData[1] = ourData[2] = ourData[3] = ourData[4] = 0;
pinMode(_mypin, INPUT_PULLUP);
delay(1);
// First set data line low for a period according to sensor type
pinMode(_mypin, OUTPUT);
digitalWrite(_mypin, LOW);
delayMicroseconds(1100);
uint32_t ourCycles[80];
{
pinMode(_mypin, INPUT_PULLUP);
delayMicroseconds(pullTime);
if (expectPulse(LOW) == TIMEOUT) {
_lastresult = false;
return _lastresult;
}
if (expectPulse(HIGH) == TIMEOUT) {
_lastresult = false;
return _lastresult;
}
//InterruptLock lock;
for (int i = 0; i < 80; i += 2) {
ourCycles[i] = expectPulse(LOW);
ourCycles[i + 1] = expectPulse(HIGH);
}
} // Timing critical code is now complete.
// Inspect pulses and determine which ones are 0 (high state cycle count < low
// state cycle count), or 1 (high state cycle count > low state cycle count).
for (int i = 0; i < 40; ++i) {
uint32_t lowCycles = ourCycles[2 * i];
uint32_t highCycles = ourCycles[2 * i + 1];
ourData[i / 8] <<= 1;
// Now compare the low and high cycle times to see if the bit is a 0 or 1.
if (highCycles > lowCycles) {
// High cycles are greater than 50us low cycle count, must be a 1.
ourData[i / 8] |= 1;
}
}
if (ourData[4] == ((ourData[0] + ourData[1] + ourData[2] + ourData[3]) & 0xFF)) {
Serial.print(F("Checksum Okay"));
} else {
Serial.print(F("Checksum Not Okay"));
}
float ourValue = NAN;
ourValue = ((word)(ourData[2] & 0x7F)) << 8 | ourData[3];
ourValue *= 0.1;
if (ourData[2] & 0x80) {
ourValue *= -1;
}
Serial.print(F("Temperatur: "));
Serial.print(ourValue);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.println(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}