Shield Wemos D1 Mini DHT11 or DHT22: Arduino code – ESP Easy

Wemos offers a wide range of expansion cards (Shield) for the D1 Mini. They are grouped in this article of presentation as they are released. In this quick tutorial, we will discover how to assemble the DHT22 shield and use it in an Arduino code or using the ESP Easy firmware.

Unpacking the DHT22 shield

Like all Wemos cards, this card comes with 3 types of connectors to be welded:

  • For prototyping board (breadboard)
  • Short stack connector
  • Connector to be stacked long

The board measures 36 (length) x 25 (width) x 9.6 (height) mm. It will be necessary to add a few additional millimeters according to the chosen connector.

shield wemos d1 mini dht22 unboxing deballage

Shield DHT22 for Wemos D1 Mini

Once assembled, this configuration is obtained, for example. It is still possible to stack other shields (OLED screen, power on battery …)

shield dht22 wemos d1 mini monte empile assemble

Shield DHT22 assembled on Wemos D1 Mini

The Shield embeds a DHT22 quite classic, the same one found in the many Arduino / Raspberry Pi learning kits. We will therefore be able to use the libraries already developed for the 2 platforms, or rather say the 3 platforms if The ESP8266 is considered a platform in its own right.

The DHT22 (or DHT11) is soldered to the expansion board. It is therefore not possible to choose the spindle. We will retrieve the measurements on pin D4, equivalent to the GPIO-2 of Arduino.

Arduino Code

Before you begin, you need to install the Adafruit library (most used). It is very easy to install from the Arduino IDE library manager.

ide arduino librairie dht

Create a new sketch and paste this code. No need to change the Pin.

/* DHT Shield - Simple
 *
 * Example testing sketch for various DHT humidity/temperature sensors
 * Written by ladyada, public domain
 *
 * Depends on Adafruit DHT Arduino library
 * https://github.com/adafruit/DHT-sensor-library
 */

#include "DHT.h"

#define DHTPIN D4     // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT12   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  // Wait a few seconds 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)
  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("Impossible de lire la sonde DHT!");
    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.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");
}

Source : https://github.com/wemos/D1_mini_Examples/blob/master/examples/04.Shields/DHT_Shield/Simple/Simple.ino

Download the program to the Wemos and open the serial monitor to receive the measurements.

Several examples have been made available by Wemos on Github here

  • Simple.ino: example above
  • SimpleServer.ino: a good example to learn how to develop an ESP8266 as a WEB server. One opens an internet page in a browser to read the measurements returned by the probe.
  • DeepSleep.ino: an example integrating the ESP8266 sleeping and waking at regular intervals for battery operation.
    With ESP Easy: a temperature / humidity sensor in 5 minutes!

If you have only a small connected temperature sensor to be developed, ESP Easy will suffice. The DHT11 / DHT22 sensors are very well managed. If you are starting out with ESP Easy, follow this tutorial to install the firmware and configure the Wi-Fi connection of the Wemos D1 Mini.

Once connected, go to the Device tab and click on an Edit (any line). From the Devices list, choose Temperature & Humidity – DHT, and then adjust the settings like this

  • Name: give a name.
  • Delay: the refresh time of the measurements. No need to go too fast.
  • IDX / Var: choose an identifier (1 to 255), different from 0 otherwise it does not work. If you use Domoticz, it will be the internal ID
  • GPIO: GPIO-2
  • DHT Type: select the type of your card (DHT11 or DHT22)
  • Check Send Data if you want to send the measurements to a home automation server
  • Formula: not really used
  • Decimals: You can limit to one digit after the decimal point. That’s enough.
  • Name variables 1 and 2. Choose a short name, with no spaces or accented characters. It is used to identify variables to communicate with a home automation server, MQTT, etc …

esp easy configuration device dht22 wemos d1 mini shield

You can now easily integrate your probe to a home automation server (Jeedom, Domoticz) or retrieve the measurements with Node-RED to create graphics with Grafana.

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

Tags:

We will be happy to hear your thoughts

Leave a Reply

DIY Projects
%d bloggers like this: