# ESP8266-DHT22-BLYNK- auto trigger relay dependant on humidity

**URL:** <https://forum.arduino.cc/t/esp8266-dht22-blynk-auto-trigger-relay-dependant-on-humidity/389073>\
**Category:** General Guidance\
**Created:** [May 30, 2016, 5:48pm UTC](https://forum.arduino.cc/t/esp8266-dht22-blynk-auto-trigger-relay-dependant-on-humidity/389073 "2016-05-30T17:48:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![reptile\_edge](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@reptile\_edge](https://forum.arduino.cc/u/reptile_edge)\
**Post date:** [May 30, 2016, 5:48pm UTC](https://forum.arduino.cc/t/esp8266-dht22-blynk-auto-trigger-relay-dependant-on-humidity/389073/1 "2016-05-30T17:48:48Z")

</div>

Hi Guys,

I have been doing alot more research and am trying to get my project full operational. My code is as follows:

```cpp
#define BLYNK_PRINT Serial    
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN 12 
#define DHTTYPE DHT22  
//Constants 
const int airPump = 9;    
const int hygrometer = 12;  

//Variables 

int value;

char auth[] = " ************"; 

SimpleTimer timer;

void setup()
{
  Serial.begin(9600); 
  Blynk.begin(auth, "ssid", "pass!");
  

  timer.setInterval(1000L, sendUptime);

    pinMode(airPump,OUTPUT);
}

void sendUptime()
{
  
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  Blynk.virtualWrite(10, t); // virtual pin
  Blynk.virtualWrite(11, h); // virtual pin
}

void loop()
{
 
  
  value = analogRead(hygrometer);  //Read analog value 
  value = constrain(value,400,1023);  //Keep the ranges!
  value = map(value,400,1023,100,0);  //Map value : 400 will be 100 and 1023 will be 0
  
  if (value < 50){
    digitalWrite(airPump,HIGH);
  }

  else {
      digitalWrite(airPump,LOW);
  }

  delay(150); //small delay
  Blynk.run();
  timer.run();

}

```

I am trying to be able to read temperature and humidity from my phone but also auto trigger a pump through a relay if humidity reaches below 50%. i cant seem to get it right, i think it might be reading temperature instead of humidity, how do i separate the two? What have i done wrong?

Parts:

ESP8266 Sparkfun Thing Dev  
DHT22

using a led just to see if it works and it is but wont shut off if above 50%.

---

<div class="post-metadata">

**Author:** ![couka](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/couka/32/223382_2.png) [@couka](https://forum.arduino.cc/u/couka)\
**Post date:** [May 30, 2016, 6:15pm UTC](https://forum.arduino.cc/t/esp8266-dht22-blynk-auto-trigger-relay-dependant-on-humidity/389073/2 "2016-05-30T18:15:59Z")

</div>

If this is working:

```cpp
float h = dht.readHumidity();

```

Why are you doing this?

```cpp
value = analogRead(hygrometer);

```

lg, couka

---

<div class="post-metadata">

**Author:** ![reptile\_edge](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@reptile\_edge](https://forum.arduino.cc/u/reptile_edge)\
**Post date:** [May 30, 2016, 6:29pm UTC](https://forum.arduino.cc/t/esp8266-dht22-blynk-auto-trigger-relay-dependant-on-humidity/389073/3 "2016-05-30T18:29:06Z")

</div>

Not sure, how would i edit the code to make it work? I Frankensteined a bunch of code together to make this. Still a hardcore newb.

---

<div class="post-metadata">

**Author:** ![couka](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/couka/32/223382_2.png) [@couka](https://forum.arduino.cc/u/couka)\
**Post date:** [May 30, 2016, 6:40pm UTC](https://forum.arduino.cc/t/esp8266-dht22-blynk-auto-trigger-relay-dependant-on-humidity/389073/4 "2016-05-30T18:40:25Z")

</div>

Please read the code carefully to understand what every single line is doing, then write it again on your own.

You connected a digital sensor to pin 12 and you are using a library to read it. And then suddenly you are using analogRead() on it.

That doesn't make any sense.

Don't get me wrong, we are here to help, but don't just copy&paste some code you don't understand and then ask why it doesn't work.

lg, couka

---

<div class="post-metadata">

**Author:** ![reptile\_edge](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@reptile\_edge](https://forum.arduino.cc/u/reptile_edge)\
**Post date:** [May 30, 2016, 7:26pm UTC](https://forum.arduino.cc/t/esp8266-dht22-blynk-auto-trigger-relay-dependant-on-humidity/389073/5 "2016-05-30T19:26:21Z")

</div>

got it to work with the following:

```cpp
#define BLYNK_PRINT Serial    
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN 12 //pin gpio 12 in sensor
#define DHTTYPE DHT22   
DHT dht(DHTPIN, DHTTYPE);

//Constants 
const int airPump = 15;   
const int humidity = 12;  

int value;
int minhum = 40;

char auth[] = "0 *********";  

SimpleTimer timer;

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, "SSID", "pass"); 

  timer.setInterval(1000L, sendUptime);

    pinMode(airPump,OUTPUT);
}

void sendUptime()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  Blynk.virtualWrite(10, t); 
  Blynk.virtualWrite(11, h); 
}

void loop()
{
    
  float h = dht.readHumidity();
  if(h < minhum){
    digitalWrite(airPump,HIGH);
  }
  else {
      digitalWrite(airPump,LOW);
  }
  delay(150); 
  Blynk.run();
  timer.run();
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 5:52pm UTC](https://forum.arduino.cc/t/esp8266-dht22-blynk-auto-trigger-relay-dependant-on-humidity/389073/6 "2021-05-05T17:52:37Z")

</div>

Closed on May 5, 2021, 5:52pm UTC


