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

Hi Guys,

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

#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%.

If this is working:

float h = dht.readHumidity();

Why are you doing this?

value = analogRead(hygrometer);

lg, couka

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.

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

got it to work with the following:

#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();
}