// UH 5COM2004
// Practical 3 - DHT Sensor
// 3.1 - Library
//
// Use a DFU Temperature and Humidity Sensor
// Make use of the DHT library
// https://github.com/adafruit/DHT-sensor-library
// https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/
#include "DHT.h"
#define DHTPIN 14 // 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.
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// initialise the sensor
DHT dht(DHTPIN, DHTTYPE);
// container variables
float humidity;
float temp_c;
float temp_f;
float hic; // heat index for celsius temp
float hif; // heat index for fahrenheit temp
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
}
void loop() {
// put your main code here, to run repeatedly:
// wait 2s between measurements
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
temp_c = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
temp_f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temp_c) || isnan(temp_f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(temp_f, humidity);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(temp_c, humidity, false);
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F("% Temperature: "));
Serial.print(temp_c);
Serial.print(F("C "));
Serial.print(temp_f);
Serial.print(F("F Heat index: "));
Serial.print(hic);
Serial.print(F("C "));
Serial.print(hif);
Serial.println(F("F"));
}