Connection of a sensor

In the next step, the sensor for the detection of humidity and temperature should be added.

For this project, the popular digital sensor DHT11 or DHT22 (more expensive but more accurate) is used.

 Working Task

Commission the sensor and the additional signal lamps in the following steps:

  • Install the required library for the DHT22 sensor
  • Create a functional description of the program shown in the information section
  • Connect the LEDs as signal lights to the outputs specified by the program

Below is a program code that has been supplemented with the integration of the DHT22 sensor and other signal lights for the display of temperature and humidity limits.

#include <ESP8266WiFi.h>
#include <DHT.h>

#define DHTPIN D4
#define DHTTYPE DHT22 // DHT 22 (AM2302)

// Sensor
DHT dht(DHTPIN, DHTTYPE);
long lastSensorTime = 0;
float lastTemperature = 0.0f;
float lastHumidity = 0.0f;

// local hardware
const int leds[]={D0, D1, D2, D3, D5, D6, D7, D8};
const int ledCount = 8;
const int signalTCold = D0;
const int signalTOK = D1;
const int signalTHot = D2;
const int signalHDry = D3;
const int signalHOK = D5;
const int signalHWet = D6;
const int signalFan = D7;
const int signalHeating = D8;
bool activateFan=false;
bool activateHeating=false;

void processInputs() {
 long now = millis();
 float t= dht.readTemperature();
 float h= dht.readHumidity();
 if(!isnan(t) && !isnan(h)) {
   lastSensorTime = now;
   lastTemperature = t;
   lastHumidity = h; 
 }
}

void processOutputs() {
 digitalWrite(signalTCold, LOW);
 digitalWrite(signalTOK, LOW);
 digitalWrite(signalTHot, LOW); 
 if(lastTemperature < 18){
   digitalWrite(signalTCold, HIGH);
 } else if(lastTemperature > 22) {
   digitalWrite(signalTHot, HIGH); 
 } else {
   digitalWrite(signalTOK, HIGH);
 }

 digitalWrite(signalHWet, LOW);
 digitalWrite(signalHOK, LOW);
 digitalWrite(signalHDry, LOW); 
 if(lastHumidity < 50){
   digitalWrite(signalHDry, HIGH);
 } else if(lastHumidity > 60) {
   digitalWrite(signalHWet, HIGH); 
 } else {
   digitalWrite(signalHOK, HIGH);
 }
 digitalWrite(signalFan, activateFan ? HIGH : LOW);
 digitalWrite(signalHeating, activateHeating ? HIGH : LOW);
}

void loop() {
 processInputs();
 processOutputs();
}

Leave a Reply