Have a Question?
Table of Contents
< All Topics

301 Measuring Temperature and Humidity

Measuring Temperature and humidity is easier in some respects and much more difficult in others than I thought it would be.  There is a bunch of information and libraries for the DHT11 and DHT22 sensors; some are great, some not so much.  I am hoping mine is great, but we will see.

We are going to start with the cheaply popular DHT22 sensor. The DHT22 is the white version of this DHT series sensor. I have found the DHT11 (Blue) is only half as fast as the DHT22, but in my quick testing, the accuracy is just about as good.  The specs say differently, though that is not to say that the DHT22 is accurate. There are much better sensors on the market and are much more expensive. Let’s stick with the DHT22 because it’s good enough, and I have a few. The Uctronics ESP8266 board will drive this.

Step 1 Wiring.

I found an extra wide breadboard somewhere, but I can’t remember where sorry. It is six pins wide instead of the standard five and works great for this, The 5-pin will also work but it doesn’t go to 12. Starting with figuring out where you want to place everything. I like to place my stuff at the ends of the board. This makes it much easier to figure out which holes are what, especially with the DTH22.

DHT22 Breadboard Layout
DHT22 Breadboard Layout

DHT22 Breadboard Layout

The pinouts for the DHT22 are as follows:

Finished wiring

I connected the red wire from 3v3 on the Uctronics ESP8266 to Pin 1 on the DHT22.  Then I connected the black wire to GND on the Uctronics ESP8266 to Pin 4 on the DHT22. Finally, I connected the yellow wire from D1 on the Uctronics ESP8266 to Pin2 on the DHT22.

Note: if you are using the DHT11 with the kit, you would wire it up like this:

DHT11 Wiring
DHT11 Wiring

Step 2 Code:

// Wifi Configuration
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
const char* ssid = "WiFiname";
const char* password = "PASSWORD";
String serverName = "http://FillInServerURL";
unsigned long lastTime = 0;
unsigned long timerDelay = 900000;
// Setup Sensor
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 4   
#define DHTTYPE DHT22 
float hum;
float temp;
// Setup Lights
#define ledPin1 2 /* LED connected to GPIO 2 */
#define ledPin2 16 /* LED connected to GPIO 16 */

void setup() {
  // Sensor Setup

  // Wifi Setup
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  WiFi.begin(ssid, password);
  while(WiFi.status() != WL_CONNECTED) {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin1, LOW);
  Serial.print("Connected to WiFi network with IP Address: ");
  digitalWrite(ledPin1, LOW);
  Serial.println("Waiting for first reading");

void loop() {
  // Send an HTTP POST request depending on timerDelay
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      float converted = 0.00;
      hum = 0;
      temp = 0;
      hum = dht.readHumidity();
      temp= dht.readTemperature();
      converted = ( temp * 1.8 ) + 32;
      // Send Results
      WiFiClient client;
      HTTPClient http;
      String serverPath = serverName + "?Location=" + WiFi.macAddress() + "&Temperature=" +converted + "&Humidity=" + hum;
      http.begin(client, serverPath.c_str());
      int httpResponseCode = http.GET();
      //Check Results
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        String payload = http.getString();
      else {
        Serial.print("Error code: ");
      // Free resources
      digitalWrite(ledPin2, HIGH);
    else {
      Serial.println("WiFi Disconnected");
      digitalWrite(ledPin1, HIGH);
    lastTime = millis();