In this scenario, we will introduce a popular DHT11 sensor. The DHT series covers DHT11, DHT22, and AM2302. Those sensors differ in accuracy and physical dimensions but can all read environmental temperature and humidity. This scenario can be run stand-alone to read weather data in the laboratory nodes' room. The DHT11 sensor is controlled with one GPIO (in all our laboratory nodes, it is GPIO 47) and uses a proprietary protocol.
Air temperature and humidity can be easily read using a dedicated library. Actually, you need to include two of them, as presented below:
lib_deps = adafruit/DHT sensor library@^1.4.6 adafruit/Adafruit Unified Sensor@^1.1.9
Sensor readings can be sent over the network or presented on one of the node's displays (e.g. LCD), so understanding how to handle at least one of the displays is essential:
It is also possible to present the temperature as the LED colour changes with a PWM-controlled LED or LED stripe. Their usage is described here:
A good understanding of the hardware timers is essential if you plan to use asynchronous programming (see note below). Consider getting familiar with the following:
In this scenario, we only focus on reading the sensor (temperature and humidity). Information on how to display measurements is part of other scenarios that you should refer to to create a fully functional solution (see links above).
Present the current temperature, and humidity on any display (e.g. LCD). Remember to add units (C, %Rh).
A general check to see if you can see the chosen display in the camera field of view is necessary. No other actions are required before starting development.
The steps below present only interaction with the sensor. Those steps should be supplied to present the data (or send it over the network) using other scenarios accordingly.
Include the DHT library and related sensor library.
#include <Adafruit_Sensor.h> #include <DHT.h>
Declare type of the sensor and GPIO pin:
#define DHTTYPE DHT11 // DHT 11 #define DHTPIN 47
Declare controller class and variables to store data:
static DHT dht(DHTPIN, DHTTYPE,50); static float hum = 0; static float temp = 0; static boolean isDHTOk = false;
Initialise sensor (mind the delay(100);
after initialisation as the DHT11 sensor requires some time to initialise before one can read the temperature and humidity:
dht.begin(); delay(100);
Reat the data and check whether the sensor works OK. In the case of the DHT sensor and its controller class, we check the correctness of the readings once the reading is finished. The sensor does not return any status but checks if the reading is okay. This can be done by comparing the readings with the NaN
(not a number) value, each separately:
hum = dht.readHumidity(); temp = dht.readTemperature(); (isnan(hum) || isnan(temp))?isDHTOk = false:isDHTOk = true;
NaN
numbers. Please give it some 250ms, at least, between consecutive readings, whether you do it asynchronously or using a blocking call of delay(250);
in the loop.
The observed temperature is usually between 19 and 24C, with humidity about 40-70%, depending on the weather. On rainy days, it can even go higher.
I've got NaN (Not a Number) readings. What to do?: Check if GPIO is OK (should be D22), check if you initialised controller class and most of all, give the sensor some recovery time (at least 250ms) between consecutive readings.