Arduino library · Sensors

DHT sensor library for ESPx: an example that runs

Yes. You can use the DHT sensor library for ESPx library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Temperature and humidity, DHT22). The example below, "Temperature, humidity and heat index with DHTesp", runs on the Arduino Uno R3.

Built in on Mokxi: #include <DHTesp.h> and it builds, with nothing to install. Free, and no account needed.

What DHT sensor library for ESPx does

DHTesp: setup(pin, DHTesp::DHT22), getTempAndHumidity(), getStatusString(), heat index and dew point. That is what DHT sensor library for ESPx, by beegee_tokyo, covers. The sketch below uses it on the Arduino Uno: “Temperature, humidity and heat index with DHTesp”.

The interface of beegee-tokyo's "DHT sensor library for ESPx" (DHTesp), the DHT11 and DHT22 library many ESP32 and ESP8266 tutorials use.

As upstream: a reading is taken at most once a sampling period (1 s for a DHT11, 2 s for a DHT22) and the calls in between return the last one. A failed read is NaN, with getStatus() saying TIMEOUT or CHECKSUM.

The calls follow release 1.19.0 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameDHT sensor library for ESPx
Authorbeegee_tokyo
Include#include <DHTesp.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesTemperature and humidity, DHT22

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <DHTesp.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "DHT sensor library for ESPx" by beegee_tokyo from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add DHT sensor library for ESPx to lib_deps in PlatformIO. Mokxi follows the calls of release 1.19.0, so a sketch written here builds there too.

What to know in the simulator: getComfortRatio() uses a fixed comfort box of 20 to 26 C and 30 to 70 %, not upstream’s comfort profile.

The calls you will use

In the order a sketch meets them.

DHTesp dht

Makes dht, the DHTesp object the calls below are made on.

dht.setup(2, DHTesp::DHT22)

Starts it up. Call it once in setup().

dht.getMinimumSamplingPeriod()

Returns the minimum sampling period as a whole number.

TempAndHumidity v

Declares v, a TempAndHumidity that the example uses.

dht.getTempAndHumidity()

Returns the temp and humidity as a TempAndHumidity.

dht.getStatusString()

Returns the status string as text.

dht.computeHeatIndex(v.temperature, v.humidity)

Computes the heat index and returns a decimal number. Arguments: temperature, percent humidity, is fahrenheit.

Example: Temperature, humidity and heat index with DHTesp

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include "DHTesp.h"

DHTesp dht;

void setup() {
  Serial.begin(115200);
  dht.setup(2, DHTesp::DHT22);
}

void loop() {
  delay(dht.getMinimumSamplingPeriod());
  TempAndHumidity v = dht.getTempAndHumidity();
  Serial.print(dht.getStatusString());
  Serial.print("\tTemperature: ");
  Serial.print(v.temperature, 1);
  Serial.print(" C\tHumidity: ");
  Serial.print(v.humidity, 1);
  Serial.print(" %\tHeat index: ");
  Serial.println(dht.computeHeatIndex(v.temperature, v.humidity), 1);
}

Wiring

  • Arduino Uno R3 2 to Temperature and humidity, DHT22 DATA
  • Arduino Uno R3 5V to Temperature and humidity, DHT22 VCC
  • Arduino Uno R3 GND to Temperature and humidity, DHT22 GND

Open it in the editor, wired and ready to run

Try DHT sensor library for ESPx without installing anything

Open the example, change the sketch, and keep your version in a free account.