Page tree
Skip to end of metadata
Go to start of metadata

This data measurement is implemented using  Adafruit Feather M0 WiFi, Adafruit ESP32, and Adafruit Huzzah ESP8266 micro-controllers using WiFi and REST API.

Table of Contents

1.0 DHTxx Overview

An overview of the DHT11 and DHT22 sensors can be found here.

2.0 Diagram of DHT to Adafruit connection

The DHT22 connection to Adafruit microcontrollers is shown below.

Connection tips:

  • connect pin 1 of DHT sensor to 3.3V
  • connect pin 2 of sensor to DHTPIN
  • connect sensor pin 4 to GROUND
  • connect a 10k resistor between pins 1 and 2 of sensor

3.0 Implementation

The Library

Create the "config.h" file as described here

Include the config and DHT libraries

Include library
#include "config.h"
#include "DHT.h"


Define DHT type, pin and create DHT object "dht"

#define DHTPIN 6
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

#define DEVICE_ID   "your_device_id"
#define DATA_STREAM "Temperature and Humidity measurements"


Connect to the WiFi network and UCW IoT Cloud

this is done  in the "void setup" section

connect method
 // Connect to UCW IoT Cloud
  Serial.print("Connecting to UCW IoT Cloud...");
  ucw.connect();

  // Wait for a connection
  while (ucw.status() != UCW_NET_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  // We are connected
  Serial.println(" Connected!");
  ucw.printNetworkInfo(); 

dht.begin()

this method is called here

dht.begin()
dht.begin();

sys()

this confirms micor-controller is still connected to cloud

sys()
ucw.sys();

Read data

  • the DHT22 sensor measures both temperature (Celsius and Fahrenheit ) and humidity
  • DHT library has an internal method for computing the heat index using the temperature and humidity measurements

Read data
//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("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);

String data = "{\"humidity\": %humidity, \"temperatureC\": %temperatureC,\"temperatureF\": %temperatureF,\"heat_indexC\": %heat_indexC,\"heat_indexF\": %heat_indexF}";
data.replace("%humidity", String(h));
data.replace("%temperatureC", String(t));
data.replace("%temperatureF", String(f));
data.replace("%heat_indexC", String(hic));
data.replace("%heat_indexF", String(hif));

Sending the data

The data is sent to the server using the sendData(String your_deviceID, String your_dataStreamName,String payload) method

Example using sendData method
ucw.sendData(DEVICE_ID, DATA_STREAM, data);


The full example can be found here.

  • No labels